{"id":15565402,"url":"https://github.com/zihaomu/opencv_lite","last_synced_at":"2025-09-15T15:31:06.409Z","repository":{"id":178914251,"uuid":"637713459","full_name":"zihaomu/opencv_lite","owner":"zihaomu","description":"Use OpenCV API to run ONNX model by ONNXRuntime.","archived":false,"fork":false,"pushed_at":"2024-06-24T09:09:30.000Z","size":47128,"stargazers_count":20,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-30T21:21:52.728Z","etag":null,"topics":["onnxruntime","opencv"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zihaomu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-08T08:56:14.000Z","updated_at":"2024-12-06T06:10:12.000Z","dependencies_parsed_at":"2024-06-24T10:42:23.863Z","dependency_job_id":null,"html_url":"https://github.com/zihaomu/opencv_lite","commit_stats":null,"previous_names":["zihaomu/opencv_ort"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zihaomu%2Fopencv_lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zihaomu%2Fopencv_lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zihaomu%2Fopencv_lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zihaomu%2Fopencv_lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zihaomu","download_url":"https://codeload.github.com/zihaomu/opencv_lite/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233128206,"owners_count":18629046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["onnxruntime","opencv"],"created_at":"2024-10-02T16:55:54.626Z","updated_at":"2025-01-09T02:47:04.838Z","avatar_url":"https://github.com/zihaomu.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OpenCV-lite \n还在开发中。。。\n\nOpenCV-lite 是一个轻量化版本的opencv，专注dnn模型部署场景。主要的修改包括：\n- 移除一些不太常用的模块：features2d, flann, gapi, ml, objdetect, stitching, video.\n- 保留dnn模块API，直接使用MNN、ONNXRuntime、Tflite和TensorRT去做相应的模型推理。\n\n相关项目：\n\nOpenCV-lite相比于原始OpenCV的优势：裁剪掉多余模块，减少包体；引入模型格式对应的推理引擎，让对应的推理引擎推理对应的模型结构。\n\n\n## \n\n1. The API of opencv is easy to use, but the compatibility with ONNX model is poor.  \n2. ONNXRuntime is very compatible with ONNX, but the API is hard to use and changes all the time.\n\n\u003e The compatibility with ONNX model is poor.\n\nIt's a headache, user always encounter such error:\n```angular2html\n[ERROR:0@0.357] global onnx_importer.cpp:xxxx cv::dnn::xxxx::ONNXImporter::handleNode ...\n```\nIn 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.\nIt is expected that after the release of OpenCV 5.0, things will improve.\n\n**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.**\n\nIn this project, I removed all dnn implementation codes, only kept the dnn's API. \nAnd connected to the C++ API of ONNXRuntime.\n\n### The ONNX op test coverage:\n| Project                                                             | ONNX op coverage (%)                             |\n|---------------------------------------------------------------------|--------------------------------------------------|\n| [OpenCV DNN](https://github.com/opencv/opencv/tree/4.x/modules/dnn) | 30.22%**                                         |\n| OpenCV-ORT                                                          | **91.69%***                                      |\n| [ONNXRuntime](https://github.com/microsoft/onnxruntime)     | [**92.22%**](http://onnx.ai/backend-scoreboard/) |\n\n**: Statistical methods:\n\n([All_test](https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_onnx_conformance.cpp#L33) - [all_denylist](https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_onnx_conformance_layer_filter_opencv_all_denylist.inl.hpp) - [parser_denylist](https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hpp))/[All_test](https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_onnx_conformance.cpp#L33)\n= (867 - 56 - 549)/867 = 30.2%\n\n*: the unsupported test case can be found [here](https://github.com/zihaomu/opencv_ort/blob/main/modules/dnn/test/test_onnx_conformance_denylist.inl.hpp).\n\n## TODO List\n1. Fix some bugs in imgproc.\n2. Add Github Action.\n3. video demo.\n4. Add ORT-CUDA support, and compatible with `net.setPreferableBackend(DNN_BACKEND_CUDA)` API.\n\n# How to install?\n\n### Step1: Download ONNXRuntime binary package and unzip.\nPlease choose it by your platform.\nhttps://github.com/microsoft/onnxruntime/releases\n\nI have tested it with ONNXRuntime version: 1.14.1, and it works well.\n\n### Step2: Set enviroment path\nThe keywork of `ORT_SDK` will be used in the OpenCV compiler.\n```bash\nexport ORT_SDK=/opt/onnxruntime-osx-arm64-1.14.1 # Fix the ORT_SDK path.\n```\n### Step3: Compile OpenCV_ORT from source code.\nThe compilation process is same from original OpenCV project.\nAnd 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.\n\n```bash\ngit clone https://github.com/zihaomu/opencv_ort.git\ncd opencv_ort\nmkdir build \u0026 cd build\ncmake -D ORT_SDK=/opt/onnxruntime-osx-arm64-1.14.1 .. # Fix the ORT_SDK path.\n```\n\n# How to use it?\nThe code is the totally same as original OpenCV DNN.\n```C++\n#include \u003ciostream\u003e\n#include \u003cvector\u003e\n#include \u003copencv2/dnn.hpp\u003e\n#include \u003copencv2/imgproc.hpp\u003e\n#include \u003copencv2/highgui.hpp\u003e\n#include\u003calgorithm\u003e\n\nusing namespace std;\nusing namespace cv;\nusing namespace cv::dnn;\n\nint main()\n{\n    // load input\n    Mat image = imread(\"PATH_TO_image\");\n    Scalar meanValue(0.485, 0.456, 0.406);\n    Scalar stdValue(0.229, 0.224, 0.225);\n\n    Mat blob = blobFromImage(image, 1.0/255.0, Size(224, 224), meanValue, true);\n    blob /= stdValue;\n    \n    Net net = readNetFromONNX(\"PATH_TO_MODEL/resnet50-v1-12.onnx\");\n\n    std::vector\u003cMat\u003e out;\n    net.setInput(blob);\n    net.forward(out);\n    \n    double min=0, max=0;\n    Point minLoc, maxLoc;\n    minMaxLoc(out[0], \u0026min, \u0026max, \u0026minLoc, \u0026maxLoc);\n    cout\u003c\u003c\"class num = \"\u003c\u003cmaxLoc.x\u003c\u003cstd::endl;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzihaomu%2Fopencv_lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzihaomu%2Fopencv_lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzihaomu%2Fopencv_lite/lists"}