I am trying to do some image recognition using @tensorflow/tfjs-node and mobilenet. Here is the code:
import fs from 'fs';
import * as tf from '@tensorflow/tfjs-node'; // TensorFlow.js for Node.js
import * as mobilenet from '@tensorflow-models/mobilenet'; // MobileNet for image classification
import sharp from 'sharp';
const classifyImage = async (imagePath) => {
try {
await tf.setBackend('tensorflow');
// Resize the image to 224x224 as required by MobileNet
const resizedImageBuffer = await sharp(imagePath)
.resize(224, 224) // MobileNet requires 224x224 images
.toBuffer();
// Decode the resized image to a tensor
const imageTensor = tf.node.decodeImage(resizedImageBuffer, 3);
// Load the MobileNet model
const model = await mobilenet.load();
// Perform classification
const predictions = await model.classify(imageTensor);
console.log('Predictions:', predictions);
// Clean up tensors to avoid memory leaks
tf.dispose(imageTensor);
} catch (error) {
console.error('Error during image classification:', error);
}
};
// Provide the image path as an argument
const imagePath = process.argv[2];
if (!imagePath) {
console.error('Please provide an image path as the first argument.');
process.exit(1);
}
classifyImage(imagePath);
program fails in line:
const imageTensor = tf.node.decodeImage(resizedImageBuffer, 3);
with error:
node app.js test-image.jpg
(node:15224) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/app.js is not specified and it doesn't parse as CommonJS.
Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
To eliminate this warning, add "type": "module" to /Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/package.json.
(Use `node --trace-warnings ...` to show where the warning was created)
Error during image classification: TypeError: (0 , util_1.isNullOrUndefined) is not a function
at createTensorsTypeOpAttr (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-node/dist/nodejs_kernel_backend.js:675:38)
at Object.kernelFunc (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-node/dist/kernels/Cast.js:30:65)
at kernelFunc (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4707:32)
at /Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4767:27
at Engine.scopedRun (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4572:23)
at Engine.runKernelFunc (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4763:14)
at Engine.runKernel (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs-core/dist/tf-core.node.js:4636:21)
at cast_ (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs/dist/tf.node.js:4084:19)
at cast__op (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs/dist/tf.node.js:4027:28)
at getGlobalTensorClass.toInt (/Users/albertmontolio/Documents/coding_area/interviews/koerber/image-recognition/node_modules/@tensorflow/tfjs/dist/tf.node.js:33989:12)
This is my package.json:
{
"name": "image-recognition",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@tensorflow-models/mobilenet": "^2.1.1",
"@tensorflow/tfjs-node": "^4.22.0",
"sharp": "^0.33.5"
}
}
I am using node version 23.3.0.
I've tried many many things without success: check image, change image, install new versions, old versions. I don't know what else to try. If this internal method is broken, what can I do?
I found out that version 4.22.0 of tensorflow is not compatible with node 23 due to util_1.isNullOrUndefined. This util and others were removed in node 23. Solution was to downgrade node to version 22.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With