javascript - 在电子中使用来自 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 那就太好了,因为我已经为此工作了很长时间。
解决方案
电子有两个独立的上下文;可以将其视为服务器端上下文,称为主上下文和调用浏览器及其脚本的渲染器上下文。虽然这个问题不够精确,但它试图在电子的主要上下文中执行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
}
推荐阅读
- haskell - 为什么类型类覆盖条件在 Haskell 而不是 PureScript 中失败
- lua - Lua:如何从数组中获取随机元素?
- c# - 在 C#/.Net Core 中使用给定比特率通过 UDP 多播流式传输 MPEG TS 文件
- devops - Prometheus 警报重新标记以应用后备标签
- javascript - 如何在现有 Laravel 项目中将 Guess.js 添加到 Vue.js
- azure - Azure Devops:在 Devops 管道任务中通过 Azure CLI 进行模型注册和打包
- python - 如何获得 DF 的单个值
- python - Python 无法解析嵌套参数
- python - 从列表 np.select 分配条件并创建一个新列(熊猫)
- reactjs - 如何在 AWS AppSync Client React App 中离线时禁用自动网络请求突变?