unity3d - Unity 对象检测:梭子鱼、MobileNET 和网络摄像头
问题描述
我正在尝试在 Unity 中运行以下 ONNX 模型,使用梭子鱼和以下预训练模型: https ://github.com/onnx/models/tree/master/vision/classification/mobilenet 使用我的网络摄像头作为摄像头,并使用以下脚本测试系统:
using UnityEngine;
using UnityEngine.UI;
using Unity.Barracuda;
using System.IO;
using System.Linq;
public class Webcam : MonoBehaviour
{
public NNModel mob_net;
public Model model;
private IWorker worker;
int current_cam_index = 0;
WebCamTexture tex;
public RawImage display;
private bool brain_on = false;
private const int SIZE = 224;
public void start_cam()
{
model = ModelLoader.Load(mob_net);
worker = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model);
if (tex != null)
{
stop_cam();
}
else
{
WebCamDevice device = WebCamTexture.devices[current_cam_index];
tex = new WebCamTexture(device.name);
display.texture = tex;
tex.Play();
}
}
public void stop_cam()
{
display.texture = null;
tex.Stop();
tex = null;
}
void crop__normalize_inference(WebCamTexture src)
{
int x = Mathf.FloorToInt(display.transform.position.x);
int y = Mathf.FloorToInt(display.transform.position.y);
Color[] pix = src.GetPixels(x, y, SIZE, SIZE);
Texture2D dest = new Texture2D(SIZE, SIZE);
dest.SetPixels(pix);
dest.Apply();
float[] floats = new float[224 * 224 * 3];
for (int i = 0; i < pix.Length; ++i)
{
var color = pix[i];
floats[i * 3 + 0] = (color.r - 127) / 127.5f;
floats[i * 3 + 1] = (color.g - 127) / 127.5f;
floats[i * 3 + 2] = (color.b - 127) / 127.5f;
}
Tensor in_tensor = new Tensor(1, 224, 224, 3, floats);
worker.Execute(in_tensor);
Tensor out_tensor = worker.PeekOutput("MobilenetV2/Predictions/Reshape_1");
var max = Mathf.Max(out_tensor.ToReadOnlyArray());
var arr = out_tensor.ToReadOnlyArray();
var index = System.Array.IndexOf(arr, max);
string line = File.ReadLines(@"D:\Unity\WebCam\Cam\Assets\Scenes\mobile_net.txt").Skip(index).Take(1).First();
Debug.Log(line);
in_tensor.Dispose();
out_tensor.Dispose();
worker.Dispose();
}
public void brain()
{
brain_on = !brain_on;
}
private void Update()
{
if (brain_on)
{
crop_and_normalize(tex);
brain_on = false;
}
}
}
但是当我运行它时,我得到一个错误,上面写着:
ArgumentException: Can only specify one unknown dimension
我的猜测是 Unity 不支持该模型,或者由于某种原因我的输入张量形式不正确。. .
任何帮助将不胜感激,
ķ
解决方案
解决方案:设置为梭子鱼V1.3.0
推荐阅读
- c# - 如何从 C# 中的字符串中删除 '\n'
- c++ - 如何获取组件和搜索 csv 文件
- javascript - 如何根据 Angular 中的 .CSV 文件行创建多个数组?
- c - STM32 中 PWM 的预分频器和周期值
- java - 无法访问 Spark 集群管理器以请求执行程序
- jvm - double 值是如何存储在 JVM 中的,JVM 是如何从内存中读出其对应的二进制值并将其转换为十进制值的?
- python - 名称“赢家”未定义
- amazon-web-services - AWS AppSync 是否有限制定义?
- python - 切片数据帧捕获错误“TypeError:元组索引必须是整数或切片,而不是 str”
- angular - Angular/Ionic 应用程序 - 尽管服务器端设置正确,但 CORS 问题