首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >统一目标检测:梭鱼、MobileNET和网络摄像头

统一目标检测:梭鱼、MobileNET和网络摄像头
EN

Stack Overflow用户
提问于 2021-02-16 15:53:08
回答 1查看 912关注 0票数 1

我正在使用Barracuda和以下预先培训过的模型在Unity中运行以下ONNX模型:以我的摄像头为摄像头的https://github.com/onnx/models/tree/master/vision/classification/mobilenet,并使用以下脚本测试系统:

代码语言:javascript
运行
复制
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;
        }
    }
}

但是当我运行它时,我会发现一个错误,它说:

代码语言:javascript
运行
复制
ArgumentException: Can only specify one unknown dimension

我的猜测是,统一不支持模型,或者由于某种原因,我的输入张量形式是不正确的。。。

任何帮助都是非常感谢的,

K

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-22 13:19:32

解决方案:设置为梭鱼V1.3.0

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66227737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档