main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <iostream>
  2. #include<opencv2/opencv.hpp>
  3. #include<math.h>
  4. #include "yolov8_onnx.h"
  5. #include "yolov8_pose_onnx.h"
  6. #include<time.h>
  7. //#define VIDEO_OPENCV //if define, use opencv for video.
  8. using namespace std;
  9. using namespace cv;
  10. using namespace dnn;
  11. template<typename _Tp>
  12. int yolov8_onnx(_Tp& task, cv::Mat& img, std::string& model_path)
  13. {
  14. if (task.ReadModel(model_path, true)) {
  15. std::cout << "read net ok!" << std::endl;
  16. }
  17. else {
  18. std::cerr << "fail to read net!" << std::endl;
  19. return -1;
  20. }
  21. //生成随机颜色
  22. std::vector<cv::Scalar> color;
  23. srand(time(0));
  24. for (int i = 0; i < 80; i++) {
  25. int b = rand() % 256;
  26. int g = rand() % 256;
  27. int r = rand() % 256;
  28. color.push_back(cv::Scalar(b, g, r));
  29. }
  30. bool isPose = false;
  31. if (typeid(task) == typeid(Yolov8PoseOnnx)) {
  32. isPose = true;
  33. }
  34. PoseParams poseParams;
  35. std::vector<OutputParams> result;
  36. if (task.OnnxDetect(img, result)) {
  37. if (isPose)
  38. DrawPredPose(img, result, poseParams);
  39. else
  40. DrawPred(img, result, task._className, color);
  41. }
  42. else {
  43. std::cout << "Detect Failed!" << std::endl;
  44. }
  45. cv::imshow("1", img);
  46. cv::waitKey(0);
  47. system("pause");
  48. return 0;
  49. }
  50. template<typename _Tp>
  51. int video_demo(_Tp& task, std::string& model_path)
  52. {
  53. std::vector<cv::Scalar> color;
  54. srand(time(0));
  55. for (int i = 0; i < 80; i++) {
  56. int b = rand() % 256;
  57. int g = rand() % 256;
  58. int r = rand() % 256;
  59. color.push_back(cv::Scalar(b, g, r));
  60. }
  61. std::vector<OutputParams> result;
  62. cv::VideoCapture cap(0);
  63. if (!cap.isOpened())
  64. {
  65. std::cout << "open capture failured!" << std::endl;
  66. return -1;
  67. }
  68. cv::Mat frame;
  69. bool isPose = false;
  70. PoseParams poseParams;
  71. #ifdef VIDEO_OPENCV
  72. cv::dnn::Net net;
  73. if (typeid(task) == typeid(Yolov8Pose)) {
  74. isPose = true;
  75. }
  76. if (task.ReadModel(net, model_path, true)) {
  77. std::cout << "read net ok!" << std::endl;
  78. }
  79. else {
  80. std::cout << "read net failured!" << std::endl;
  81. return -1;
  82. }
  83. #else
  84. if (typeid(task) == typeid(Yolov8PoseOnnx)) {
  85. isPose = true;
  86. }
  87. if (task.ReadModel(model_path, true)) {
  88. std::cout << "read net ok!" << std::endl;
  89. }
  90. else {
  91. std::cout << "read net failured!" << std::endl;
  92. return -1;
  93. }
  94. #endif
  95. while (true)
  96. {
  97. cap.read(frame);
  98. if (frame.empty())
  99. {
  100. std::cout << "read to end" << std::endl;
  101. break;
  102. }
  103. result.clear();
  104. #ifdef VIDEO_OPENCV
  105. if (task.Detect(frame, net, result)) {
  106. if (isPose)
  107. DrawPredPose(frame, result, poseParams,true);
  108. else
  109. DrawPred(frame, result, task._className, color,true);
  110. }
  111. #else
  112. if (task.OnnxDetect(frame, result)) {
  113. if (isPose)
  114. DrawPredPose(frame, result, poseParams);
  115. else
  116. DrawPred(frame, result, task._className, color);
  117. }
  118. #endif
  119. cv::imshow("1", frame);
  120. int k = waitKey(10);
  121. if (k == 27) { //esc
  122. break;
  123. }
  124. }
  125. cap.release();
  126. system("pause");
  127. return 0;
  128. }
  129. #define Pos_Flag
  130. int main() {
  131. #ifdef Pos_Flag
  132. std::string img_path = "/home/sxyx2026/cdq_data/imgs/2/2_2026-01-22_21-48-33-291.jpg";
  133. std::string model_path = "/home/sxyx2026/cdq_arrow.onnx";
  134. cv::Mat src = imread(img_path);
  135. cv::Mat img = src.clone();
  136. Yolov8PoseOnnx task_pose_ort;
  137. yolov8_onnx(task_pose_ort, img, model_path); //yolov8 onnxruntime pose
  138. #else
  139. std::string img_path = "/home/sxyx2026/cdq_data/imgs/0/0_2026-01-22_14-35-48-249.jpg";
  140. std::string model_path = "/home/sxyx2026/cdq_grab.onnx";
  141. cv::Mat src = imread(img_path);
  142. cv::Mat img = src.clone();
  143. Yolov8Onnx task_detect_ort;
  144. yolov8_onnx(task_detect_ort,img,model_path); //yoolov8 onnxruntime detect
  145. #endif
  146. return 0;
  147. }