-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
196 lines (180 loc) · 6.21 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <string>
#include <tuple>
#include <NvInfer.h>
#include <opencv2/opencv.hpp>
#include "utils.h"
#include "birefnet.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
using namespace std;
// Helper function to replace all occurrences of a character in a string
void replaceChar(std::string& str, char find, char replace) {
size_t pos = 0;
while ((pos = str.find(find, pos)) != std::string::npos) {
str[pos] = replace;
pos++;
}
}
bool IsPathExist(const std::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 std::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
}
bool createFolder(const std::string& folderPath) {
#ifdef _WIN32
if (!CreateDirectory(folderPath.c_str(), NULL)) {
DWORD error = GetLastError();
if (error == ERROR_ALREADY_EXISTS) {
std::cout << "Folder already exists!" << std::endl;
return true; // Folder already exists
}
else {
std::cerr << "Failed to create folder! Error code: " << error << std::endl;
return false; // Failed to create folder
}
}
#else
if (mkdir(folderPath.c_str(), 0777) != 0) {
if (errno == EEXIST) {
std::cout << "Folder already exists!" << std::endl;
return true; // Folder already exists
}
else {
std::cerr << "Failed to create folder! Error code: " << errno << std::endl;
return false; // Failed to create folder
}
}
#endif
std::cout << "Folder created successfully!" << std::endl;
return true; // Folder created successfully
}
/**
* @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 std::string engine_file_path{ argv[1] };
const std::string path{ argv[2] };
std::vector<std::string> imagePathList;
bool isVideo{ false };
assert(argc == 3);
if (IsFile(path)) {
std::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")
{
isVideo = true;
}
else {
printf("suffix %s is wrong !!!\n", suffix.c_str());
std::abort();
}
}
else if (IsPathExist(path))
{
cv::glob(path + "/*.jpg", imagePathList);
}
// Assume it's a folder, add logic to handle folders
// init model
cout << "Loading model from " << engine_file_path << "..." << endl;
BiRefNet birefnet_model(engine_file_path, logger);
cout << "The model has been successfully loaded!" << endl;
if (isVideo) {
//path to video
string VideoPath = path;
// open cap
cv::VideoCapture cap(VideoPath);
int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
// Create a VideoWriter object to save the processed video
cv::VideoWriter output_video("output_video.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), 30, cv::Size(width*2, height));
while (1)
{
cv::Mat frame;
cv::Mat show_frame;
cap >> frame;
if (frame.empty())
break;
frame.copyTo(show_frame);
cv::Mat new_frame;
frame.copyTo(new_frame);
auto start = std::chrono::system_clock::now();
std::pair<cv::Mat, cv::Mat> result_pair = birefnet_model.predict(frame);
auto end = chrono::system_clock::now();
cout << "Time of per frame: " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << "ms" << endl;
cv::Mat result;
cv::hconcat(result_pair.first, result_pair.second, result);
output_video.write(result);
}
// Release resources
cv::destroyAllWindows();
cap.release();
output_video.release();
}
else {
// path to folder saves images
string imageFolderPath_out = "results/";
createFolder(imageFolderPath_out);
for (const auto& imagePath : imagePathList)
{
// open image
cv::Mat frame = cv::imread(imagePath);
if (frame.empty())
{
cerr << "Error reading image: " << imagePath << endl;
continue;
}
cv::Mat show_frame;
frame.copyTo(show_frame);
auto start = chrono::system_clock::now();
std::pair<cv::Mat, cv::Mat> result_pair = birefnet_model.predict(frame);
auto end = chrono::system_clock::now();
cout << "Time of per frame: " << chrono::duration_cast<chrono::milliseconds>(end - start).count() << "ms" << endl;
cv::Mat result;
cv::hconcat(result_pair.first, result_pair.second, result);
std::istringstream iss(imagePath);
std::string token;
while (std::getline(iss, token, '/'))
{
}
token = token.substr(token.find_last_of("/\\") + 1);
std::cout << "Path : " << imageFolderPath_out + token << std::endl;
cv::imwrite(imageFolderPath_out + token, result);
}
}
cout << "finished" << endl;
return 0;
}