还在开发中。。。
OpenCV-lite 是一个轻量化版本的opencv,专注dnn模型部署场景。主要的修改包括:
- 移除一些不太常用的模块:features2d, flann, gapi, ml, objdetect, stitching, video.
- 保留dnn模块API,直接使用MNN、ONNXRuntime、Tflite和TensorRT去做相应的模型推理。
相关项目:
OpenCV-lite相比于原始OpenCV的优势:裁剪掉多余模块,减少包体;引入模型格式对应的推理引擎,让对应的推理引擎推理对应的模型结构。
- The API of opencv is easy to use, but the compatibility with ONNX model is poor.
- ONNXRuntime is very compatible with ONNX, but the API is hard to use and changes all the time.
The compatibility with ONNX model is poor.
It's a headache, user always encounter such error:
[ERROR:0@0.357] global onnx_importer.cpp:xxxx cv::dnn::xxxx::ONNXImporter::handleNode ...
In view of the fact that OpenCV DNN does not fully support dynamic shape input and has low coverage for ONNX. That means user may either in readNet()
, or in net.forward()
, always get an error.
It is expected that after the release of OpenCV 5.0, things will improve.
If you have a model that needs to be inferred and deployed in a C++ environment, and you encounter errors above, maybe you can try this library.
In this project, I removed all dnn implementation codes, only kept the dnn's API. And connected to the C++ API of ONNXRuntime.
Project | ONNX op coverage (%) |
---|---|
OpenCV DNN | 30.22%** |
OpenCV-ORT | 91.69%* |
ONNXRuntime | 92.22% |
**: Statistical methods:
(All_test - all_denylist - parser_denylist)/All_test = (867 - 56 - 549)/867 = 30.2%
*: the unsupported test case can be found here.
- Fix some bugs in imgproc.
- Add Github Action.
- video demo.
- Add ORT-CUDA support, and compatible with
net.setPreferableBackend(DNN_BACKEND_CUDA)
API.
Please choose it by your platform. https://github.com/microsoft/onnxruntime/releases
I have tested it with ONNXRuntime version: 1.14.1, and it works well.
The keywork of ORT_SDK
will be used in the OpenCV compiler.
export ORT_SDK=/opt/onnxruntime-osx-arm64-1.14.1 # Fix the ORT_SDK path.
The compilation process is same from original OpenCV project.
And only difference is that we need to set the one PATH:ORT
, so that cmake
can find ONNXRuntime lib file
and ONNXRuntime head file
correctly.
git clone https://github.com/zihaomu/opencv_ort.git
cd opencv_ort
mkdir build & cd build
cmake -D ORT_SDK=/opt/onnxruntime-osx-arm64-1.14.1 .. # Fix the ORT_SDK path.
The code is the totally same as original OpenCV DNN.
#include <iostream>
#include <vector>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include<algorithm>
using namespace std;
using namespace cv;
using namespace cv::dnn;
int main()
{
// load input
Mat image = imread("PATH_TO_image");
Scalar meanValue(0.485, 0.456, 0.406);
Scalar stdValue(0.229, 0.224, 0.225);
Mat blob = blobFromImage(image, 1.0/255.0, Size(224, 224), meanValue, true);
blob /= stdValue;
Net net = readNetFromONNX("PATH_TO_MODEL/resnet50-v1-12.onnx");
std::vector<Mat> out;
net.setInput(blob);
net.forward(out);
double min=0, max=0;
Point minLoc, maxLoc;
minMaxLoc(out[0], &min, &max, &minLoc, &maxLoc);
cout<<"class num = "<<maxLoc.x<<std::endl;
}