-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.cpp
145 lines (121 loc) · 3.95 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
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <iostream>
#include <string>
#include "yolov11.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);
}
// Assume it's a folder, add logic to handle folders
// init model
YOLOv11 model(engine_file_path, logger);
if (isVideo) {
//path to video
cv::VideoCapture cap(path);
while (1)
{
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);
imshow("prediction", image);
waitKey(1);
}
// Release resources
destroyAllWindows();
cap.release();
}
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;
}