Error: pixels passed to tf.browser.fromPixels() must be either an HTMLImageElement
Error: pixels passed to tf.browser.fromPixels() must be either an 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.
绘制成 canvas
但在另一部分面临问题。因为我在做图像分割,结果正好和我在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
尝试使用 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.
绘制成 canvas但在另一部分面临问题。因为我在做图像分割,结果正好和我在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