C# 转换为';OpenCVForUnity.CoreModule.Mat';至';Numpy.NDarray和#x27;

C# 转换为';OpenCVForUnity.CoreModule.Mat';至';Numpy.NDarray和#x27;,c#,numpy,opencv,unity3d,C#,Numpy,Opencv,Unity3d,我正在开发Unity 3d(2019.2.3f1)中的瞳孔检测器,它将使用网络摄像头将图像帧流到Keras模型中进行推断。作为其中的一部分,我正在使用商店中的OpenCVForUnity资产。我面临的挑战是model.Predict方法的arg需要Numpy.NDarray,因此我需要将OpenCVForUnity.CoreModule.Mat对象转换为Numpy.NDarray 有人能帮我弄清楚吗?经过无数次的搜索和阅读文档,我无法理解 多谢各位 public Numpy.NDarray pr

我正在开发Unity 3d(2019.2.3f1)中的瞳孔检测器,它将使用网络摄像头将图像帧流到Keras模型中进行推断。作为其中的一部分,我正在使用商店中的OpenCVForUnity资产。我面临的挑战是model.Predict方法的arg需要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;
}

我不知道你是否还需要它。但不管是谁

这仅限于常规图像。。您必须清楚地记住,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;
    }

从未听说过你提到的任何一种类型。。你能给你正在使用的库添加一些链接吗…?OpenCVForUnity是Enox软件的产品。下面是API的链接:具体来说,这里是OpenCVForUnity网站上OpenCVForUnity.CoreModule.Mat的类引用: