使用Tensorflow.js在服务器端进行Mobilenet和Blazeface分类

我尝试使用tensorflowjs和一个已经构建好的模型,起初我遇到了blazeface的问题,因为它无法在仅显示面部的照片上找到面部,所以我尝试使用mobilenet,结果同样毫无意义。因此我非常确定问题出在我发送的图像格式上。

这是我的代码:

module.exports = {    blazeface: require('@tensorflow-models/blazeface'),    mobilenet: require('@tensorflow-models/mobilenet'),    tf: require("@tensorflow/tfjs-node"),    fs: require("fs"),    sizeOf: require("image-size"),    async structureData(imageLink) {        let data = this.fs.readFileSync(imageLink);//返回一个缓冲区        const dimension = await this.sizeOf(imageLink);        const tensorflowImage = {            data: data,            width: dimension.width,            height: dimension.height        };        console.log(tensorflowImage)        return tensorflowImage;    },    async detectFace(imageLink) {        let image = await this.structureData(imageLink);        const model = await this./*blazeface*/mobilenet.load();        const returnTensors = false;        const predictions = await model.classify(image);        console.log(predictions);    }}

这段代码没有报错,但结果是这样的=>

[  {    className: 'theater curtain, theatre curtain',    probability: 0.03815646469593048  },  {    className: 'web site, website, internet site, site',    probability: 0.0255243219435215  },  { className: 'matchstick', probability: 0.02520526386797428 }]

无论使用哪张照片(这里是一张白色背景下的香蕉),所以我非常确定我需要重建structureData函数,但我不知道该怎么做…我也尝试过使用uint32array

    async structureData(imageLink) {        let data = this.fs.readFileSync(imageLink);        data = new Uint32Array(data);        const dimension = await this.sizeOf(imageLink);        const tensorflowImage = {            data: data,            width: dimension.width,            height: dimension.height        };        console.log(tensorflowImage)        return tensorflowImage;    },

但我得到了这个错误。

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

请记住,我使用的是Node.js,所以我认为我不能(或不确定我能否)使用HTMLImageElement

非常感谢你:)


回答:

通过使用张量、视频或图像元素作为模型的参数,它将能够进行分类。

let data = this.fs.readFileSync(imageLink);tensor = tf.node.decodeImage(data, 3)await model.classify(tensor)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注