in src/nodes/server/image-classification.ts [34:112]
async runWithInputs(inputs: Inputs, services: Services) {
const { image, apikey, modelid } = inputs;
const _modelid = modelid?.trim();
if (this.hf && apikey) {
this.hf = new HfInference(apikey);
}
if (!image) {
this.dispatchEvent(
new CustomEvent("outputs", { detail: { results: null } })
);
return;
}
if (this.cachedResult && compareObjects(this.cachedInputs, inputs)) {
this.dispatchEvent(
new CustomEvent("outputs", { detail: this.cachedResult })
);
return;
}
const canvas = services.resourceService.get(
image.canvasId
) as HTMLCanvasElement;
const canvasBlob = await new Promise<Blob | null>((resolve) => {
canvas.toBlob((blob) => {
resolve(blob);
});
});
if (!canvasBlob) {
this.dispatchEvent(
new CustomEvent("outputs", {
detail: {
error: {
title: "Error",
message: "Invalid canvas",
},
},
})
);
return;
}
try {
const imageClassRes = await this.hf?.imageClassification({
model: _modelid,
data: canvasBlob,
});
if (!imageClassRes) {
throw new Error("Invalid response");
}
const classProb = imageClassRes.map((e) => ({
className: e.label,
probability: e.score,
}));
this.cachedInputs = inputs;
this.cachedResult = {
results: { classes: classProb },
};
this.dispatchEvent(
new CustomEvent("outputs", { detail: this.cachedResult })
);
} catch (error: any) {
this.dispatchEvent(
new CustomEvent("outputs", {
detail: {
error: {
title: "Error",
message: error.message,
},
},
})
);
}
}