【问题标题】:Error: pixels passed to tf.browser.fromPixels() must be either an HTMLImageElement错误:传递给 tf.browser.fromPixels() 的像素必须是 HTMLImageElement
【发布时间】:2021-07-18 13:43:03
【问题描述】:

尝试使用 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。

【问题讨论】:

    标签: javascript angular tensor tensorflow.js tensorflowjs-converter


    【解决方案1】:

    如果imageData是一个UInt8Array,它可以很容易地用tf.tensor转换成张量

    tf.tensor(imageData)
    

    另外,可以在创建张量时指定图像的宽度和高度

    tf.tensor(imageData, [height, width, channels])
    

    现在关于图像处理,在 python 中你正在做

    img_face / 255.0
    

    你没有在 js 中做同样的事情。您需要将 js 张量除以 255

    【讨论】:

    • 在这种情况下它在使用时显示 >> let img = tf.tensor(imageData, [256, 256, -1]) -> 错误错误:未捕获(承诺):错误:张量必须具有由正整数组成的形状,但形状为 [256,256,-1] 也尝试使用 [256,256, 3] 但发生了另一个错误。
    • 无论如何,我使用 @ViewChild 解决了这个问题(直接使用来自 Html 的图像)。但是在模型预测之后,我有一个图像对象张量(添加了它 EDIT ques 部分)。知道如何将它作为图像显示到 HTML 中吗?
    • 在这种情况下,图像不是您所说的 UInt8Array,而是 HtmlElement。
    • 如果你想把张量渲染成图片,可以使用tf.browser.toPixels
    • Uint8Array(50424) [ 255, 216, 255, 224, 0, 16, 74, 70, 73, 70, ... ] 是我的数组。无论如何,在使用 tf.browser.toPixels 后,它的显示 >> Uncaught (in promise) 错误:toPixels 仅支持 2 级或 3 级张量,得到 4 级。我的张量数组形状是 [1,256,256,1]。如何在这里降低排名以使 4 到 3 ? !谢谢
    猜你喜欢
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    • 2022-11-19
    • 1970-01-01
    相关资源
    最近更新 更多