【发布时间】: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>[]>
}
}
}
这是因为我试图将对象传递到电子应用程序的“后端”吗?有什么办法可以预防吗?
【问题讨论】:
标签: javascript typescript vue.js electron tensorflow.js