【问题标题】:Object losing it's type after passing to function in preloaded script对象在预加载脚本中传递给函数后丢失其类型
【发布时间】:2022-01-14 03:56:10
【问题描述】:

我有一个小型桌面应用程序,它显示来自相机的视频源,拍摄照片并将其传递到神经网络。

当我拍摄图像时,它的类型是Tensor,正如预期的那样,但是在将它传递给一个负责分类的函数后,它失去了它的类型并变成了一个简单的对象。

结构如下:

CameraSource <video> 元素内显示视频,并调用函数

ImageRecognizer @tensorflow/tfjs-node,并预加载到窗口中

现在代码如下:

CameraSource.vue

async test() {
      let vid: HTMLVideoElement = this.$refs.camera_preview as HTMLVideoElement;
      const img = tf.browser.fromPixels(vid);
      console.log(img); <- here it's a Tensor
      await window.api.test(img);
    }

image_recognition.ts

public async checkForObject(image: tf.Tensor): Promise<tf.Tensor<tf.Rank> | tf.Tensor<tf.Rank>[]> {
        console.log(image); <- here it lose's it's Tensor type, and becomes an object
        let reshaped = image.reshape([-1,720,1280,1]); <- this won't work, since image is not a Tensor
        return this.model.predict(image);
   }

preload.ts

const imageRecognizer = new ImageRecognition(720, 1280, 3);

contextBridge.exposeInMainWorld("api", {
    test: (image: tf.Tensor): Promise<tf.Tensor<tf.Rank> | tf.Tensor<tf.Rank>[]> => {
        return imageRecognizer.checkForObject(image)
    }
});

类型声明

declare global {
    interface Window {
        api: {
            test: (image: tf.Tensor) => Promise<tf.Tensor<tf.Rank> | tf.Tensor<tf.Rank>[]>
        }
    }
}

这是因为我试图将对象传递到电子应用程序的“后端”吗?有什么办法可以预防吗?

Tensor losing it's type

【问题讨论】:

    标签: javascript typescript vue.js electron tensorflow.js


    【解决方案1】:

    好的,我应该更仔细地阅读文档。

    ContextBridge 复制值,因此它只能处理某些类型。要使用复杂类型,例如Tensor3D,建议分别对值进行序列化和反序列化。

    这里说的都是:Electron contextBridge documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-26
      • 2019-11-19
      • 1970-01-01
      • 2015-05-10
      • 2023-03-08
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多