mirror of
https://github.com/jina-ai/node-DeepResearch.git
synced 2025-12-26 14:30:17 +08:00
improve image tools
This commit is contained in:
parent
609687aaeb
commit
0ee295b83e
@ -51,13 +51,11 @@ const _loadImage = async (input: string | Buffer) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const img = await canvas.loadImage(buff).catch((err) => {
|
const img = await canvas.loadImage(buff).catch((err) => {
|
||||||
throw new Error('Loading image failed: ' + err.message);
|
console.error('Error loading image:', err);
|
||||||
|
return undefined;
|
||||||
});
|
});
|
||||||
Reflect.set(img, 'contentType', contentType);
|
|
||||||
|
|
||||||
return {
|
return img;
|
||||||
img,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const loadImage = async (uri: string | Buffer) => {
|
export const loadImage = async (uri: string | Buffer) => {
|
||||||
@ -105,7 +103,7 @@ export const canvasToBuffer = (canvas: canvas.Canvas, mimeType?: 'image/png' | '
|
|||||||
|
|
||||||
export const processImage = async (url: string, tracker: TokenTracker): Promise<ImageObject | undefined> => {
|
export const processImage = async (url: string, tracker: TokenTracker): Promise<ImageObject | undefined> => {
|
||||||
try {
|
try {
|
||||||
const { img } = await loadImage(url);
|
const img = await loadImage(url);
|
||||||
if (!img) {
|
if (!img) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -117,6 +115,7 @@ export const processImage = async (url: string, tracker: TokenTracker): Promise<
|
|||||||
|
|
||||||
const canvas = fitImageToSquareBox(img, 256);
|
const canvas = fitImageToSquareBox(img, 256);
|
||||||
const base64Data = (await canvasToDataUrl(canvas)).split(',')[1];
|
const base64Data = (await canvasToDataUrl(canvas)).split(',')[1];
|
||||||
|
img.src = ''; // Clear the image source to free memory
|
||||||
|
|
||||||
const {embeddings} = await getEmbeddings([{ image: base64Data }], tracker, {
|
const {embeddings} = await getEmbeddings([{ image: base64Data }], tracker, {
|
||||||
dimensions: 512,
|
dimensions: 512,
|
||||||
|
|||||||
@ -560,8 +560,9 @@ export async function processURLs(
|
|||||||
// Process images
|
// Process images
|
||||||
if (withImages && data.images) {
|
if (withImages && data.images) {
|
||||||
const imageEntries = Object.entries(data.images || {});
|
const imageEntries = Object.entries(data.images || {});
|
||||||
|
let imageObject: any;
|
||||||
imageEntries.forEach(async ([alt, url]) => {
|
imageEntries.forEach(async ([alt, url]) => {
|
||||||
const imageObject = await processImage(url, context.tokenTracker);
|
imageObject = await processImage(url, context.tokenTracker);
|
||||||
if (imageObject && !imageObjects.find(i => i.url === imageObject.url)) {
|
if (imageObject && !imageObjects.find(i => i.url === imageObject.url)) {
|
||||||
imageObjects.push(imageObject);
|
imageObjects.push(imageObject);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user