SavyaSanchi-Sharma commited on
Commit
675da12
·
1 Parent(s): eca0f43

pivoted from onnx to opencv 5

Browse files
Files changed (33) hide show
  1. efficientdet-d0/README.md +4 -7
  2. efficientdet-d0/demo.cpp +12 -29
  3. efficientdet-d0/demo.py +3 -3
  4. efficientdet-d0/example_outputs/output_image.png +2 -2
  5. faster_rcnn_inception_v2_coco_2018_01_28/README.md +7 -9
  6. faster_rcnn_inception_v2_coco_2018_01_28/demo.cpp +10 -27
  7. faster_rcnn_inception_v2_coco_2018_01_28/demo.py +4 -4
  8. faster_rcnn_resnet50_coco_2018_01_28/README.md +7 -9
  9. faster_rcnn_resnet50_coco_2018_01_28/demo.cpp +10 -27
  10. faster_rcnn_resnet50_coco_2018_01_28/demo.py +4 -4
  11. mask_rcnn_inception_v2_coco_2018_01_28/README.md +7 -9
  12. mask_rcnn_inception_v2_coco_2018_01_28/demo.cpp +10 -27
  13. mask_rcnn_inception_v2_coco_2018_01_28/demo.py +5 -5
  14. opencv_face_detector_uint8/README.md +5 -8
  15. opencv_face_detector_uint8/demo.cpp +10 -29
  16. opencv_face_detector_uint8/demo.py +4 -4
  17. ssd_inception_v2_coco_2017_11_17/README.md +6 -8
  18. ssd_inception_v2_coco_2017_11_17/demo.cpp +13 -30
  19. ssd_inception_v2_coco_2017_11_17/demo.py +4 -4
  20. ssd_inception_v2_coco_2017_11_17/example_outputs/output_image.png +2 -2
  21. ssd_mobilenet_v1_coco_2017_11_17/README.md +6 -8
  22. ssd_mobilenet_v1_coco_2017_11_17/demo.cpp +13 -30
  23. ssd_mobilenet_v1_coco_2017_11_17/demo.py +4 -4
  24. ssd_mobilenet_v1_coco_2017_11_17/example_outputs/output_image.png +2 -2
  25. ssd_mobilenet_v1_ppn_coco/README.md +6 -8
  26. ssd_mobilenet_v1_ppn_coco/demo.cpp +13 -30
  27. ssd_mobilenet_v1_ppn_coco/demo.py +4 -4
  28. ssd_mobilenet_v2_coco_2018_03_29/README.md +6 -8
  29. ssd_mobilenet_v2_coco_2018_03_29/demo.cpp +13 -30
  30. ssd_mobilenet_v2_coco_2018_03_29/demo.py +4 -4
  31. tensorflow_inception_graph/README.md +13 -2
  32. tensorflow_inception_graph/demo.cpp +22 -23
  33. tensorflow_inception_graph/demo.py +3 -3
efficientdet-d0/README.md CHANGED
@@ -33,22 +33,19 @@ net = cv2.dnn.readNet("efficientdet-d0_2026jul.onnx")
33
  ```
34
 
35
  ### C++
36
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
37
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
38
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
39
  ```bash
40
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
41
  OCV=/path/to/opencv # OpenCV source tree
42
  OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
43
  g++ -std=c++17 demo.cpp -o demo \
44
- -I$ORT/include \
45
  -I$OCV/include \
46
  -I$OCV/modules/core/include \
 
47
  -I$OCV/modules/imgproc/include \
48
  -I$OCV/modules/imgcodecs/include \
49
  -I$OCVBUILD \
50
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
51
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
52
  ./demo --model efficientdet-d0_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
53
  ```
54
 
 
33
  ```
34
 
35
  ### C++
36
+ The C++ demo runs inference with OpenCV's DNN module (default engine no ONNX Runtime
37
+ needed). Adjust the OpenCV paths to your setup:
 
38
  ```bash
 
39
  OCV=/path/to/opencv # OpenCV source tree
40
  OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
41
  g++ -std=c++17 demo.cpp -o demo \
 
42
  -I$OCV/include \
43
  -I$OCV/modules/core/include \
44
+ -I$OCV/modules/dnn/include \
45
  -I$OCV/modules/imgproc/include \
46
  -I$OCV/modules/imgcodecs/include \
47
  -I$OCVBUILD \
48
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
49
  ./demo --model efficientdet-d0_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
50
  ```
51
 
efficientdet-d0/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <algorithm>
@@ -36,40 +36,23 @@ int main(int argc, char** argv)
36
  resize(rgb, rgb, Size(sz, sz));
37
  if (!rgb.isContinuous()) rgb = rgb.clone();
38
 
39
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
40
- Ort::SessionOptions so;
41
- Ort::Session session(env, model.c_str(), so);
42
- Ort::AllocatorWithDefaultOptions alloc;
43
-
44
- auto in_name = session.GetInputNameAllocated(0, alloc);
45
- const char* in_names[] = {in_name.get()};
46
-
47
- size_t nout = session.GetOutputCount();
48
- std::vector<Ort::AllocatedStringPtr> out_holders;
49
- std::vector<std::string> out_str;
50
- for (size_t i = 0; i < nout; ++i)
51
- {
52
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
53
- out_str.push_back(out_holders.back().get());
54
- }
55
- std::vector<const char*> out_names;
56
- for (auto& s : out_str) out_names.push_back(s.c_str());
57
-
58
- std::array<int64_t, 4> shape = {1, sz, sz, 3};
59
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
60
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, (size_t)sz * sz * 3, shape.data(), shape.size());
61
-
62
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
63
 
64
  const float* boxp = nullptr;
65
  const float* clsp = nullptr;
66
  int n = 0, nc = 0;
67
  for (size_t i = 0; i < outs.size(); ++i)
68
  {
69
- auto os = outs[i].GetTensorTypeAndShapeInfo().GetShape();
70
- const float* p = outs[i].GetTensorMutableData<float>();
71
- if (os.back() == 4) { boxp = p; n = (int)os[os.size() - 2]; }
72
- else { clsp = p; nc = (int)os.back(); }
 
73
  }
74
 
75
  std::vector<std::array<float, 2>> baseWH;
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <algorithm>
 
36
  resize(rgb, rgb, Size(sz, sz));
37
  if (!rgb.isContinuous()) rgb = rgb.clone();
38
 
39
+ int blobShape[] = {1, sz, sz, 3};
40
+ Mat blob(4, blobShape, CV_8U, rgb.data);
41
+ dnn::Net net = dnn::readNetFromONNX(model);
42
+ net.setInput(blob);
43
+ std::vector<Mat> outs;
44
+ net.forward(outs, net.getUnconnectedOutLayersNames());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  const float* boxp = nullptr;
47
  const float* clsp = nullptr;
48
  int n = 0, nc = 0;
49
  for (size_t i = 0; i < outs.size(); ++i)
50
  {
51
+ const Mat& o = outs[i];
52
+ const float* p = (const float*)o.data;
53
+ int last = o.size[o.dims - 1];
54
+ if (last == 4) { boxp = p; n = o.size[o.dims - 2]; }
55
+ else { clsp = p; nc = last; }
56
  }
57
 
58
  std::vector<std::array<float, 2>> baseWH;
efficientdet-d0/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
  sz = 512
@@ -53,9 +52,10 @@ def main():
53
  anchors = build_anchors()
54
  acx, acy, aw, ah = anchors[:, 0], anchors[:, 1], anchors[:, 2], anchors[:, 3]
55
 
56
- sess = ort.InferenceSession(model, providers=["CPUExecutionProvider"])
57
  inp = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (sz, sz))
58
- res = sess.run(None, {sess.get_inputs()[0].name: inp[None].astype(np.uint8)})
 
59
  box = next(a for a in res if a.shape[-1] == 4).reshape(-1, 4)
60
  cls = next(a for a in res if a.shape[-1] != 4).reshape(box.shape[0], -1)
