首页 > 解决方案 > 在电子中使用来自 tensorflow.js 的posenet

问题描述

我正在尝试在电子应用程序中使用posenet MobileNetV1 网络。我希望能够从文件系统中读取图像(它是 png 还是 jpg 都没有关系),并通过网络运行它。

到目前为止我做了什么:

我正在使用以下模块:

import * as posenet from '@tensorflow-models/posenet';
var imageToBase64 = require('image-to-base64');
var toUint8Array = require('base64-to-uint8array')

并使用以下命令初始化网络:

var net = posenet.load();

为了读取图像,我将其转换为 base64 而不是 Uint8Array,而不是使用它们来创建一个对象{data: bytes, width: width, height: height},该对象符合 ImageData 的定义。

一切都在运行,但百分比结果非常低:

{
  score: 0.002851587634615819,
  keypoints: [
    { score: 0.0007664567674510181, part: 'nose', position: [Object] },
    {
      score: 0.0010295170359313488,
      part: 'leftEye',
      position: [Object]
    },
    {
      score: 0.0006740405224263668,
      part: 'rightEye',
      position: [Object]
    },

请注意,将来我打算构建这个应用程序,所以像这样Canvas的模块并不好,因为它构建得不好。

如果有人能给我一个工作 poc 那就太好了,因为我已经为此工作了很长时间。

标签: javascriptelectrontensorflow.jspose-estimation

解决方案


电子有两个独立的上下文;可以将其视为服务器端上下文,称为主上下文和调用浏览器及其脚本的渲染器上下文。虽然这个问题不够精确,但它试图在电子的主要上下文中执行posenet,可以将其进行比较,就好像一个人试图在nodejs中运行这段代码一样

主渲染器中的posenet

const data = Buffer.from(base64str, 'base64')
const t = tf.node.decodeImage(data)
const net = await posenet.load()
const poses = net.estimateMultiplePoses(t, {
      flipHorizontal: false,
      maxDetections: 2,
      scoreThreshold: 0.6,
      nmsRadius: 20})
  })
  // do whatever with the poses

来自浏览器执行的脚本的posenet

const im = new Image()
im.src = base64str
const net = await posenet.load()
im.onload = async() => {
 const poses = await net.estimateMultiplePoses(im, {
      flipHorizontal: false,
      maxDetections: 2,
      scoreThreshold: 0.6,
      nmsRadius: 20})
  })
  // do whatever with the poses
}

推荐阅读