简体   繁体   English

如何在 javascript 中为 tensorflow 加载本地图像

[英]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);

Link to therepo链接到回购

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.

相关问题 如何使用JS加载本地图像? - How do I load a local image using JS? 如何提取Javascript中本地图像的元数据? - How do I extract the metadata of a local image in Javascript? 如何通过 JavaScript 加载本地图像并转换为 Base64 (我不想加载到浏览器中) - How to load local image by JavaScript and convert into Base64 (I don't want to load into the browser) 如何使用Javascript和XMLHttpRequest加载二进制图像数据? - How do I load binary image data using Javascript and XMLHttpRequest? 如何在图像加载之前但在内容加载之后执行 javascript? - How do I execute javascript before image load, but after content load? 在Javascript中,如何通过html元素触发本地文件加载? - In Javascript, how do I trigger a local file load via an html element? 如何使用JavaScript加载我的本地json文件 - How do i load my local json file just using JavaScript 如何在 img src 中指定图像本地路径以在 react/JavaScript 中加载 - How to specify image local path in img src to load in react/JavaScript 如何从文件选择器加载图像并将其加载到 canvas 以便我可以使用 Javascript 对其进行编辑? - How do I load the image from file chooser and get it to load on the canvas so that i can edit it using Javascript? 在javascript中从本地文件系统加载Tensorflow js模型 - Load Tensorflow js model from local file system in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM