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 3.0 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
73
74
75
76
  1. // Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. #include "inference.hpp"
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <sstream>
  7. #include <iomanip>
  8. #include <cstdint>
  9. #include <chrono>
  10. #define MODEL_INPUT_IMAGE_WIDTH 640
  11. #define MODEL_INPUT_IMAGE_HEIGHT 640
  12. #define NETWORK_THRESHOLD 0.50
  13. #define IMAGE_CHANNEL 3
  14. double get_time_since_epoch_millis()
  15. {
  16. using namespace std::chrono;
  17. auto now = system_clock::now();
  18. auto duration = now.time_since_epoch();
  19. return duration_cast<microseconds>(duration).count() / 1000.0;
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. std::string triton_address= "localhost:8001";
  24. std::string model_name= "yolo11";
  25. std::string model_version= "1";
  26. std::string image_path = "test.jpg";
  27. std::string output_path = "output.jpg";
  28. std::vector<std::string> object_class_list = {"class1", "class2"};
  29. std::vector<uint16_t> triton_request_data;
  30. triton_request_data.resize(IMAGE_CHANNEL*MODEL_INPUT_IMAGE_WIDTH*MODEL_INPUT_IMAGE_HEIGHT);
  31. std::vector<struct detection_struct> detections;
  32. std::shared_ptr<TritonCommunication> triton_communication = std::make_shared<TritonCommunication>(triton_address, model_name, model_version, IMAGE_CHANNEL, MODEL_INPUT_IMAGE_WIDTH, MODEL_INPUT_IMAGE_HEIGHT,object_class_list.size());
  33. cv::Mat frame = cv::imread(image_path);
  34. if (frame.empty())
  35. {
  36. std::cerr << "Image couldn't read: " << image_path << std::endl;
  37. return -1;
  38. }
  39. int image_width = frame.cols;
  40. int image_height = frame.rows;
  41. double preprocess_time = get_time_since_epoch_millis();
  42. Image::preprocess(&frame, triton_request_data, MODEL_INPUT_IMAGE_WIDTH, MODEL_INPUT_IMAGE_HEIGHT);
  43. std::cout << "Preprocess time : " << (get_time_since_epoch_millis() - preprocess_time)<< " millisecond."<< std::endl;
  44. double infer_time = get_time_since_epoch_millis();
  45. triton_communication->infer(triton_request_data.data());
  46. std::cout << "Triton Server execute time : " << (get_time_since_epoch_millis() - infer_time) << " millisecond." << std::endl;
  47. getDetectionsFromTritonRawData(triton_communication->output_raw_data, detections, object_class_list, NETWORK_THRESHOLD, image_width, image_height);
  48. for (int i = 0; i < detections.size(); i++)
  49. {
  50. std::ostringstream oss;
  51. oss << detections[i].name << " "
  52. << std::fixed << std::setprecision(2)
  53. << detections[i].confidence_score;
  54. cv::rectangle(frame, detections[i].bbox, cv::Scalar(255, 0, 0), 2);
  55. cv::putText(frame, oss.str(), cv::Point((detections[i].bbox.x), (detections[i].bbox.y - 5)), cv::FONT_HERSHEY_DUPLEX, ((frame.cols / 640.0f) * 0.35), cv::Scalar(0, 0, 0), (int)(frame.cols / 640.0f) + 1);
  56. cv::putText(frame, oss.str(), cv::Point((detections[i].bbox.x), (detections[i].bbox.y - 5)), cv::FONT_HERSHEY_DUPLEX, ((frame.cols / 640.0f) * 0.35), cv::Scalar(0xFF, 0xFF, 0xFF), (int)(frame.cols / 640.0f));
  57. }
  58. cv::imwrite(output_path, frame);
  59. std::cout << "Result image saved!"<< std::endl;
  60. return 0;
  61. }
Tip!

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

Comments

Loading...