使用 mobilenet 和 blazeface 的 Tensorflow js 服务器端分类
Tensorflow js server side classification with mobilenet and 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);//return a buffer
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
记住我正在使用节点,所以我不能(或者我认为我不能)使用 HTMLimageelement
非常感谢:)
通过使用张量、视频或图像元素作为模型的参数,它将能够进行分类。
let data = this.fs.readFileSync(imageLink);
tensor = tf.node.decodeImage(data, 3)
await model.classify(tensor)
我正在尝试将 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);//return a buffer
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
记住我正在使用节点,所以我不能(或者我认为我不能)使用 HTMLimageelement
非常感谢:)
通过使用张量、视频或图像元素作为模型的参数,它将能够进行分类。
let data = this.fs.readFileSync(imageLink);
tensor = tf.node.decodeImage(data, 3)
await model.classify(tensor)