javascript - 错误:传递给 tf.browser.fromPixels() 的像素必须是 HTMLImageElement
问题描述
尝试使用 TensorflowJS 进行预测。但是,在上面的输入图像中显示错误?我的图像是Uint8Array类型。如何传递 Uint8Array 类型来制作张量?
async predict(imageData: any) {
let img = tf.browser.fromPixels(imageData, 3).resizeBilinear([256, 256]) # problem showing here
img = imageData.reshape([256, 256, 3])
img = tf.cast(img, 'float32')
const segmentation = this.model.predict(img) as any
console.log('success')
}
loadImage(file: FileList) {
this.fileToUpload = file.item(0);
let reader = new FileReader();
reader.readAsDataURL(this.fileToUpload);
reader.onload = (event: any) => {
this.imageUrl = reader.result
this.predict(this.convertDataURIToBinary(this.imageUrl)); # passing Unit8Array image from here
}
}
知道如何克服它吗?谢谢你的建议。
更新
使用 '@ViewChild('ImageRef') ImageRef: ElementRef;' 解决了这个问题 . 最后,我将 Unit8Array 转换为 imageData,然后使用 putImageData 绘制到画布中。
但在另一部分面临问题。当我进行图像分割时,结果与我在 python 中所做的相反。任何想法?
在python中我做了->
img_face = cv2.resize(frame,(256,256))
img_face = cv2.cvtColor(img_face, cv2.COLOR_BGR2RGB)
img_face = img_face / 255.0
img_face = img_face.astype(np.float32)
mask = model.predict(np.expand_dims(img_face , axis=0))[0]
而我当前的 js 部分已经在上面提到了 predict() func。
解决方案
如果imageData
是一个 UInt8Array,它可以很容易地转换为一个张量使用tf.tensor
tf.tensor(imageData)
此外,可以在创建张量时指定图像的宽度和高度
tf.tensor(imageData, [height, width, channels])
现在关于图像处理,在 python 中你正在做
img_face / 255.0
你在 js 中没有做同样的事情。你需要将 js 张量除以 255
推荐阅读
- javascript - 如何在我的反应组件中快进音频?
- google-cloud-platform - 向 cloud-speech-to-text api 添加说话者注释(比如 id、name),并将其包含在由 cloud-speech-to-text api 生成的输出 json 中
- javascript - CubeTextureLoader() 不工作。或者我做错了什么
- r - 将数据集中的每个单元格除以 R 中缺少单元格的列总和
- javascript - Javascript:如何获取对象或数组的`prototype`或`__proto__`的所有属性
- javascript - 点击提交时如何显示表格并将表单值传递给html和js后端?
- javascript - 如何按月和年对日期数组进行分组
- windows - Powershell 文件随未来和过去时间更新
- python - 在 matplotlib 3d 图上设置轴限制(python)
- java - 带有 JsonTypeInfo 的 Spring Boot Jackson defaultImpl