export function foodimage_beforeInsert(item, context) {
//TODO: write your code here..
let food = predict(item.image);
item.name = food;
return item;
}
async function predict(imgElement) {
const logits = tf.tidy(() => {
// tf.browser.fromPixels() returns a Tensor from an image element.
const img = tf.browser.fromPixels(imgElement).toFloat();
imgElement = tf.image.resizeNearestNeighbor(imgElement, [IMAGE_SIZE, IMAGE_SIZE]);
const offset = tf.scalar(127.5);
// Normalize the image from [0, 255] to [-1, 1].
const normalized = img.sub(offset).div(offset);
// Reshape to a single-element batch so we can pass it to predict.
const batched = normalized.reshape([1, IMAGE_SIZE, IMAGE_SIZE, 3]);
// Make a prediction through mobilenet.
return mobilenet.predict(batched);
});
// Convert logits to probabilities and class names.
const classes = await getTopKClasses(logits, TOPK_PREDICTIONS);
// $w("#dataset1").setFieldValue("name", classes[0].className);
return classes[0].className;
}
export async function getTopKClasses(logits, topK) {
const values = await logits.data();
const valuesAndIndices = [];
for (let i = 0; i < values.length; i++) {
valuesAndIndices.push({value: values[i], index: i});
}
valuesAndIndices.sort((a, b) => {
return b.value - a.value;
});
const topkValues = new Float32Array(topK);
const topkIndices = new Int32Array(topK);
for (let i = 0; i < topK; i++) {
topkValues[i] = valuesAndIndices[i].value;
topkIndices[i] = valuesAndIndices[i].index;
}
const topClassesAndProbs = [];
for (let i = 0; i < topkIndices.length; i++) {
topClassesAndProbs.push({
// className: food_classes[topkIndices[i]],
className: IMAGENET_CLASSES[topkIndices[i]],
probability: topkValues[i]
})
}
return topClassesAndProbs;
}
I have a data hook, and I want to analysis user uploaded image by using tfjs. However, tf.brower.fromPixels doesn’t support the url format from item.image. How can I convert item.image so that I can tf.brower.fromPixels()? Thank you very much!