Search code examples
node.jstensorflowmultipartform-data

Image parsing issue with @tensorflow-models/mobilenet in NodeJs


I've created one API where I need to check Nudity, violence and vulgarity before uploading images to AWS. I found @tensorflow-models/mobilenet but the code gives me a runtime error like below.

throw new Error('pixels passed to tf.browser.fromPixels() must be either an ' +
          ^

Error: pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, but was Object

    at fromPixels_ (/Users/apple/Documents/flutter_projects/rest_shopping/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:26357:15)
    at Object.fromPixels__op [as fromPixels] (/Users/apple/Documents/flutter_projects/rest_shopping/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:5612:29)

Relevent Code:

const tf = require('@tensorflow/tfjs');
const models = require('@tensorflow-models/mobilenet');

route.post("/upload-image", upload.single('image'), async (req, res) => {
  const image = req.file;
  console.log(image);

  const model = await models.load();

  const predictions = await model.classify(image);

  console.log('Predictions: ');
  console.log(predictions);
  res.send('File uploaded successfully!');
});

Note: I'm not using any webpage to upload images. These are mobile APIs where images are being uploaded from mobile app to server


Solution

  • Now I'm able to parse it after adding these two lines

    const file = fs.readFileSync(image.path);
    const adaptedImg = tf.node.decodeImage(file, 3);
    

    Use adaptedImg to classify

    const predictions = await model.classify(adaptedImg);