[英]How do i load an local image in javascript for tensorflow
I want to load a data.jpg file to be used by tesnorflow mobilenet but if i require the the file normally i get So my question is how would i load an image in my javascript code to be used i have posted all the methods i tried Requiring normally我想加载一个 data.jpg 文件以供 tesnorflow mobilenet 使用,但是如果我通常需要该文件,我会得到所以我的问题是如何在我的 javascript 代码中加载图像以供使用我已经发布了我尝试过的所有方法正常需要
node .
/home/arnav7633/jarvis/data.jpg:1
����
SyntaxError: Invalid or unexpected token
at Module._compile (internal/modules/cjs/loader.js:703:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
at Function.Module._load (internal/modules/cjs/loader.js:555:12)
at Module.require (internal/modules/cjs/loader.js:666:19)
at require (internal/modules/cjs/helpers.js:16:16)
at Object.<anonymous> (/home/arnav7633/jarvis/index.js:2:13)
at Module._compile (internal/modules/cjs/loader.js:759:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
at Module.load (internal/modules/cjs/loader.js:628:32)
```
If i use fs for readstream i get
```
(node:20025) UnhandledPromiseRejectionWarning: 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 ReadStream
If i use an url如果我使用 url
(node:20149) UnhandledPromiseRejectionWarning: 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 String
Here is my code
const mobilenet = require('@tensorflow-models/mobilenet');
const img = 'https://imgur.com/a/IPaQnZ5'
require('@tensorflow/tfjs-node')
const func = async(img) => {
console.log('called')
// Load the model.
const model = await mobilenet.load();
console.log('loaded')
// Classify the image.
const predictions = await model.classify(img);
console.log('Predictions: ');
console.log(predictions);
}
(async() => func(img))()
Here is the code in the docs这是文档中的代码
const mobilenet = require('@tensorflow-models/mobilenet');
const img = document.getElementById('img');
// Load the model.
const model = await mobilenet.load();
// Classify the image.
const predictions = await model.classify(img);
console.log('Predictions: ');
console.log(predictions);
The code of the docs added to the question is for a prediction using the browser.添加到问题中的文档代码用于使用浏览器进行预测。
Here is how to load an image file and use it for prediction with nodeJs以下是如何加载图像文件并将其用于使用nodeJs进行预测
const tfnode = require('@tensorflow/tfjs-node')
const imageBuffer = await fs.readFile(file_path);
const tensor = tfnode.node.decodeImage(imageBuffer)
...
const predictions = await model.classify(tensor);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.