-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.cpp
370 lines (334 loc) · 14.4 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include <iostream>
#include <string>
#include <tuple>
#include <cmath>
#include <unordered_map>
#include <NvInfer.h>
#include <opencv2/opencv.hpp>
#include "utils.h"
#include "depth_anything.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)) {
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) {
bool model_loaded = false;
// all valid video & image extensions
vector<string> video_extensions = {"avi", "mp4", "m4v", "mpeg", "mov", "mkv"};
vector<string> image_extensions = {"jpeg", "jpg", "png"};
// organizing options & arguements into map
unordered_map<string, string> options;
string previous_option = "";
string current_arguement = "";
DepthAnything depth_model;
int cutoff = 0;
for (int i = 0; i < argc; i++) {
cutoff = 0;
current_arguement = argv[i];
if (current_arguement[0] == '-') {
if (current_arguement[1] == '-') {
cutoff = 2;
} else {
cutoff = 1;
}
previous_option = current_arguement.substr(cutoff);
options[previous_option] = "1";
} else if (!previous_option.empty()) {
options[previous_option] = current_arguement.substr(cutoff);
previous_option = "";
}
}
// load selected model
if (!options["model"].empty()) {
string model_path = options["model"];
if (!IsFile(model_path)) {
cout << "Model not found!" << endl;
abort();
}
cout << "Loading model: \"" << model_path << "\"" << endl;
string alternate_path = model_path.substr(0, model_path.length() - 5) + ".engine";
if (model_path.substr(model_path.find_last_of('.') + 1) == "onnx" && IsFile(alternate_path)) {
if (options["find-engine"].empty()) {
string confirm_engine = "";
cout << "\"" << alternate_path << "\" found, Override existing .engine file? (Y/N): ";
cin >> confirm_engine;
if (confirm_engine != "Y" && confirm_engine != "y"){
model_path = alternate_path;
}
} else {
model_path = alternate_path;
}
}
depth_model.init(model_path, logger);
cout << "Model successfully loaded." << endl << endl;
model_loaded = true;
}
if (!options["input"].empty()) {
string input = options["input"];
std::vector<std::string> imagePathList;
std::vector<std::string> videoPathList;
string suffix = input.substr(input.find_last_of('.') + 1);
bool suffix_found = false;
// organize images and videos in path into seperate lists
if (IsFile(input)) {
for (string& proper_suffix : image_extensions) {
if (suffix == proper_suffix) {
imagePathList.push_back(input);
suffix_found = true;
break;
}
}
if (!suffix_found) {
for (string& proper_suffix : video_extensions) {
if (suffix == proper_suffix) {
videoPathList.push_back(input);
suffix_found = true;
break;
}
}
}
if (!suffix_found) {
printf("Incorrect suffix %s!\n", suffix.c_str());
std::abort();
}
} else if (IsPathExist(input)) {
vector<string> current_extension_found;
for (string& current_extension : image_extensions) {
cv::glob(input + "/*." + current_extension, current_extension_found);
imagePathList.insert(
imagePathList.end(),
current_extension_found.begin(),
current_extension_found.end()
);
current_extension_found.clear();
}
for (string& current_extension : video_extensions) {
cv::glob(input + "/*." + current_extension, current_extension_found);
videoPathList.insert(
videoPathList.end(),
current_extension_found.begin(),
current_extension_found.end()
);
current_extension_found.clear();
}
} else {
cout << "Input location invalid!" << endl;
}
if (model_loaded) {
string base_filename;
string filename;
string prefix = "depth_";
string output;
string generated_name;
string full_output_location;
string current_suffix;
if (!options["prefix"].empty()) {
prefix = options["prefix"];
}
//iterate through videoPathList and render depthmaps.
for (const auto& videoPath : videoPathList) {
base_filename = videoPath.substr(videoPath.find_last_of("/\\") + 1);
filename = base_filename.substr(0, base_filename.find_last_of('.'));
generated_name = prefix + filename;
if (!options["output"].empty()) {
output = options["output"];
if (output.back() != '\\') {
output += "\\";
}
if (!IsPathExist(output)) {
cout << "Output location invalid!" << endl;
abort();
}
}
full_output_location = output + generated_name;
// open cap
cv::VideoCapture cap(videoPath);
int width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
double fps;
double frame_total = cap.get(cv::CAP_PROP_FRAME_COUNT);
int frame_num = 0;
double previous_tpf = 0;
double estimate = 0;
double tpf = 0;
int estimate_rounded = 0;
int progress = 0;
if (!options["fps"].empty()) {
try {
fps = stod(options["fps"]);
} catch (const invalid_argument&) {
cerr << "Invalid fps value!" << endl;
abort();
}
} else {
fps = cap.get(cv::CAP_PROP_FPS);
}
// Create a VideoWriter object to save the processed video
full_output_location += ".avi";
cout << full_output_location << ":" << endl;
cv::VideoWriter output_video(full_output_location, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), fps, cv::Size(width, height));
while (1) {
frame_num ++;
cv::Mat frame;
cap >> frame;
if (frame.empty())
break;
auto start = std::chrono::system_clock::now();
cv::Mat result_d = depth_model.predict(frame);
auto end = chrono::system_clock::now();
tpf = chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
if (previous_tpf == 0) {
estimate = (frame_total - frame_num) * (tpf / 1000.0);
} else if (frame_num <= 10) {
estimate = (frame_total - frame_num) * (((tpf * (frame_num - 1) + previous_tpf) / frame_num) / 1000.0);
} else {
estimate = (frame_total - frame_num) * (((tpf * (9) + previous_tpf) / 10) / 1000.0);
}
previous_tpf = tpf;
progress = floor((frame_num / frame_total) * 100);
cout << "frame#:" << setw(6) << frame_num << " progress:" << setw(3) << progress << "% time per frame:" << setw(9) << tpf << "ms fps:" << setw(4) << floor(100 / (tpf / 1000)) / 100.0 << " eta:";
if (estimate >= 60) {
estimate_rounded = estimate;
cout << setw(3) << floor(estimate / 60) << ":" << setw(2) << estimate_rounded % 60;
} else {
cout << setw(5) << floor(estimate * 100) / 100.0 << "sec";
}
cout << " [" << string(floor(progress / 2.5), 'I') << string(40 - floor(progress / 2.5), ' ') << "]";
if (options["one-line"].empty()) {
cout << endl;
} else {
cout << "\t\r" << flush;
}
if (!options["preview"].empty()) {
cv::Mat show_frame;
frame.copyTo(show_frame);
cv::Mat result;
cv::hconcat(show_frame, result_d, result);
cv::resize(result, result, cv::Size(width, height / 2)); //IMPORTANT
imshow("Depth: Before -> After", result);
}
output_video.write(result_d);
cv::waitKey(1);
}
if (!options["one-line"].empty()) {
cout << endl;
}
// Release resources
cv::destroyAllWindows();
cap.release();
output_video.release();
cout << full_output_location << " finished generating." << endl << endl;
}
// iterate through imagePathList and render depthmaps.
for (const auto& imagePath : imagePathList) {
base_filename = imagePath.substr(imagePath.find_last_of("/\\") + 1);
filename = base_filename.substr(0, base_filename.find_last_of('.'));
generated_name = prefix + filename;
if (!options["output"].empty()) {
output = options["output"];
if (output.back() != '\\') {
output += "\\";
}
if (!IsPathExist(output)) {
cout << "Output location invalid!" << endl;
abort();
}
}
current_suffix = imagePath.substr(imagePath.find_last_of('.') + 1);
full_output_location = output + generated_name + "." + current_suffix;
cout << full_output_location << ":" << endl;
// open image
cv::Mat frame = cv::imread(imagePath);
if (frame.empty())
{
cerr << "Error reading image: " << imagePath << endl;
continue;
}
auto start = chrono::system_clock::now();
cv::Mat result_d = depth_model.predict(frame);
auto end = chrono::system_clock::now();
double tpf = chrono::duration_cast<std::chrono::duration<double, std::milli>>(end - start).count();
cout << "time per frame:" << setw(9) << tpf << "ms fps:" << setw(4) << floor(100 / (tpf / 1000)) / 100.0 << endl;
if (!options["preview"].empty()) {
cv::Mat show_frame;
frame.copyTo(show_frame);
cv::Mat result;
cv::hconcat(show_frame, result_d, result);
cv::resize(result, result, cv::Size(1080, 480));
imshow("depth_result", result);
cv::waitKey(1);
}
cv::imwrite(full_output_location, result_d);
cout << full_output_location << " finished generating." << endl << endl;
}
}
}
return 0;
}