|
| 1 | + |
| 2 | +#include <iostream> |
| 3 | +#include <fstream> |
| 4 | +#include <boost/format.hpp> |
| 5 | +#include "lighttrack/LightTrack.hpp" |
| 6 | +#include "ostrack/OSTrack.hpp" |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | + |
| 11 | +inline static cv::Rect read_gt(std::string >_file) { |
| 12 | + std::string line; |
| 13 | + cv::Rect gt_box; |
| 14 | + std::ifstream fin(gt_file); //真实值文件 |
| 15 | + getline(fin, line); |
| 16 | + if (!fin) |
| 17 | + std::cout << " Do not read groundtruth!!! " << std::endl; |
| 18 | + |
| 19 | + std::stringstream line_ss = std::stringstream(line); |
| 20 | + if (line.find(',') != std::string::npos) { |
| 21 | + std::vector<std::string> data; |
| 22 | + std::string tmp; |
| 23 | + while (getline(line_ss, tmp, ',')) |
| 24 | + data.push_back(tmp); |
| 25 | + gt_box.x = stoi(data[0]); |
| 26 | + gt_box.y = stoi(data[1]); |
| 27 | + gt_box.width = stoi(data[2]); |
| 28 | + gt_box.height = stoi(data[3]); |
| 29 | + } else |
| 30 | + line_ss >> gt_box.x >> gt_box.y >> gt_box.width >> gt_box.height; |
| 31 | + |
| 32 | + return gt_box; |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +template<class T> |
| 37 | +void LaunchTrack(shared_ptr<T> tracker, int Mode, const string& path){ |
| 38 | + |
| 39 | + cv::VideoCapture cap; |
| 40 | + string display_name = "Track"; |
| 41 | + |
| 42 | + if (Mode == 0 || Mode == 1) { |
| 43 | + cv::Mat frame; |
| 44 | + cap.open(path); |
| 45 | + cap >> frame; |
| 46 | + cv::imshow(display_name, frame); |
| 47 | + cv::putText(frame, "Select target ROI and press ENTER", cv::Point2i(20, 30), |
| 48 | + cv::FONT_HERSHEY_COMPLEX_SMALL, 1, cv::Scalar(255,0,0), 1); |
| 49 | + cv::Rect init_bbox = cv::selectROI(display_name, frame); |
| 50 | + tracker->init(frame, init_bbox); |
| 51 | + cv::Rect bbox; |
| 52 | + cv::Mat img; |
| 53 | + while (true) { |
| 54 | + bool ret = cap.read(img); |
| 55 | + if (!ret) { |
| 56 | + cout << "----------Read failed!!!----------" << endl; |
| 57 | + return; |
| 58 | + } |
| 59 | + auto start = std::chrono::steady_clock::now(); |
| 60 | + bbox = tracker->track(img); |
| 61 | + auto end = std::chrono::steady_clock::now(); |
| 62 | + std::chrono::duration<double> elapsed = end - start; |
| 63 | + double time = 1000 * elapsed.count(); |
| 64 | + printf("all infer time: %f ms\n", time); |
| 65 | + cv::rectangle(img, bbox, cv::Scalar(0,255,0), 2); |
| 66 | + cv::imshow(display_name, img); |
| 67 | + cv::waitKey(1); |
| 68 | + } |
| 69 | + } |
| 70 | + else if (Mode == 2) { |
| 71 | + string gt_path = "Woman/groundtruth_rect.txt"; |
| 72 | + boost::format fmt(path.data()); //数据集图片 |
| 73 | + cv::Mat frame; |
| 74 | + frame = cv::imread((fmt % 1).str(),1); |
| 75 | + cv::imshow(display_name, frame); |
| 76 | + cv::Rect init_bbox = read_gt(gt_path); |
| 77 | + tracker->init(frame, init_bbox); |
| 78 | + cv::Rect bbox; |
| 79 | + cv::Mat img; |
| 80 | + for (int i = 1; i < 597; ++i) { |
| 81 | + img = cv::imread((fmt % i).str(),1); |
| 82 | + auto start = std::chrono::steady_clock::now(); |
| 83 | + bbox = tracker->track(img); |
| 84 | + auto end = std::chrono::steady_clock::now(); |
| 85 | + std::chrono::duration<double> elapsed = end - start; |
| 86 | + double time = 1000 * elapsed.count(); |
| 87 | + printf("all infer time: %f ms\n", time); |
| 88 | + cv::rectangle(img, bbox, cv::Scalar(0,255,0), 2); |
| 89 | + cv::imshow(display_name, img); |
| 90 | + cv::waitKey(1); |
| 91 | + } |
| 92 | + return; |
| 93 | + } |
| 94 | + else { |
| 95 | + printf("Mode错误,0:视频文件;1:摄像头;2:数据集"); |
| 96 | + return; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +int infer_track(int Mode, const string& path){ |
| 102 | + string z_path = "lighttrack-z.trt"; |
| 103 | + string x_path = "lighttrack-x-head.trt"; |
| 104 | + string engine_path = "ostrack-256.trt"; |
| 105 | + |
| 106 | +// auto tracker = LightTrack::create_tracker(z_path, x_path); |
| 107 | + auto tracker = OSTrack::create_tracker(engine_path); |
| 108 | + if(tracker == nullptr){ |
| 109 | + printf("tracker is nullptr.\n"); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + LaunchTrack(tracker, Mode, path); |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
0 commit comments