【问题标题】:Convert from 'OpenCVForUnity.CoreModule.Mat' to 'Numpy.NDarray'从“OpenCVForUnity.CoreModule.Mat”转换为“Numpy.NDarray”
【发布时间】:2020-02-25 05:11:42
【问题描述】:

我正在开发 Unity 3d (2019.2.3f1) 中的瞳孔检测器,该检测器将使用网络摄像头将图像帧流式传输到 Keras 模型中以进行推理。作为其中的一部分,我正在使用商店中的 OpenCVForUnity 资产。我面临的挑战是 model.Predict 方法的参数采用 Numpy.NDarray,因此我需要将 OpenCVForUnity.CoreModule.Mat 对象转换为 Numpy.NDarray。

有人可以帮我解决这个问题吗?经过多次搜索和阅读文档后,我无法做到。

谢谢。

public Numpy.NDarray prediction(Mat image)
{
    var model = Keras.Models.Model.ModelFromJson(jsonAIModel);
    model.LoadWeight(aiModelWeights);
    //need to convert the image object to Numpy.NDarray here
    var result = model.Predict(image);

    return result;
}

【问题讨论】:

标签: c# numpy opencv unity3d


【解决方案1】:

我不知道你是否还需要它。但是对于任何人来说..

这仅限于常规图像。您必须清楚地记住,opencv 读取为 BGR 而不是 RGB。这似乎对我有用。

   Mat ndarray2mat(NDArray a)
    { 
        int[] arr_shape = a.shape;
        int rows = arr_shape[0];
        int cols = arr_shape[1];
        Mat m = new Mat(rows, cols, CvType.CV_8UC3);
        byte[] array = a.ToByteArray();
        m.put(0, 0, array);
        return m;
    }
    NDArray mat2ndarray(Mat m)
    {
        int rows = m.rows();
        int cols = m.cols();
        byte[] arr = new byte[rows*cols*3];
        m.get(0, 0, arr);
        var numpy_array = np.array(arr);
        numpy_array = numpy_array.reshape(new int[] { rows, cols, 3 });
        return numpy_array;
    }

【讨论】:

    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 2011-06-13
    • 2018-11-21
    • 2014-08-11
    • 2020-07-06
    • 1970-01-01
    • 2022-06-27
    • 2019-08-31
    相关资源
    最近更新 更多