使用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

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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