-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
123 lines (113 loc) · 3.39 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
//
// Created by lsf on 2023/5/11.
//
#include "Nanodet.h"
int image_demo(const std::shared_ptr<NanoDet>& detector, const char* imagepath)
{
cv::Mat image = cv::imread(imagepath);
if (image.empty())
{
fprintf(stderr, "cv::imread %s failed.\n", imagepath);
return -1;
}
std::vector<Box> boxes;
detector->detect(image, boxes);
detector->draw(image, boxes);
cv::imshow("Nanodet", image);
cv::waitKey(0);
boxes.clear();
return 0;
}
int webcam_demo(const std::shared_ptr<NanoDet>& detector, int cam_id)
{
cv::Mat image;
cv::VideoCapture cap(cam_id);
std::vector<Box> boxes;
while (true)
{
bool ret = cap.read(image);
if (!ret) {
fprintf(stderr, "VideoCapture %d read failed.\n", cam_id);
return 0;
}
auto start = std::chrono::steady_clock::now();
detector->detect(image, boxes);
detector->draw(image, boxes);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = end - start;
double time = 1000 * elapsed.count();
printf("Detect time = %.2f ms\n", time);
cv::imshow("Nanodet", image);
cv::waitKey(1);
boxes.clear();
}
}
int video_demo(const std::shared_ptr<NanoDet>& detector, const char* path)
{
cv::Mat image;
cv::VideoCapture cap(path);
std::vector<Box> boxes;
while (true)
{
bool ret = cap.read(image);
if (!ret) {
fprintf(stderr, "Video %s read failed.\n", path);
return 0;
}
auto start = std::chrono::steady_clock::now();
detector->detect(image, boxes);
detector->draw(image, boxes);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = end - start;
double time = 1000 * elapsed.count();
printf("Detect time = %.2f ms\n", time);
cv::imshow("Nanodet", image);
cv::waitKey(1);
boxes.clear();
}
}
void benchmark(const std::shared_ptr<NanoDet>& detector)
{
int loop_num = 1000;
detector->benchmark(loop_num);
}
int main(int argc, char** argv)
{
if (argc != 3)
{
fprintf(stderr, "usage: %s [mode] [path]. \n For webcam mode=0, path is cam id; \n For image demo, mode=1, path=xxx/xxx/*.jpg; \n For video, mode=2; \n For benchmark, mode=3 path=0.\n", argv[0]);
return -1;
}
std::cout << "Start init model." << std::endl;
// nanodet-full-1.5x-320-int8-ppq.xml nanodet-1.5x-320-int8.xml
auto detector = std::make_shared<NanoDet>("nanodet-full-1.5x-416-int8-ppq.xml",
416, 416, 0.4, 0.5);
std::cout << "Init model success." << std::endl;
int mode = atoi(argv[1]);
switch (mode)
{
case 0:{
const char* images = argv[2];
image_demo(detector, images);
break;
}
case 1:{
int cam_id = atoi(argv[2]);
webcam_demo(detector, cam_id);
break;
}
case 2:{
const char* path = argv[2];
video_demo(detector, path);
break;
}
case 3:{
benchmark(detector);
break;
}
default:{
fprintf(stderr, "usage: %s [mode] [path]. \n For webcam mode=0, path is cam id; \n For image demo, mode=1, path=xxx/xxx/*.jpg; \n For video, mode=2; \n For benchmark, mode=3 path=0.\n", argv[0]);
break;
}
}
}