-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
172 lines (137 loc) · 4.98 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <iostream>
#include <string>
#include "YOLOv12.h"
bool IsPathExist(const string& path) {
#ifdef _WIN32
DWORD fileAttributes = GetFileAttributesA(path.c_str());
return (fileAttributes != INVALID_FILE_ATTRIBUTES);
#else
return (access(path.c_str(), F_OK) == 0);
#endif
}
bool IsFile(const string& path) {
if (!IsPathExist(path)) {
printf("%s:%d %s not exist\n", __FILE__, __LINE__, path.c_str());
return false;
}
#ifdef _WIN32
DWORD fileAttributes = GetFileAttributesA(path.c_str());
return ((fileAttributes != INVALID_FILE_ATTRIBUTES) && ((fileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0));
#else
struct stat buffer;
return (stat(path.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode));
#endif
}
/**
* @brief Setting up Tensorrt logger
*/
class Logger : public nvinfer1::ILogger {
void log(Severity severity, const char* msg) noexcept override {
// Only output logs with severity greater than warning
if (severity <= Severity::kWARNING)
std::cout << msg << std::endl;
}
}logger;
int main(int argc, char** argv){
const string engine_file_path{ argv[1] };
const string path{ argv[2] };
vector<string> imagePathList;
bool isVideo{ false };
assert(argc == 3);
if (IsFile(path)){
string suffix = path.substr(path.find_last_of('.') + 1);
if (suffix == "jpg" || suffix == "jpeg" || suffix == "png"){
imagePathList.push_back(path);
}
else if (suffix == "mp4" || suffix == "avi" || suffix == "m4v" || suffix == "mpeg" || suffix == "mov" || suffix == "mkv" || suffix == "webm"){
isVideo = true;
}
else {
printf("suffix %s is wrong !!!\n", suffix.c_str());
abort();
}
}
else if (IsPathExist(path)){
glob(path + "/*.jpg", imagePathList);
}
YOLOv12 model(engine_file_path, logger);
if (engine_file_path.find(".onnx") != std::string::npos){
return 0;
}
if (isVideo) {
cout << "Opening video: " << path << endl;
cv::VideoCapture cap(path);
if (!cap.isOpened()) {
cerr << "Error: Cannot open video file!" << endl;
return 0 ;
}
// Get frame width, height, and FPS
int frameWidth = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
int frameHeight = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
int fps = static_cast<int>(cap.get(cv::CAP_PROP_FPS));
// Define the codec and create VideoWriter object
cv::VideoWriter videoWriter("output.mp4",
cv::VideoWriter::fourcc('m', 'p', '4', 'v'),
fps,
cv::Size(frameWidth, frameHeight));
if (!videoWriter.isOpened()) {
cerr << "Error: Cannot open VideoWriter!" << endl;
return 0 ;
}
while (true) {
cv::Mat image;
cap >> image;
if (image.empty()) {
break;
}
vector<Detection> objects;
model.preprocess(image);
auto start = std::chrono::system_clock::now();
model.infer();
auto end = std::chrono::system_clock::now();
model.postprocess(objects);
model.draw(image, objects);
auto tc = (double)std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.;
printf("Cost %2.4lf ms\n", tc);
// Write processed frame to output video
videoWriter.write(image);
if (cv::waitKey(1) == 27) { // Press 'ESC' to exit early
break;
}
}
// Release resources
cap.release();
videoWriter.release();
cv::destroyAllWindows();
}
else {
// path to folder saves images
for (const auto& imagePath : imagePathList){
// open image
Mat image = imread(imagePath);
if (image.empty()){
cerr << "Error reading image: " << imagePath << endl;
continue;
}
vector<Detection> objects;
model.preprocess(image);
auto start = std::chrono::system_clock::now();
model.infer();
auto end = std::chrono::system_clock::now();
model.postprocess(objects);
model.draw(image, objects);
auto tc = (double)std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.;
printf("cost %2.4lf ms\n", tc);
model.draw(image, objects);
imshow("Result", image);
waitKey(0);
}
}
return 0;
}