Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

main.cpp 2.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  1. // Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. #include <iostream>
  3. #include <vector>
  4. #include <getopt.h>
  5. #include <opencv2/opencv.hpp>
  6. #include "inference.h"
  7. using namespace std;
  8. using namespace cv;
  9. int main(int argc, char **argv)
  10. {
  11. std::string projectBasePath = "/home/user/ultralytics"; // Set your ultralytics base path
  12. bool runOnGPU = true;
  13. //
  14. // Pass in either:
  15. //
  16. // "yolov8s.onnx" or "yolov5s.onnx"
  17. //
  18. // To run Inference with yolov8/yolov5 (ONNX)
  19. //
  20. // Note that in this example the classes are hard-coded and 'classes.txt' is a place holder.
  21. Inference inf(projectBasePath + "/yolov8s.onnx", cv::Size(640, 640), "classes.txt", runOnGPU);
  22. std::vector<std::string> imageNames;
  23. imageNames.push_back(projectBasePath + "/ultralytics/assets/bus.jpg");
  24. imageNames.push_back(projectBasePath + "/ultralytics/assets/zidane.jpg");
  25. for (int i = 0; i < imageNames.size(); ++i)
  26. {
  27. cv::Mat frame = cv::imread(imageNames[i]);
  28. // Inference starts here...
  29. std::vector<Detection> output = inf.runInference(frame);
  30. int detections = output.size();
  31. std::cout << "Number of detections:" << detections << std::endl;
  32. for (int i = 0; i < detections; ++i)
  33. {
  34. Detection detection = output[i];
  35. cv::Rect box = detection.box;
  36. cv::Scalar color = detection.color;
  37. // Detection box
  38. cv::rectangle(frame, box, color, 2);
  39. // Detection box text
  40. std::string classString = detection.className + ' ' + std::to_string(detection.confidence).substr(0, 4);
  41. cv::Size textSize = cv::getTextSize(classString, cv::FONT_HERSHEY_DUPLEX, 1, 2, 0);
  42. cv::Rect textBox(box.x, box.y - 40, textSize.width + 10, textSize.height + 20);
  43. cv::rectangle(frame, textBox, color, cv::FILLED);
  44. cv::putText(frame, classString, cv::Point(box.x + 5, box.y - 10), cv::FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 0, 0), 2, 0);
  45. }
  46. // Inference ends here...
  47. // This is only for preview purposes
  48. float scale = 0.8;
  49. cv::resize(frame, frame, cv::Size(frame.cols*scale, frame.rows*scale));
  50. cv::imshow("Inference", frame);
  51. cv::waitKey(-1);
  52. }
  53. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...