| 279 | } |
| 280 | |
| 281 | static void draw_output_infos( |
| 282 | const cv::Mat& bgr, const std::vector<OutputInfo>& output_infos, |
| 283 | std::string& output_name) { |
| 284 | cv::Mat image = bgr.clone(); |
| 285 | |
| 286 | for (size_t i = 0; i < output_infos.size(); i++) { |
| 287 | const OutputInfo& obj = output_infos[i]; |
| 288 | |
| 289 | fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob, |
| 290 | obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height); |
| 291 | |
| 292 | cv::Scalar color = cv::Scalar( |
| 293 | color_list[obj.label][0], color_list[obj.label][1], |
| 294 | color_list[obj.label][2]); |
| 295 | float c_mean = cv::mean(color)[0]; |
| 296 | cv::Scalar txt_color; |
| 297 | if (c_mean > 0.5) { |
| 298 | txt_color = cv::Scalar(0, 0, 0); |
| 299 | } else { |
| 300 | txt_color = cv::Scalar(255, 255, 255); |
| 301 | } |
| 302 | |
| 303 | cv::rectangle(image, obj.rect, color * 255, 2); |
| 304 | |
| 305 | char text[256]; |
| 306 | sprintf(text, "%s %.1f%%", class_names[obj.label], obj.prob * 100); |
| 307 | |
| 308 | int baseLine = 0; |
| 309 | cv::Size label_size = |
| 310 | cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, &baseLine); |
| 311 | |
| 312 | cv::Scalar txt_bk_color = color * 0.7 * 255; |
| 313 | |
| 314 | int x = obj.rect.x; |
| 315 | int y = obj.rect.y + 1; |
| 316 | if (y > image.rows) |
| 317 | y = image.rows; |
| 318 | cv::rectangle( |
| 319 | image, |
| 320 | cv::Rect( |
| 321 | cv::Point(x, y), |
| 322 | cv::Size(label_size.width, label_size.height + baseLine)), |
| 323 | txt_bk_color, -1); |
| 324 | |
| 325 | cv::putText( |
| 326 | image, text, cv::Point(x, y + label_size.height), |
| 327 | cv::FONT_HERSHEY_SIMPLEX, 0.4, txt_color, 1); |
| 328 | } |
| 329 | |
| 330 | cv::imwrite(output_name.c_str(), image); |
| 331 | std::cout << "save output to " << output_name << std::endl; |
| 332 | } |
| 333 | |
| 334 | std::vector<float> run_model(LiteNetwork model) { |
| 335 | struct timeval start; |