61
 
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
  sz = 512
 
52
  anchors = build_anchors()
53
  acx, acy, aw, ah = anchors[:, 0], anchors[:, 1], anchors[:, 2], anchors[:, 3]
54
 
55
+ net = cv.dnn.readNetFromONNX(model)
56
  inp = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (sz, sz))
57
+ net.setInput(inp[None].astype(np.uint8))
58
+ res = net.forward(net.getUnconnectedOutLayersNames())
59
  box = next(a for a in res if a.shape[-1] == 4).reshape(-1, 4)
60
  cls = next(a for a in res if a.shape[-1] != 4).reshape(box.shape[0], -1)
61
 
efficientdet-d0/example_outputs/output_image.png CHANGED

Git LFS Details

  • SHA256: 1b74311a77ed6ca8a39a207f782e59d8b79f6b09b474da53e4e8fb7339c3e723
  • Pointer size: 131 Bytes
  • Size of remote file: 331 kB

Git LFS Details

  • SHA256: 6edda1b62da08b28f04a227bc0054152b76b4dcbc04a50f856e6912fd8ea9b3a
  • Pointer size: 131 Bytes
  • Size of remote file: 331 kB
faster_rcnn_inception_v2_coco_2018_01_28/README.md CHANGED
@@ -3,7 +3,7 @@
3
  Object detection with the Faster-RCNN meta-architecture and an Inception v2 backbone,
4
  trained on the COCO dataset. The model was originally distributed as a frozen TensorFlow
5
  graph (`faster_rcnn_inception_v2_coco_2018_01_28.pb`) from the TensorFlow Object Detection
6
- API and converted to ONNX for inference with ONNX Runtime.
7
 
8
  ## Model Details
9
  - **Architecture**: Faster-RCNN with an Inception v2 backbone
@@ -20,22 +20,20 @@ python demo.py --model faster_rcnn_inception_v2_coco_2018_01_28_2026jul.onnx --i
20
  ```
21
 
22
  ### C++
23
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
24
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
25
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
26
  ```bash
27
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
28
  OCV=/path/to/opencv # OpenCV source tree
29
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
30
  g++ -std=c++17 demo.cpp -o demo \
31
- -I$ORT/include \
32
  -I$OCV/include \
33
  -I$OCV/modules/core/include \
 
34
  -I$OCV/modules/imgproc/include \
35
  -I$OCV/modules/imgcodecs/include \
36
  -I$OCVBUILD \
37
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
38
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
39
  ./demo --model faster_rcnn_inception_v2_coco_2018_01_28_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
40
  ```
41
 
 
3
  Object detection with the Faster-RCNN meta-architecture and an Inception v2 backbone,
4
  trained on the COCO dataset. The model was originally distributed as a frozen TensorFlow
5
  graph (`faster_rcnn_inception_v2_coco_2018_01_28.pb`) from the TensorFlow Object Detection
6
+ API and converted to ONNX for inference with OpenCV's DNN module (ONNX Runtime engine).
7
 
8
  ## Model Details
9
  - **Architecture**: Faster-RCNN with an Inception v2 backbone
 
20
  ```
21
 
22
  ### C++
23
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
24
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
25
+ paths to your setup:
26
  ```bash
 
27
  OCV=/path/to/opencv # OpenCV source tree
28
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
29
  g++ -std=c++17 demo.cpp -o demo \
 
30
  -I$OCV/include \
31
  -I$OCV/modules/core/include \
32
+ -I$OCV/modules/dnn/include \
33
  -I$OCV/modules/imgproc/include \
34
  -I$OCV/modules/imgcodecs/include \
35
  -I$OCVBUILD \
36
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
37
  ./demo --model faster_rcnn_inception_v2_coco_2018_01_28_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
38
  ```
39
 
faster_rcnn_inception_v2_coco_2018_01_28/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
@@ -34,38 +34,21 @@ int main(int argc, char** argv)
34
  cv::resize(rgb, rgb, cv::Size(W, H));
35
  if (!rgb.isContinuous()) rgb = rgb.clone();
36
 
37
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
38
- Ort::SessionOptions so;
39
- Ort::Session session(env, model.c_str(), so);
40
- Ort::AllocatorWithDefaultOptions alloc;
41
-
42
- auto in_name = session.GetInputNameAllocated(0, alloc);
43
- const char* in_names[] = {in_name.get()};
44
-
45
- size_t nout = session.GetOutputCount();
46
- std::vector<Ort::AllocatedStringPtr> out_holders;
47
- std::vector<std::string> out_strs;
48
- for (size_t i = 0; i < nout; ++i)
49
- {
50
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
51
- out_strs.push_back(out_holders.back().get());
52
- }
53
- std::vector<const char*> out_names;
54
- for (auto& s : out_strs) out_names.push_back(s.c_str());
55
-
56
- std::array<int64_t, 4> shape = {1, H, W, 3};
57
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
58
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, (size_t)H * W * 3, shape.data(), shape.size());
59
-
60
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
61
 
62
  float* boxes = nullptr;
63
  float* scores = nullptr;
64
  float* classes = nullptr;
65
  float* numd = nullptr;
