|
| 1 | + |
| 2 | +#include <fstream> |
| 3 | +#include <opencv2/opencv.hpp> |
| 4 | +#include "yolov10/yolov10.hpp" |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +inline vector<string> cocolabels = { |
| 9 | + "person", "bicycle", "car", "motorcycle", "airplane", |
| 10 | + "bus", "train", "truck", "boat", "traffic light", "fire hydrant", |
| 11 | + "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", |
| 12 | + "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", |
| 13 | + "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", |
| 14 | + "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", |
| 15 | + "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", |
| 16 | + "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", |
| 17 | + "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", |
| 18 | + "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", |
| 19 | + "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", |
| 20 | + "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", |
| 21 | + "scissors", "teddy bear", "hair drier", "toothbrush" |
| 22 | +}; |
| 23 | + |
| 24 | +inline std::tuple<uint8_t, uint8_t, uint8_t> hsv2bgr(float h, float s, float v){ |
| 25 | + const int h_i = static_cast<int>(h * 6); |
| 26 | + const float f = h * 6 - h_i; |
| 27 | + const float p = v * (1 - s); |
| 28 | + const float q = v * (1 - f*s); |
| 29 | + const float t = v * (1 - (1 - f) * s); |
| 30 | + float r, g, b; |
| 31 | + switch (h_i) { |
| 32 | + case 0:r = v; g = t; b = p;break; |
| 33 | + case 1:r = q; g = v; b = p;break; |
| 34 | + case 2:r = p; g = v; b = t;break; |
| 35 | + case 3:r = p; g = q; b = v;break; |
| 36 | + case 4:r = t; g = p; b = v;break; |
| 37 | + case 5:r = v; g = p; b = q;break; |
| 38 | + default:r = 1; g = 1; b = 1;break;} |
| 39 | + return make_tuple(static_cast<uint8_t>(b * 255), static_cast<uint8_t>(g * 255), static_cast<uint8_t>(r * 255)); |
| 40 | +} |
| 41 | + |
| 42 | +inline std::tuple<uint8_t, uint8_t, uint8_t> random_color(int id){ |
| 43 | + float h_plane = ((((unsigned int)id << 2) ^ 0x937151) % 100) / 100.0f;; |
| 44 | + float s_plane = ((((unsigned int)id << 3) ^ 0x315793) % 100) / 100.0f; |
| 45 | + return hsv2bgr(h_plane, s_plane, 1); |
| 46 | +} |
| 47 | + |
| 48 | +inline string get_file_name(const string& path, bool include_suffix){ |
| 49 | + if (path.empty()) return ""; |
| 50 | + int p = path.rfind('/'); |
| 51 | + int e = path.rfind('\\'); |
| 52 | + p = std::max(p, e); |
| 53 | + p += 1; |
| 54 | + //include suffix |
| 55 | + if (include_suffix) |
| 56 | + return path.substr(p); |
| 57 | + int u = path.rfind('.'); |
| 58 | + if (u == -1) |
| 59 | + return path.substr(p); |
| 60 | + |
| 61 | + if (u <= p) u = path.size(); |
| 62 | + return path.substr(p, u - p); |
| 63 | +} |
| 64 | + |
| 65 | +void performance_v10(const string& engine_file, int gpuid){ |
| 66 | + auto infer = YOLOV10::create_infer(engine_file, gpuid, 0.5); |
| 67 | + if(infer == nullptr){ |
| 68 | + printf("infer is nullptr.\n"); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + int batch = 1; |
| 73 | + std::vector<cv::Mat> images{cv::imread("imgs/bus.jpg"), cv::imread("imgs/girl.jpg"), |
| 74 | + cv::imread("imgs/group.jpg"), cv::imread("imgs/yq.jpg")}; |
| 75 | + for (int i = images.size(); i < batch; ++i) |
| 76 | + images.push_back(images[i % 4]); |
| 77 | + |
| 78 | + // warmup |
| 79 | + vector<shared_future<YOLOV10::BoxArray>> boxes_array; |
| 80 | + for(int i = 0; i < 10; ++i) |
| 81 | + boxes_array = infer->commits(images); |
| 82 | + boxes_array.back().get(); |
| 83 | + boxes_array.clear(); |
| 84 | + |
| 85 | + // 测试 100 轮 |
| 86 | + const int ntest = 100; |
| 87 | + auto start = std::chrono::steady_clock::now(); |
| 88 | + for(int i = 0; i < ntest; ++i) |
| 89 | + boxes_array = infer->commits(images); |
| 90 | + // 等待全部推理结束 |
| 91 | + boxes_array.back().get(); |
| 92 | + |
| 93 | + std::chrono::duration<double> during = std::chrono::steady_clock::now() - start; |
| 94 | + double all_time = 1000.0 * during.count(); |
| 95 | + float avg_time = all_time / ntest / images.size(); |
| 96 | + printf("Average time: %.2f ms, FPS: %.2f\n", engine_file.c_str(), avg_time, 1000 / avg_time); |
| 97 | +} |
| 98 | + |
| 99 | +void batch_inference_v10(const string& engine_file, int gpuid){ |
| 100 | + auto infer = YOLOV10::create_infer(engine_file, gpuid, 0.5); |
| 101 | + if(infer == nullptr){ |
| 102 | + printf("infer is nullptr.\n"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + vector<cv::String> files_; |
| 107 | + files_.reserve(100); |
| 108 | + cv::glob("imgs/*.jpg", files_, true); |
| 109 | + vector<string> files(files_.begin(), files_.end()); |
| 110 | + |
| 111 | + vector<cv::Mat> images; |
| 112 | + for(const auto& file : files){ |
| 113 | + auto image = cv::imread(file); |
| 114 | + images.emplace_back(image); |
| 115 | + } |
| 116 | + |
| 117 | + vector<shared_future<YOLOV10::BoxArray>> boxes_array; |
| 118 | + boxes_array = infer->commits(images); |
| 119 | + |
| 120 | + // 等待全部推理结束 |
| 121 | + boxes_array.back().get(); |
| 122 | + |
| 123 | + string root_res = "infer_res/yolov10"; |
| 124 | + for(int i = 0; i < boxes_array.size(); ++i){ |
| 125 | + cv::Mat image = images[i]; |
| 126 | + auto boxes = boxes_array[i].get(); |
| 127 | + for(auto & ibox : boxes){ |
| 128 | + cv::Scalar color; |
| 129 | + std::tie(color[0], color[1], color[2]) = random_color(ibox.label); |
| 130 | + cv::rectangle(image, cv::Point(ibox.left, ibox.top), cv::Point(ibox.right, ibox.bottom), color, 2); |
| 131 | + |
| 132 | + auto name = cocolabels[ibox.label]; |
| 133 | + auto caption = cv::format("%s %.2f", name.c_str(), ibox.confidence); |
| 134 | + int text_width = cv::getTextSize(caption, 0, 1, 2, nullptr).width + 10; |
| 135 | + cv::rectangle(image, cv::Point(ibox.left-2, ibox.top-32), cv::Point(ibox.left + text_width, ibox.top), color, -1); |
| 136 | + cv::putText(image, caption, cv::Point(ibox.left, ibox.top-5), 0, 1, cv::Scalar::all(0), 2, 16); |
| 137 | + } |
| 138 | + string file_name = get_file_name(files[i], false); |
| 139 | + string save_path = cv::format("%s/%s.jpg", root_res.c_str(), file_name.c_str()); |
| 140 | + cv::imwrite(save_path, image); |
| 141 | + printf("Save to %s, %d object\n", save_path.c_str(), boxes.size()); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +void single_inference_v10(const string& engine_file, int gpuid){ |
| 147 | + auto infer = YOLOV10::create_infer(engine_file, gpuid, 0.6); |
| 148 | + if(infer == nullptr){ |
| 149 | + printf("infer is nullptr.\n"); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + auto image = cv::imread("imgs/bus.jpg"); |
| 154 | + auto boxes = infer->commit(image).get(); |
| 155 | + |
| 156 | + for(auto& ibox : boxes){ |
| 157 | + cv::Scalar color; |
| 158 | + std::tie(color[0], color[1], color[2]) = random_color(ibox.label); |
| 159 | + cv::rectangle(image, cv::Point(ibox.left, ibox.top), cv::Point(ibox.right, ibox.bottom), color, 2); |
| 160 | + |
| 161 | + auto name = cocolabels[ibox.label]; |
| 162 | + auto caption = cv::format("%s %.2f", name.c_str(), ibox.confidence); |
| 163 | + int text_width = cv::getTextSize(caption, 0, 1, 2, nullptr).width + 10; |
| 164 | + cv::rectangle(image, cv::Point(ibox.left-2, ibox.top-32), cv::Point(ibox.left + text_width, ibox.top), color, -1); |
| 165 | + cv::putText(image, caption, cv::Point(ibox.left, ibox.top-5), 0, 1, cv::Scalar::all(0), 2, 16); |
| 166 | + } |
| 167 | + cv::imwrite("infer_res/result.jpg", image); |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | + |
0 commit comments