SavyaSanchi-Sharma commited on
Commit
cac97e7
·
1 Parent(s): e9eb33d

tensorflow_inception_graph

Browse files
tensorflow_inception_graph/README.md CHANGED
@@ -27,19 +27,9 @@ net = cv2.dnn.readNet("tensorflow_inception_graph_2026jul.onnx")
27
  ```
28
 
29
  ### C++
30
- The C++ demo runs inference with OpenCV's DNN module. Adjust the OpenCV paths to your setup:
31
  ```bash
32
- OCV=/path/to/opencv # OpenCV source tree
33
- OCVBUILD=/path/to/opencv/build # OpenCV build directory (generated headers + libs)
34
- g++ -std=c++17 demo.cpp -o demo \
35
- -I$OCV/include \
36
- -I$OCV/modules/core/include \
37
- -I$OCV/modules/dnn/include \
38
- -I$OCV/modules/imgproc/include \
39
- -I$OCV/modules/imgcodecs/include \
40
- -I$OCVBUILD \
41
- -L$OCVBUILD/lib -Wl,-rpath,$OCVBUILD/lib -lopencv_dnn -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
42
- ./demo --model tensorflow_inception_graph_2026jul.onnx --image example_outputs/input_image.png
43
  ```
44
 
45
  ## Conversion
 
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
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 <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,44 +22,45 @@ 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
- 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
  }
 
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
  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
  }
tensorflow_inception_graph/demo.py CHANGED
@@ -3,6 +3,7 @@ import os
3
 
4
  import cv2 as cv
5
  import numpy as np
 
6
 
7
  here = os.path.dirname(os.path.abspath(__file__))
8
 
@@ -21,9 +22,8 @@ def main():
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])
 
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
 
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])