66
- for (size_t i = 0; i < nout; ++i)
67
  {
68
- float* p = outs[i].GetTensorMutableData<float>();
69
  const std::string& n = out_strs[i];
70
  if (n.find("detection_boxes") != std::string::npos) boxes = p;
71
  else if (n.find("detection_scores") != std::string::npos) scores = p;
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
 
34
  cv::resize(rgb, rgb, cv::Size(W, H));
35
  if (!rgb.isContinuous()) rgb = rgb.clone();
36
 
37
+ int blobShape[] = {1, H, W, 3};
38
+ cv::Mat blob(4, blobShape, CV_8U, rgb.data);
39
+ cv::dnn::Net net = cv::dnn::readNetFromONNX(model, cv::dnn::ENGINE_ORT);
40
+ net.setInput(blob);
41
+ std::vector<cv::String> out_strs = {"detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"};
42
+ std::vector<cv::Mat> outs;
43
+ net.forward(outs, out_strs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  float* boxes = nullptr;
46
  float* scores = nullptr;
47
  float* classes = nullptr;
48
  float* numd = nullptr;
49
+ for (size_t i = 0; i < out_strs.size(); ++i)
50
  {
51
+ float* p = (float*)outs[i].data;
52
  const std::string& n = out_strs[i];
53
  if (n.find("detection_boxes") != std::string::npos) boxes = p;
54
  else if (n.find("detection_scores") != std::string::npos) scores = p;
faster_rcnn_inception_v2_coco_2018_01_28/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -23,9 +22,10 @@ def main():
23
  raise SystemExit("could not read image: %s" % args.image)
24
 
25
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (800, 600))
26
- sess = ort.InferenceSession(model, providers=["CPUExecutionProvider"])
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
22
  raise SystemExit("could not read image: %s" % args.image)
23
 
24
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (800, 600))
25
+ net = cv.dnn.readNetFromONNX(model, cv.dnn.ENGINE_ORT)
26
+ onames = ["detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
faster_rcnn_resnet50_coco_2018_01_28/README.md CHANGED
@@ -3,7 +3,7 @@
3
  Object detection with the Faster-RCNN meta-architecture and a ResNet-50 backbone,
4
  trained on the COCO dataset. The model was originally distributed as a frozen TensorFlow
5
  graph (`faster_rcnn_resnet50_coco_2018_01_28.pb`) from the TensorFlow Object Detection
6
- API and converted to ONNX for inference with ONNX Runtime.
7
 
8
  ## Model Details
9
  - **Architecture**: Faster-RCNN with a ResNet-50 backbone
@@ -20,22 +20,20 @@ python demo.py --model faster_rcnn_resnet50_coco_2018_01_28_2026jul.onnx --image
20
  ```
21
 
22
  ### C++
23
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
24
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
25
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
26
  ```bash
27
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
28
  OCV=/path/to/opencv # OpenCV source tree
29
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
30
  g++ -std=c++17 demo.cpp -o demo \
31
- -I$ORT/include \
32
  -I$OCV/include \
33
  -I$OCV/modules/core/include \
 
34
  -I$OCV/modules/imgproc/include \
35
  -I$OCV/modules/imgcodecs/include \
36
  -I$OCVBUILD \
37
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
38
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
39
  ./demo --model faster_rcnn_resnet50_coco_2018_01_28_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
40
  ```
41
 
 
3
  Object detection with the Faster-RCNN meta-architecture and a ResNet-50 backbone,
4
  trained on the COCO dataset. The model was originally distributed as a frozen TensorFlow
5
  graph (`faster_rcnn_resnet50_coco_2018_01_28.pb`) from the TensorFlow Object Detection
6
+ API and converted to ONNX for inference with OpenCV's DNN module (ONNX Runtime engine).
7
 
8
  ## Model Details
9
  - **Architecture**: Faster-RCNN with a ResNet-50 backbone
 
20
  ```
21
 
22
  ### C++
23
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
24
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
25
+ paths to your setup:
26
  ```bash
 
27
  OCV=/path/to/opencv # OpenCV source tree
28
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
29
  g++ -std=c++17 demo.cpp -o demo \
 
30
  -I$OCV/include \
31
  -I$OCV/modules/core/include \
32
+ -I$OCV/modules/dnn/include \
33
  -I$OCV/modules/imgproc/include \
34
  -I$OCV/modules/imgcodecs/include \
35
  -I$OCVBUILD \
36
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
37
  ./demo --model faster_rcnn_resnet50_coco_2018_01_28_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
38
  ```
39
 
faster_rcnn_resnet50_coco_2018_01_28/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
@@ -34,38 +34,21 @@ int main(int argc, char** argv)
34
  cv::resize(rgb, rgb, cv::Size(W, H));
35
  if (!rgb.isContinuous()) rgb = rgb.clone();
36
 
37
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
38
- Ort::SessionOptions so;
39
- Ort::Session session(env, model.c_str(), so);
40
- Ort::AllocatorWithDefaultOptions alloc;
41
-
42
- auto in_name = session.GetInputNameAllocated(0, alloc);
43
- const char* in_names[] = {in_name.get()};
44
-
45
- size_t nout = session.GetOutputCount();
46
- std::vector<Ort::AllocatedStringPtr> out_holders;
47
- std::vector<std::string> out_strs;
48
- for (size_t i = 0; i < nout; ++i)
49
- {
50
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
51
- out_strs.push_back(out_holders.back().get());
52
- }
53
- std::vector<const char*> out_names;
54
- for (auto& s : out_strs) out_names.push_back(s.c_str());
55
-
56
- std::array<int64_t, 4> shape = {1, H, W, 3};
57
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
58
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, (size_t)H * W * 3, shape.data(), shape.size());
59
-
60
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
61
 
62
  float* boxes = nullptr;
63
  float* scores = nullptr;
64
  float* classes = nullptr;
65
  float* numd = nullptr;
66
- for (size_t i = 0; i < nout; ++i)
67
  {
68
- float* p = outs[i].GetTensorMutableData<float>();
69
  const std::string& n = out_strs[i];
70
  if (n.find("detection_boxes") != std::string::npos) boxes = p;
71
  else if (n.find("detection_scores") != std::string::npos) scores = p;
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
 
34
  cv::resize(rgb, rgb, cv::Size(W, H));
35
  if (!rgb.isContinuous()) rgb = rgb.clone();
36
 
37
+ int blobShape[] = {1, H, W, 3};
38
+ cv::Mat blob(4, blobShape, CV_8U, rgb.data);
39
+ cv::dnn::Net net = cv::dnn::readNetFromONNX(model, cv::dnn::ENGINE_ORT);
40
+ net.setInput(blob);
41
+ std::vector<cv::String> out_strs = {"detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"};
42
+ std::vector<cv::Mat> outs;
43
+ net.forward(outs, out_strs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  float* boxes = nullptr;
46
  float* scores = nullptr;
47
  float* classes = nullptr;
48
  float* numd = nullptr;
49
+ for (size_t i = 0; i < out_strs.size(); ++i)
50
  {
51
+ float* p = (float*)outs[i].data;
52
  const std::string& n = out_strs[i];
53
  if (n.find("detection_boxes") != std::string::npos) boxes = p;
54
  else if (n.find("detection_scores") != std::string::npos) scores = p;
faster_rcnn_resnet50_coco_2018_01_28/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -23,9 +22,10 @@ def main():
23
  raise SystemExit("could not read image: %s" % args.image)
24
 
25
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (800, 600))
26
- sess = ort.InferenceSession(model, providers=["CPUExecutionProvider"])
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
22
  raise SystemExit("could not read image: %s" % args.image)
23
 
24
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (800, 600))
25
+ net = cv.dnn.readNetFromONNX(model, cv.dnn.ENGINE_ORT)
26
+ onames = ["detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
mask_rcnn_inception_v2_coco_2018_01_28/README.md CHANGED
@@ -3,7 +3,7 @@
3
  Instance segmentation with the Mask-RCNN Inception v2 network trained on the COCO dataset.
4
  The model was originally distributed as a frozen TensorFlow graph
5
  (`mask_rcnn_inception_v2_coco_2018_01_28.pb`) from the TensorFlow Object Detection API
6
- and converted to ONNX for use with ONNX Runtime.
7
 
8
  ## Model Details
9
  - **Architecture**: Mask-RCNN with an Inception v2 backbone
@@ -22,22 +22,20 @@ python demo.py --model mask_rcnn_inception_v2_coco_2018_01_28_2026jul.onnx --ima
22
  ```
23
 
24
  ### C++
25
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
26
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
27
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
28
  ```bash
29
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
30
  OCV=/path/to/opencv # OpenCV source tree
31
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
32
  g++ -std=c++17 demo.cpp -o demo \
33
- -I$ORT/include \
34
  -I$OCV/include \
35
  -I$OCV/modules/core/include \
 
36
  -I$OCV/modules/imgproc/include \
37
  -I$OCV/modules/imgcodecs/include \
38
  -I$OCVBUILD \
39
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
40
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
41
  ./demo --model mask_rcnn_inception_v2_coco_2018_01_28_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
42
  ```
43
 
 
3
  Instance segmentation with the Mask-RCNN Inception v2 network trained on the COCO dataset.
4
  The model was originally distributed as a frozen TensorFlow graph
5
  (`mask_rcnn_inception_v2_coco_2018_01_28.pb`) from the TensorFlow Object Detection API
6
+ and converted to ONNX for use with OpenCV's DNN module (ONNX Runtime engine).
7
 
8
  ## Model Details
9
  - **Architecture**: Mask-RCNN with an Inception v2 backbone
 
22
  ```
23
 
24
  ### C++
25
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
26
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
27
+ paths to your setup:
28
  ```bash
 
29
  OCV=/path/to/opencv # OpenCV source tree
30
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
31
  g++ -std=c++17 demo.cpp -o demo \
 
32
  -I$OCV/include \
33
  -I$OCV/modules/core/include \
34
+ -I$OCV/modules/dnn/include \
35
  -I$OCV/modules/imgproc/include \
36
  -I$OCV/modules/imgcodecs/include \
37
  -I$OCVBUILD \
38
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
39
  ./demo --model mask_rcnn_inception_v2_coco_2018_01_28_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
40
  ```
41
 
mask_rcnn_inception_v2_coco_2018_01_28/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <algorithm>
@@ -35,39 +35,22 @@ int main(int argc, char** argv)
35
  cv::resize(rgb, rgb, cv::Size(W, H));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
39
- Ort::SessionOptions so;
40
- Ort::Session session(env, model.c_str(), so);
41
- Ort::AllocatorWithDefaultOptions alloc;
42
-
43
- auto in_name = session.GetInputNameAllocated(0, alloc);
44
- const char* in_names[] = {in_name.get()};
45
-
46
- size_t nout = session.GetOutputCount();
47
- std::vector<Ort::AllocatedStringPtr> out_holders;
48
- std::vector<std::string> out_strs;
49
- for (size_t i = 0; i < nout; ++i)
50
- {
51
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
52
- out_strs.push_back(out_holders.back().get());
53
- }
54
- std::vector<const char*> out_names;
55
- for (auto& s : out_strs) out_names.push_back(s.c_str());
56
-
57
- std::array<int64_t, 4> shape = {1, H, W, 3};
58
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
59
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, (size_t)H * W * 3, shape.data(), shape.size());
60
-
61
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
62
 
63
  float* boxes = nullptr;
64
  float* scores = nullptr;
65
  float* classes = nullptr;
66
  float* numd = nullptr;
67
  float* masks = nullptr;
68
- for (size_t i = 0; i < nout; ++i)
69
  {
70
- float* p = outs[i].GetTensorMutableData<float>();
71
  const std::string& n = out_strs[i];
72
  if (n.find("detection_boxes") != std::string::npos) boxes = p;
73
  else if (n.find("detection_scores") != std::string::npos) scores = p;
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <algorithm>
 
35
  cv::resize(rgb, rgb, cv::Size(W, H));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
+ int blobShape[] = {1, H, W, 3};
39
+ cv::Mat blob(4, blobShape, CV_8U, rgb.data);
40
+ cv::dnn::Net net = cv::dnn::readNetFromONNX(model, cv::dnn::ENGINE_ORT);
41
+ net.setInput(blob);
42
+ std::vector<cv::String> out_strs = {"num_detections:0", "detection_boxes:0", "detection_scores:0", "detection_classes:0", "detection_masks:0"};
43
+ std::vector<cv::Mat> outs;
44
+ net.forward(outs, out_strs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  float* boxes = nullptr;
47
  float* scores = nullptr;
48
  float* classes = nullptr;
49
  float* numd = nullptr;
50
  float* masks = nullptr;
51
+ for (size_t i = 0; i < out_strs.size(); ++i)
52
  {
53
+ float* p = (float*)outs[i].data;
54
  const std::string& n = out_strs[i];
55
  if (n.find("detection_boxes") != std::string::npos) boxes = p;
56
  else if (n.find("detection_scores") != std::string::npos) scores = p;
mask_rcnn_inception_v2_coco_2018_01_28/demo.py CHANGED
@@ -4,13 +4,12 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
11
 
12
  def main():
13
- parser = argparse.ArgumentParser(description="Mask-RCNN Inception v2 COCO (ONNX Runtime) detection + mask demo")
14
  found = glob.glob(os.path.join(here, "*.onnx"))
15
  parser.add_argument("--model", default=found[0] if found else None)
16
  parser.add_argument("--image", default=os.path.join(here, "example_outputs", "input_image.png"))
@@ -22,10 +21,11 @@ def main():
22
  if img is None:
23
  raise SystemExit("could not read image: %s" % args.image)
24
 
25
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
26
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (800, 800))
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
10
 
11
  def main():
12
+ parser = argparse.ArgumentParser(description="Mask-RCNN Inception v2 COCO (OpenCV DNN) detection + mask demo")
13
  found = glob.glob(os.path.join(here, "*.onnx"))
14
  parser.add_argument("--model", default=found[0] if found else None)
15
  parser.add_argument("--image", default=os.path.join(here, "example_outputs", "input_image.png"))
 
21
  if img is None:
22
  raise SystemExit("could not read image: %s" % args.image)
23
 
24
+ net = cv.dnn.readNetFromONNX(args.model, cv.dnn.ENGINE_ORT)
25
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (800, 800))
26
+ onames = ["num_detections:0", "detection_boxes:0", "detection_scores:0", "detection_classes:0", "detection_masks:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
opencv_face_detector_uint8/README.md CHANGED
@@ -2,7 +2,7 @@
2
 
3
  Single-shot face detection with the OpenCV SSD ResNet-10 network. The model ships in the
4
  OpenCV project as a quantized frozen TensorFlow graph (`opencv_face_detector_uint8.pb`) and is
5
- converted here to ONNX for use with OpenCV's DNN module and onnxruntime. Only the backbone is
6
  exported — PriorBox generation, the confidence softmax, variance decode, score threshold and NMS
7
  are run in host code (see `demo.py` / `demo.cpp`).
8
 
@@ -36,22 +36,19 @@ net = cv2.dnn.readNet("opencv_face_detector_uint8_2026jul.onnx")
36
  ```
37
 
38
  ### C++
39
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
40
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
41
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
42
  ```bash
43
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
44
  OCV=/path/to/opencv # OpenCV source tree
45
  OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
46
  g++ -std=c++17 demo.cpp -o demo \
47
- -I$ORT/include \
48
  -I$OCV/include \
49
  -I$OCV/modules/core/include \
 
50
  -I$OCV/modules/imgproc/include \
51
  -I$OCV/modules/imgcodecs/include \
52
  -I$OCVBUILD \
53
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
54
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
55
  ./demo --model opencv_face_detector_uint8_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
56
  ```
57
 
 
2
 
3
  Single-shot face detection with the OpenCV SSD ResNet-10 network. The model ships in the
4
  OpenCV project as a quantized frozen TensorFlow graph (`opencv_face_detector_uint8.pb`) and is
5
+ converted here to ONNX for use with OpenCV's DNN module. Only the backbone is
6
  exported — PriorBox generation, the confidence softmax, variance decode, score threshold and NMS
7
  are run in host code (see `demo.py` / `demo.cpp`).
8
 
 
36
  ```
37
 
38
  ### C++
39
+ The C++ demo runs inference with OpenCV's DNN module (default engine no ONNX Runtime
40
+ needed). Adjust the OpenCV paths to your setup:
 
41
  ```bash
 
42
  OCV=/path/to/opencv # OpenCV source tree
43
  OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
44
  g++ -std=c++17 demo.cpp -o demo \
 
45
  -I$OCV/include \
46
  -I$OCV/modules/core/include \
47
+ -I$OCV/modules/dnn/include \
48
  -I$OCV/modules/imgproc/include \
49
  -I$OCV/modules/imgcodecs/include \
50
  -I$OCVBUILD \
51
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
52
  ./demo --model opencv_face_detector_uint8_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
53
  ```
54
 
opencv_face_detector_uint8/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <algorithm>
@@ -40,39 +40,20 @@ int main(int argc, char** argv)
40
  subtract(inp, Scalar(104, 177, 123), inp);
41
  if (!inp.isContinuous()) inp = inp.clone();
42
 
43
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
44
- Ort::SessionOptions so;
45
- Ort::Session session(env, model.c_str(), so);
46
- Ort::AllocatorWithDefaultOptions alloc;
47
-
48
- auto in_name = session.GetInputNameAllocated(0, alloc);
49
- const char* in_names[] = {in_name.get()};
50
-
51
- size_t nout = session.GetOutputCount();
52
- std::vector<Ort::AllocatedStringPtr> out_holders;
53
- std::vector<std::string> out_str;
54
- for (size_t i = 0; i < nout; ++i)
55
- {
56
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
57
- out_str.push_back(out_holders.back().get());
58
- }
59
- std::vector<const char*> out_names;
60
- for (auto& s : out_str) out_names.push_back(s.c_str());
61
-
62
- std::array<int64_t, 4> shape = {1, sz, sz, 3};
63
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
64
- Ort::Value input = Ort::Value::CreateTensor<float>(mem, (float*)inp.data, (size_t)sz * sz * 3, shape.data(), shape.size());
65
-
66
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
67
 
68
  const float* loc = nullptr;
69
  const float* conf = nullptr;
70
  for (size_t i = 0; i < outs.size(); ++i)
71
  {
72
- auto os = outs[i].GetTensorTypeAndShapeInfo().GetShape();
73
- size_t tot = 1;
74
- for (auto d : os) tot *= (size_t)d;
75
- const float* p = outs[i].GetTensorMutableData<float>();
76
  if (tot == 35568) loc = p;
77
  else if (tot == 17784) conf = p;
78
  }
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <algorithm>
 
40
  subtract(inp, Scalar(104, 177, 123), inp);
41
  if (!inp.isContinuous()) inp = inp.clone();
42
 
43
+ int blobShape[] = {1, sz, sz, 3};
44
+ Mat blob(4, blobShape, CV_32F, inp.data);
45
+ dnn::Net net = dnn::readNetFromONNX(model);
46
+ net.setInput(blob);
47
+ std::vector<Mat> outs;
48
+ net.forward(outs, net.getUnconnectedOutLayersNames());
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  const float* loc = nullptr;
51
  const float* conf = nullptr;
52
  for (size_t i = 0; i < outs.size(); ++i)
53
  {
54
+ const Mat& o = outs[i];
55
+ size_t tot = o.total();
56
+ const float* p = (const float*)o.data;
 
57
  if (tot == 35568) loc = p;
58
  else if (tot == 17784) conf = p;
59
  }
opencv_face_detector_uint8/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -57,9 +56,10 @@ def main():
57
 
58
  inp = cv.resize(img, (sz, sz)).astype(np.float32) - np.array([104.0, 177.0, 123.0], np.float32)
59
 
60
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
61
- res = sess.run(None, {sess.get_inputs()[0].name: inp[None]})
62
- onames = [o.name for o in sess.get_outputs()]
 
63
  loc = res[[i for i, n in enumerate(onames) if "mbox_loc" in n][0]].reshape(-1, 4)
64
  conf = res[[i for i, n in enumerate(onames) if "mbox_conf" in n][0]].reshape(-1, 2)
65
 
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
56
 
57
  inp = cv.resize(img, (sz, sz)).astype(np.float32) - np.array([104.0, 177.0, 123.0], np.float32)
58
 
59
+ net = cv.dnn.readNetFromONNX(args.model)
60
+ onames = net.getUnconnectedOutLayersNames()
61
+ net.setInput(inp[None])
62
+ res = net.forward(onames)
63
  loc = res[[i for i, n in enumerate(onames) if "mbox_loc" in n][0]].reshape(-1, 4)
64
  conf = res[[i for i, n in enumerate(onames) if "mbox_conf" in n][0]].reshape(-1, 2)
65
 
ssd_inception_v2_coco_2017_11_17/README.md CHANGED
@@ -19,22 +19,20 @@ python demo.py --model ssd_inception_v2_coco_2017_11_17_2026jul.onnx --image exa
19
  ```
20
 
21
  ### C++
22
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
23
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
24
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
25
  ```bash
26
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
27
  OCV=/path/to/opencv # OpenCV source tree
28
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
29
  g++ -std=c++17 demo.cpp -o demo \
30
- -I$ORT/include \
31
  -I$OCV/include \
32
  -I$OCV/modules/core/include \
 
33
  -I$OCV/modules/imgproc/include \
34
  -I$OCV/modules/imgcodecs/include \
35
  -I$OCVBUILD \
36
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
37
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
38
  ./demo --model ssd_inception_v2_coco_2017_11_17_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
39
  ```
40
 
 
19
  ```
20
 
21
  ### C++
22
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
23
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
24
+ paths to your setup:
25
  ```bash
 
26
  OCV=/path/to/opencv # OpenCV source tree
27
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
28
  g++ -std=c++17 demo.cpp -o demo \
 
29
  -I$OCV/include \
30
  -I$OCV/modules/core/include \
31
+ -I$OCV/modules/dnn/include \
32
  -I$OCV/modules/imgproc/include \
33
  -I$OCV/modules/imgcodecs/include \
34
  -I$OCVBUILD \
35
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
36
  ./demo --model ssd_inception_v2_coco_2017_11_17_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
37
  ```
38
 
ssd_inception_v2_coco_2017_11_17/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
@@ -35,39 +35,22 @@ int main(int argc, char** argv)
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
39
- Ort::SessionOptions so;
40
- Ort::Session session(env, model.c_str(), so);
41
- Ort::AllocatorWithDefaultOptions alloc;
42
-
43
- auto in_name = session.GetInputNameAllocated(0, alloc);
44
- const char* in_names[] = {in_name.get()};
45
-
46
- size_t out_count = session.GetOutputCount();
47
- std::vector<Ort::AllocatedStringPtr> out_holders;
48
- std::vector<std::string> out_str;
49
- std::vector<const char*> out_names;
50
- for (size_t i = 0; i < out_count; ++i)
51
- {
52
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
53
- out_str.push_back(out_holders.back().get());
54
- out_names.push_back(out_str.back().c_str());
55
- }
56
-
57
- std::array<int64_t, 4> shape = {1, 300, 300, 3};
58
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
59
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, 300 * 300 * 3, shape.data(), shape.size());
60
-
61
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
62
 
63
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
64
- for (size_t i = 0; i < out_count; ++i)
65
  {
66
  const std::string& n = out_str[i];
67
- if (n.find("detection_boxes") != std::string::npos) boxes = outs[i].GetTensorMutableData<float>();
68
- else if (n.find("detection_scores") != std::string::npos) scores = outs[i].GetTensorMutableData<float>();
69
- else if (n.find("detection_classes") != std::string::npos) classes = outs[i].GetTensorMutableData<float>();
70
- else if (n.find("num_detections") != std::string::npos) num = outs[i].GetTensorMutableData<float>();
71
  }
72
  if (!boxes || !scores || !classes || !num)
73
  {
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
 
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
+ int blobShape[] = {1, 300, 300, 3};
39
+ Mat blob(4, blobShape, CV_8U, rgb.data);
40
+ dnn::Net net = dnn::readNetFromONNX(model, dnn::ENGINE_ORT);
41
+ net.setInput(blob);
42
+ std::vector<String> out_str = {"detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"};
43
+ std::vector<Mat> outs;
44
+ net.forward(outs, out_str);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
47
+ for (size_t i = 0; i < out_str.size(); ++i)
48
  {
49
  const std::string& n = out_str[i];
50
+ if (n.find("detection_boxes") != std::string::npos) boxes = (const float*)outs[i].data;
51
+ else if (n.find("detection_scores") != std::string::npos) scores = (const float*)outs[i].data;
52
+ else if (n.find("detection_classes") != std::string::npos) classes = (const float*)outs[i].data;
53
+ else if (n.find("num_detections") != std::string::npos) num = (const float*)outs[i].data;
54
  }
55
  if (!boxes || !scores || !classes || !num)
56
  {
ssd_inception_v2_coco_2017_11_17/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -23,9 +22,10 @@ def main():
23
 
24
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
25
 
26
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
22
 
23
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
24
 
25
+ net = cv.dnn.readNetFromONNX(args.model, cv.dnn.ENGINE_ORT)
26
+ onames = ["detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
ssd_inception_v2_coco_2017_11_17/example_outputs/output_image.png CHANGED

Git LFS Details

  • SHA256: d9f22cff789563af08e4d954034d24a2cd493807becbbebd3ee323880acb0876
  • Pointer size: 131 Bytes
  • Size of remote file: 458 kB

Git LFS Details

  • SHA256: 8051cbe576541917d5a68bbdb6d119c2cf26bdf84aab362b909d8d1a8f9ad6c5
  • Pointer size: 131 Bytes
  • Size of remote file: 458 kB
ssd_mobilenet_v1_coco_2017_11_17/README.md CHANGED
@@ -19,22 +19,20 @@ python demo.py --model ssd_mobilenet_v1_coco_2017_11_17_2026jul.onnx --image exa
19
  ```
20
 
21
  ### C++
22
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
23
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
24
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
25
  ```bash
26
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
27
  OCV=/path/to/opencv # OpenCV source tree
28
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
29
  g++ -std=c++17 demo.cpp -o demo \
30
- -I$ORT/include \
31
  -I$OCV/include \
32
  -I$OCV/modules/core/include \
 
33
  -I$OCV/modules/imgproc/include \
34
  -I$OCV/modules/imgcodecs/include \
35
  -I$OCVBUILD \
36
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
37
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
38
  ./demo --model ssd_mobilenet_v1_coco_2017_11_17_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
39
  ```
40
 
 
19
  ```
20
 
21
  ### C++
22
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
23
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
24
+ paths to your setup:
25
  ```bash
 
26
  OCV=/path/to/opencv # OpenCV source tree
27
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
28
  g++ -std=c++17 demo.cpp -o demo \
 
29
  -I$OCV/include \
30
  -I$OCV/modules/core/include \
31
+ -I$OCV/modules/dnn/include \
32
  -I$OCV/modules/imgproc/include \
33
  -I$OCV/modules/imgcodecs/include \
34
  -I$OCVBUILD \
35
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
36
  ./demo --model ssd_mobilenet_v1_coco_2017_11_17_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
37
  ```
38
 
ssd_mobilenet_v1_coco_2017_11_17/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
@@ -35,39 +35,22 @@ int main(int argc, char** argv)
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
39
- Ort::SessionOptions so;
40
- Ort::Session session(env, model.c_str(), so);
41
- Ort::AllocatorWithDefaultOptions alloc;
42
-
43
- auto in_name = session.GetInputNameAllocated(0, alloc);
44
- const char* in_names[] = {in_name.get()};
45
-
46
- size_t out_count = session.GetOutputCount();
47
- std::vector<Ort::AllocatedStringPtr> out_holders;
48
- std::vector<std::string> out_str;
49
- std::vector<const char*> out_names;
50
- for (size_t i = 0; i < out_count; ++i)
51
- {
52
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
53
- out_str.push_back(out_holders.back().get());
54
- out_names.push_back(out_str.back().c_str());
55
- }
56
-
57
- std::array<int64_t, 4> shape = {1, 300, 300, 3};
58
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
59
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, 300 * 300 * 3, shape.data(), shape.size());
60
-
61
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
62
 
63
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
64
- for (size_t i = 0; i < out_count; ++i)
65
  {
66
  const std::string& n = out_str[i];
67
- if (n.find("detection_boxes") != std::string::npos) boxes = outs[i].GetTensorMutableData<float>();
68
- else if (n.find("detection_scores") != std::string::npos) scores = outs[i].GetTensorMutableData<float>();
69
- else if (n.find("detection_classes") != std::string::npos) classes = outs[i].GetTensorMutableData<float>();
70
- else if (n.find("num_detections") != std::string::npos) num = outs[i].GetTensorMutableData<float>();
71
  }
72
  if (!boxes || !scores || !classes || !num)
73
  {
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
 
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
+ int blobShape[] = {1, 300, 300, 3};
39
+ Mat blob(4, blobShape, CV_8U, rgb.data);
40
+ dnn::Net net = dnn::readNetFromONNX(model, dnn::ENGINE_ORT);
41
+ net.setInput(blob);
42
+ std::vector<String> out_str = {"detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"};
43
+ std::vector<Mat> outs;
44
+ net.forward(outs, out_str);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
47
+ for (size_t i = 0; i < out_str.size(); ++i)
48
  {
49
  const std::string& n = out_str[i];
50
+ if (n.find("detection_boxes") != std::string::npos) boxes = (const float*)outs[i].data;
51
+ else if (n.find("detection_scores") != std::string::npos) scores = (const float*)outs[i].data;
52
+ else if (n.find("detection_classes") != std::string::npos) classes = (const float*)outs[i].data;
53
+ else if (n.find("num_detections") != std::string::npos) num = (const float*)outs[i].data;
54
  }
55
  if (!boxes || !scores || !classes || !num)
56
  {
ssd_mobilenet_v1_coco_2017_11_17/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -23,9 +22,10 @@ def main():
23
 
24
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
25
 
26
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
22
 
23
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
24
 
25
+ net = cv.dnn.readNetFromONNX(args.model, cv.dnn.ENGINE_ORT)
26
+ onames = ["detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
ssd_mobilenet_v1_coco_2017_11_17/example_outputs/output_image.png CHANGED

Git LFS Details

  • SHA256: b0fa4655a0dfaed8af05d75a19e45cfb2444532017094fa6c3e66c900d1b9392
  • Pointer size: 131 Bytes
  • Size of remote file: 329 kB

Git LFS Details

  • SHA256: 953b498fb864bd5a1bf7d9abc47dfb86f20aac0954d45b8b77ae5acf379fe23a
  • Pointer size: 131 Bytes
  • Size of remote file: 329 kB
ssd_mobilenet_v1_ppn_coco/README.md CHANGED
@@ -20,22 +20,20 @@ python demo.py --model ssd_mobilenet_v1_ppn_coco_2026jul.onnx --image example_ou
20
  ```
21
 
22
  ### C++
23
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
24
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
25
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
26
  ```bash
27
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
28
  OCV=/path/to/opencv # OpenCV source tree
29
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
30
  g++ -std=c++17 demo.cpp -o demo \
31
- -I$ORT/include \
32
  -I$OCV/include \
33
  -I$OCV/modules/core/include \
 
34
  -I$OCV/modules/imgproc/include \
35
  -I$OCV/modules/imgcodecs/include \
36
  -I$OCVBUILD \
37
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
38
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
39
  ./demo --model ssd_mobilenet_v1_ppn_coco_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
40
  ```
41
 
 
20
  ```
21
 
22
  ### C++
23
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
24
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
25
+ paths to your setup:
26
  ```bash
 
27
  OCV=/path/to/opencv # OpenCV source tree
28
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
29
  g++ -std=c++17 demo.cpp -o demo \
 
30
  -I$OCV/include \
31
  -I$OCV/modules/core/include \
32
+ -I$OCV/modules/dnn/include \
33
  -I$OCV/modules/imgproc/include \
34
  -I$OCV/modules/imgcodecs/include \
35
  -I$OCVBUILD \
36
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
37
  ./demo --model ssd_mobilenet_v1_ppn_coco_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
38
  ```
39
 
ssd_mobilenet_v1_ppn_coco/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
@@ -35,39 +35,22 @@ int main(int argc, char** argv)
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
39
- Ort::SessionOptions so;
40
- Ort::Session session(env, model.c_str(), so);
41
- Ort::AllocatorWithDefaultOptions alloc;
42
-
43
- auto in_name = session.GetInputNameAllocated(0, alloc);
44
- const char* in_names[] = {in_name.get()};
45
-
46
- size_t out_count = session.GetOutputCount();
47
- std::vector<Ort::AllocatedStringPtr> out_holders;
48
- std::vector<std::string> out_str;
49
- std::vector<const char*> out_names;
50
- for (size_t i = 0; i < out_count; ++i)
51
- {
52
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
53
- out_str.push_back(out_holders.back().get());
54
- out_names.push_back(out_str.back().c_str());
55
- }
56
-
57
- std::array<int64_t, 4> shape = {1, 300, 300, 3};
58
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
59
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, 300 * 300 * 3, shape.data(), shape.size());
60
-
61
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
62
 
63
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
64
- for (size_t i = 0; i < out_count; ++i)
65
  {
66
  const std::string& n = out_str[i];
67
- if (n.find("detection_boxes") != std::string::npos) boxes = outs[i].GetTensorMutableData<float>();
68
- else if (n.find("detection_scores") != std::string::npos) scores = outs[i].GetTensorMutableData<float>();
69
- else if (n.find("detection_classes") != std::string::npos) classes = outs[i].GetTensorMutableData<float>();
70
- else if (n.find("num_detections") != std::string::npos) num = outs[i].GetTensorMutableData<float>();
71
  }
72
  if (!boxes || !scores || !classes || !num)
73
  {
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
 
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
+ int blobShape[] = {1, 300, 300, 3};
39
+ Mat blob(4, blobShape, CV_8U, rgb.data);
40
+ dnn::Net net = dnn::readNetFromONNX(model, dnn::ENGINE_ORT);
41
+ net.setInput(blob);
42
+ std::vector<String> out_str = {"detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"};
43
+ std::vector<Mat> outs;
44
+ net.forward(outs, out_str);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
47
+ for (size_t i = 0; i < out_str.size(); ++i)
48
  {
49
  const std::string& n = out_str[i];
50
+ if (n.find("detection_boxes") != std::string::npos) boxes = (const float*)outs[i].data;
51
+ else if (n.find("detection_scores") != std::string::npos) scores = (const float*)outs[i].data;
52
+ else if (n.find("detection_classes") != std::string::npos) classes = (const float*)outs[i].data;
53
+ else if (n.find("num_detections") != std::string::npos) num = (const float*)outs[i].data;
54
  }
55
  if (!boxes || !scores || !classes || !num)
56
  {
ssd_mobilenet_v1_ppn_coco/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -23,9 +22,10 @@ def main():
23
 
24
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
25
 
26
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
22
 
23
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
24
 
25
+ net = cv.dnn.readNetFromONNX(args.model, cv.dnn.ENGINE_ORT)
26
+ onames = ["detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
ssd_mobilenet_v2_coco_2018_03_29/README.md CHANGED
@@ -19,22 +19,20 @@ python demo.py --model ssd_mobilenet_v2_coco_2018_03_29_2026jul.onnx --image exa
19
  ```
20
 
21
  ### C++
22
- The C++ demo runs inference with ONNX Runtime (C++ API) and uses OpenCV only for image I/O.
23
- Install ONNX Runtime (C++) from https://github.com/microsoft/onnxruntime/releases this build
24
- uses `onnxruntime-linux-x64-1.25.0` — and adjust the ONNX Runtime and OpenCV paths to your setup:
25
  ```bash
26
- ORT=/path/to/onnxruntime-linux-x64-1.25.0 # ONNX Runtime release dir (contains include/ and lib/)
27
  OCV=/path/to/opencv # OpenCV source tree
28
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
29
  g++ -std=c++17 demo.cpp -o demo \
30
- -I$ORT/include \
31
  -I$OCV/include \
32
  -I$OCV/modules/core/include \
 
33
  -I$OCV/modules/imgproc/include \
34
  -I$OCV/modules/imgcodecs/include \
35
  -I$OCVBUILD \
36
- -L$ORT/lib -Wl,-rpath,$ORT/lib -lonnxruntime \
37
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
38
  ./demo --model ssd_mobilenet_v2_coco_2018_03_29_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
39
  ```
40
 
 
19
  ```
20
 
21
  ### C++
22
+ The C++ demo runs inference with OpenCV's DNN module using its ONNX Runtime engine
23
+ (`ENGINE_ORT`), so OpenCV must be built with `-DWITH_ONNXRUNTIME=ON`. Adjust the OpenCV
24
+ paths to your setup:
25
  ```bash
 
26
  OCV=/path/to/opencv # OpenCV source tree
27
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (built with -DWITH_ONNXRUNTIME=ON)
28
  g++ -std=c++17 demo.cpp -o demo \
 
29
  -I$OCV/include \
30
  -I$OCV/modules/core/include \
31
+ -I$OCV/modules/dnn/include \
32
  -I$OCV/modules/imgproc/include \
33
  -I$OCV/modules/imgcodecs/include \
34
  -I$OCVBUILD \
35
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
 
36
  ./demo --model ssd_mobilenet_v2_coco_2018_03_29_2026jul.onnx --image example_outputs/input_image.png --output example_outputs/output_image.png
37
  ```
38
 
ssd_mobilenet_v2_coco_2018_03_29/demo.cpp CHANGED
@@ -1,4 +1,4 @@
1
- #include <onnxruntime_cxx_api.h>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
@@ -35,39 +35,22 @@ int main(int argc, char** argv)
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
- Ort::Env env(ORT_LOGGING_LEVEL_WARNING, "demo");
39
- Ort::SessionOptions so;
40
- Ort::Session session(env, model.c_str(), so);
41
- Ort::AllocatorWithDefaultOptions alloc;
42
-
43
- auto in_name = session.GetInputNameAllocated(0, alloc);
44
- const char* in_names[] = {in_name.get()};
45
-
46
- size_t out_count = session.GetOutputCount();
47
- std::vector<Ort::AllocatedStringPtr> out_holders;
48
- std::vector<std::string> out_str;
49
- std::vector<const char*> out_names;
50
- for (size_t i = 0; i < out_count; ++i)
51
- {
52
- out_holders.push_back(session.GetOutputNameAllocated(i, alloc));
53
- out_str.push_back(out_holders.back().get());
54
- out_names.push_back(out_str.back().c_str());
55
- }
56
-
57
- std::array<int64_t, 4> shape = {1, 300, 300, 3};
58
- auto mem = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
59
- Ort::Value input = Ort::Value::CreateTensor<uint8_t>(mem, rgb.data, 300 * 300 * 3, shape.data(), shape.size());
60
-
61
- auto outs = session.Run(Ort::RunOptions{nullptr}, in_names, &input, 1, out_names.data(), out_names.size());
62
 
63
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
64
- for (size_t i = 0; i < out_count; ++i)
65
  {
66
  const std::string& n = out_str[i];
67
- if (n.find("detection_boxes") != std::string::npos) boxes = outs[i].GetTensorMutableData<float>();
68
- else if (n.find("detection_scores") != std::string::npos) scores = outs[i].GetTensorMutableData<float>();
69
- else if (n.find("detection_classes") != std::string::npos) classes = outs[i].GetTensorMutableData<float>();
70
- else if (n.find("num_detections") != std::string::npos) num = outs[i].GetTensorMutableData<float>();
71
  }
72
  if (!boxes || !scores || !classes || !num)
73
  {
 
1
+ #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
  #include <array>
 
35
  resize(rgb, rgb, Size(300, 300));
36
  if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
+ int blobShape[] = {1, 300, 300, 3};
39
+ Mat blob(4, blobShape, CV_8U, rgb.data);
40
+ dnn::Net net = dnn::readNetFromONNX(model, dnn::ENGINE_ORT);
41
+ net.setInput(blob);
42
+ std::vector<String> out_str = {"detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"};
43
+ std::vector<Mat> outs;
44
+ net.forward(outs, out_str);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  const float *boxes = 0, *scores = 0, *classes = 0, *num = 0;
47
+ for (size_t i = 0; i < out_str.size(); ++i)
48
  {
49
  const std::string& n = out_str[i];
50
+ if (n.find("detection_boxes") != std::string::npos) boxes = (const float*)outs[i].data;
51
+ else if (n.find("detection_scores") != std::string::npos) scores = (const float*)outs[i].data;
52
+ else if (n.find("detection_classes") != std::string::npos) classes = (const float*)outs[i].data;
53
+ else if (n.find("num_detections") != std::string::npos) num = (const float*)outs[i].data;
54
  }
55
  if (!boxes || !scores || !classes || !num)
56
  {
ssd_mobilenet_v2_coco_2018_03_29/demo.py CHANGED
@@ -4,7 +4,6 @@ import os
4
 
5
  import cv2 as cv
6
  import numpy as np
7
- import onnxruntime as ort
8
 
9
  here = os.path.dirname(os.path.abspath(__file__))
10
 
@@ -23,9 +22,10 @@ def main():
23
 
24
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
25
 
26
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
27
- res = sess.run(None, {sess.get_inputs()[0].name: rgb[None].astype(np.uint8)})
28
- onames = [o.name for o in sess.get_outputs()]
 
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
 
4
 
5
  import cv2 as cv
6
  import numpy as np
 
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
 
22
 
23
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (300, 300))
24
 
25
+ net = cv.dnn.readNetFromONNX(args.model, cv.dnn.ENGINE_ORT)
26
+ onames = ["detection_boxes:0", "detection_scores:0", "detection_classes:0", "num_detections:0"]
27
+ net.setInput(rgb[None].astype(np.uint8))
28
+ res = net.forward(onames)
29
  boxes = res[[i for i, n in enumerate(onames) if "detection_boxes" in n][0]].reshape(-1, 4)
30
  scores = res[[i for i, n in enumerate(onames) if "detection_scores" in n][0]].reshape(-1)
31
  classes = res[[i for i, n in enumerate(onames) if "detection_classes" in n][0]].reshape(-1)
tensorflow_inception_graph/README.md CHANGED
@@ -27,9 +27,20 @@ net = cv2.dnn.readNet("tensorflow_inception_graph_2026jul.onnx")
27
  ```
28
 
29
  ### C++
 
 
30
  ```bash
31
- cmake -B build && cmake --build build
32
- ./build/demo --model tensorflow_inception_graph_2026jul.onnx --image example_outputs/input_image.png
 
 
 
 
 
 
 
 
 
33
  ```
34
 
35
  ## Conversion
 
27
  ```
28
 
29
  ### C++
30
+ The C++ demo runs inference with OpenCV's DNN module (default engine — no ONNX Runtime
31
+ needed). Adjust the OpenCV paths to your setup:
32
  ```bash
33
+ OCV=/path/to/opencv # OpenCV source tree
34
+ OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
35
+ g++ -std=c++17 demo.cpp -o demo \
36
+ -I$OCV/include \
37
+ -I$OCV/modules/core/include \
38
+ -I$OCV/modules/dnn/include \
39
+ -I$OCV/modules/imgproc/include \
40
+ -I$OCV/modules/imgcodecs/include \
41
+ -I$OCVBUILD \
42
+ -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
43
+ ./demo --model tensorflow_inception_graph_2026jul.onnx --image example_outputs/input_image.png
44
  ```
45
 
46
  ## Conversion
tensorflow_inception_graph/demo.cpp CHANGED
@@ -1,13 +1,13 @@
1
  #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
 
 
4
  #include <fstream>
5
  #include <iostream>
6
  #include <string>
7
  #include <vector>
8
 
9
- using namespace cv;
10
-
11
  static std::string argVal(int argc, char** argv, const std::string& key, const std::string& def)
12
  {
13
  for (int i = 1; i + 1 < argc; ++i)
@@ -22,45 +22,44 @@ int main(int argc, char** argv)
22
  std::string output = argVal(argc, argv, "--output", "example_outputs/output_image.png");
23
  std::string labels = argVal(argc, argv, "--labels", "");
24
 
25
- Mat img = imread(image);
26
  if (img.empty())
27
  {
28
  std::cerr << "could not read image: " << image << std::endl;
29
  return 1;
30
  }
31
 
32
- Mat rgb;
33
- cvtColor(img, rgb, COLOR_BGR2RGB);
34
- resize(rgb, rgb, Size(224, 224));
35
  rgb.convertTo(rgb, CV_32F);
 
36
 
37
- int dims[] = {1, 224, 224, 3};
38
- Mat blob(4, dims, CV_32F, rgb.data);
39
-
40
- dnn::Net net = dnn::readNet(model);
41
  net.setInput(blob);
42
- Mat scores = net.forward().reshape(1, 1);
 
 
 
 
43
 
44
- Point classId;
45
- double conf;
46
- minMaxLoc(scores, 0, &conf, 0, &classId);
47
-
48
- std::string label = std::to_string(classId.x);
49
  if (!labels.empty())
50
  {
51
  std::ifstream f(labels);
52
  std::vector<std::string> names;
53
  std::string line;
54
  while (std::getline(f, line)) names.push_back(line);
55
- if (classId.x < (int)names.size()) label = names[classId.x];
56
  }
 
57
 
58
- std::cout << "class " << classId.x << " " << label << " confidence " << conf << std::endl;
59
-
60
- Mat out = img.clone();
61
- putText(out, format("%s (%.2f)", label.c_str(), conf), Point(10, 30),
62
- FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 255, 0), 2);
63
- imwrite(output, out);
64
  std::cout << "wrote " << output << std::endl;
65
  return 0;
66
  }
 
1
  #include <opencv2/dnn.hpp>
2
  #include <opencv2/imgproc.hpp>
3
  #include <opencv2/imgcodecs.hpp>
4
+ #include <algorithm>
5
+ #include <array>
6
  #include <fstream>
7
  #include <iostream>
8
  #include <string>
9
  #include <vector>
10
 
 
 
11
  static std::string argVal(int argc, char** argv, const std::string& key, const std::string& def)
12
  {
13
  for (int i = 1; i + 1 < argc; ++i)
 
22
  std::string output = argVal(argc, argv, "--output", "example_outputs/output_image.png");
23
  std::string labels = argVal(argc, argv, "--labels", "");
24
 
25
+ cv::Mat img = cv::imread(image);
26
  if (img.empty())
27
  {
28
  std::cerr << "could not read image: " << image << std::endl;
29
  return 1;
30
  }
31
 
32
+ cv::Mat rgb;
33
+ cv::cvtColor(img, rgb, cv::COLOR_BGR2RGB);
34
+ cv::resize(rgb, rgb, cv::Size(224, 224));
35
  rgb.convertTo(rgb, CV_32F);
36
+ if (!rgb.isContinuous()) rgb = rgb.clone();
37
 
38
+ int blobShape[] = {1, 224, 224, 3};
39
+ cv::Mat blob(4, blobShape, CV_32F, rgb.data);
40
+ cv::dnn::Net net = cv::dnn::readNetFromONNX(model);
 
41
  net.setInput(blob);
42
+ cv::Mat scoresMat = net.forward();
43
+ float* scores = (float*)scoresMat.data;
44
+ int n = (int)scoresMat.total();
45
+ int top = (int)(std::max_element(scores, scores + n) - scores);
46
+ float conf = scores[top];
47
 
48
+ std::string label = std::to_string(top);
 
 
 
 
49
  if (!labels.empty())
50
  {
51
  std::ifstream f(labels);
52
  std::vector<std::string> names;
53
  std::string line;
54
  while (std::getline(f, line)) names.push_back(line);
55
+ if (top < (int)names.size()) label = names[top];
56
  }
57
+ std::cout << "class " << top << " " << label << " confidence " << conf << std::endl;
58
 
59
+ cv::Mat out = img.clone();
60
+ cv::putText(out, cv::format("%s (%.2f)", label.c_str(), conf), cv::Point(10, 30),
61
+ cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 255, 0), 2);
62
+ cv::imwrite(output, out);
 
 
63
  std::cout << "wrote " << output << std::endl;
64
  return 0;
65
  }
tensorflow_inception_graph/demo.py CHANGED
@@ -3,7 +3,6 @@ import os
3
 
4
  import cv2 as cv
5
  import numpy as np
6
- import onnxruntime as ort
7
 
8
  here = os.path.dirname(os.path.abspath(__file__))
9
 
@@ -22,8 +21,9 @@ def main():
22
 
23
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (224, 224)).astype(np.float32)
24
 
25
- sess = ort.InferenceSession(args.model, providers=["CPUExecutionProvider"])
26
- scores = sess.run(None, {sess.get_inputs()[0].name: rgb[None]})[0].ravel()
 
27
 
28
  top = int(np.argmax(scores))
29
  conf = float(scores[top])
 
3
 
4
  import cv2 as cv
5
  import numpy as np
 
6
 
7
  here = os.path.dirname(os.path.abspath(__file__))
8
 
 
21
 
22
  rgb = cv.resize(cv.cvtColor(img, cv.COLOR_BGR2RGB), (224, 224)).astype(np.float32)
23
 
24
+ net = cv.dnn.readNetFromONNX(args.model)
25
+ net.setInput(rgb[None])
26
+ scores = net.forward().ravel()
27
 
28
  top = int(np.argmax(scores))
29
  conf = float(scores[top])