Skip to content

Commit

Permalink
Added ReadNetFromONNX overload that read model from byte array.
Browse files Browse the repository at this point in the history
  • Loading branch information
emgucv committed May 27, 2024
1 parent 5008379 commit 1669c21
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Emgu.CV.Extern/dnn/dnn_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ cv::dnn::Net* cveReadNetFromONNX(cv::String* onnxFile)
throw_no_dnn();
#endif
}
cv::dnn::Net* cveReadNetFromONNX2(const char* bufferModel, int lenModel)
{
#ifdef HAVE_OPENCV_DNN
cv::dnn::Net net = cv::dnn::readNetFromONNX(bufferModel, lenModel);
return new cv::dnn::Net(net);
#else
throw_no_dnn();
#endif
}

void cveReadTensorFromONNX(cv::String* path, cv::Mat* tensor)
{
#ifdef HAVE_OPENCV_DNN
Expand Down
1 change: 1 addition & 0 deletions Emgu.CV.Extern/dnn/dnn_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ CVAPI(cv::dnn::Net*) cveReadNetFromTFLite(cv::String* model);
CVAPI(cv::dnn::Net*) cveReadNetFromTFLite2(const char* bufferModel, int lenModel);

CVAPI(cv::dnn::Net*) cveReadNetFromONNX(cv::String* onnxFile);
CVAPI(cv::dnn::Net*) cveReadNetFromONNX2(const char* bufferModel, int lenModel);
CVAPI(void) cveReadTensorFromONNX(cv::String* path, cv::Mat* tensor);

CVAPI(cv::dnn::Net*) cveReadNetFromTorch(cv::String* model, bool isBinary, bool evaluate);
Expand Down
25 changes: 25 additions & 0 deletions Emgu.CV/Dnn/DnnInvoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@ public static Net ReadNetFromONNX(String onnxFile)
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
private static extern IntPtr cveReadNetFromONNX(IntPtr onnxFile);

/// <summary>
/// Reads a network model from ONNX in-memory buffer.
/// </summary>
/// <param name="model">Memory address of the first byte of the buffer.</param>
/// <returns>Net object</returns>
public static Net ReadNetFromONNX(byte[] model)
{
GCHandle modelHandle = GCHandle.Alloc(model, GCHandleType.Pinned);

try
{
return new Net(cveReadNetFromONNX2(
modelHandle.AddrOfPinnedObject(),
model.Length));
}
finally
{
modelHandle.Free();

}

}
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
private static extern IntPtr cveReadNetFromONNX2(IntPtr bufferModel, int lenModel);

/// <summary>
/// Creates blob from .pb file.
/// </summary>
Expand Down

0 comments on commit 1669c21

Please sign in to comment.