-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
119 lines (98 loc) · 3.91 KB
/
main.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include "mvt.h"
void track(MVT *tracker, const char *video_path)
{
std::string folderPath = video_path;
// Open the folder
std::string folderGlob = folderPath + "*.jpg"; // Change the file extension if necessary
std::vector<std::string> imageFiles;
cv::glob(folderGlob, imageFiles);
// Read the annotaiton file
std::ifstream inputFile(folderPath + "../groundtruth.txt");
std::string groundtruth_anno;
if (std::getline(inputFile, groundtruth_anno)) {
std::cout << "Initial bounding-box annotation: " << groundtruth_anno << std::endl;
} else {
std::cerr << "Failed to read the annotation file" << std::endl;
}
// Initial bounding box
std::string bboxString(groundtruth_anno);
float x, y, w, h;
std::istringstream iss(bboxString);
char delimiter;
if (!(iss >> x >> delimiter >> y >> delimiter >> w >> delimiter >> h)) {
std::cerr << "Error: Invalid input format" << std::endl;
}
cv::Rect trackWindow(x, y, w, h);
int frame_idx = 0;
bool isFirstImage = true;
for (const auto& imagePath : imageFiles) {
// Read the image
cv::Mat frame = cv::imread(imagePath);
if (isFirstImage){
std::cout << "Start track init ..." << std::endl;
std::cout << "==========================" << std::endl;
MVT_Output bbox;
bbox.box.x0 = trackWindow.x;
bbox.box.x1 = trackWindow.x+trackWindow.width;
bbox.box.y0 = trackWindow.y;
bbox.box.y1 = trackWindow.y+trackWindow.height;
tracker->init(frame, bbox);
std::cout << "==========================" << std::endl;
std::cout << "Init done!" << std::endl;
std::cout << std::endl;
isFirstImage = false;
}
else{
// Start timer
double t = (double)cv::getTickCount();
// Update tracker.
MVT_Output bbox = tracker->track(frame);
// Calculate Frames per second (FPS)
double inference_time = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
// Result to rect.
cv::Rect rect;
rect.x = bbox.box.x0;
rect.y = bbox.box.y0;
rect.width = int(bbox.box.x1 - bbox.box.x0);
rect.height = int(bbox.box.y1 - bbox.box.y0);
std::cout << "[x0, y0, w, h]: [" << rect.x << " " << rect.y << " " << rect.width << " " << rect.height << "]" << std::endl;
std::cout << "target classification score: " << bbox.score << std::endl;
// Boundary judgment.
cv::Mat track_window;
if (0 <= rect.x && 0 <= rect.width && rect.x + rect.width <= frame.cols && 0 <= rect.y && 0 <= rect.height && rect.y + rect.height <= frame.rows)
{
cv::rectangle(frame, rect, cv::Scalar(0, 255, 0), 2);
}
// Save the tracker output images
std::string dst_folderPath = "../data/output/";
std::string imagePath = dst_folderPath + std::to_string(frame_idx) + ".jpg"; // Adjust the file extension if necessary
cv::imwrite(imagePath, frame);
// Display per-frame inference time
std::cout << "Inference time: " << inference_time << " seconds" << std::endl;
std::cout << "==========================" << std::endl;
std::cout << std::endl;
}
frame_idx += 1;
}
}
int main(int argc, char** argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: %s [modelpath] [videopath]\n", argv[0]);
return -1;
}
// Get model path.
const char *model_path = argv[1];
// Get video path.
const char *video_path = argv[2];
// Build tracker.
MVT* MVT_tracker;
MVT_tracker = new MVT(model_path);
track(MVT_tracker, video_path);
return 0;
}