-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmain.cpp
178 lines (159 loc) · 3.55 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
#include <iostream>
#include<opencv2//opencv.hpp>
#include<math.h>
#include "yolov5.h"
#include "yolov5_seg.h"
#include "yolov5_onnx.h"
#include "yolov5_seg_onnx.h"
#include<time.h>
#define VIDEO_OPENCV //if define, use opencv for video.
using namespace std;
using namespace cv;
using namespace dnn;
template<typename _Tp>
int yolov5(_Tp& cls, Mat& img, string& model_path)
{
Net net;
if (cls.ReadModel(net, model_path, false)) {
cout << "read net ok!" << endl;
}
else {
return -1;
}
//生成随机颜色
vector<Scalar> color;
srand(time(0));
for (int i = 0; i < 80; i++) {
int b = rand() % 256;
int g = rand() % 256;
int r = rand() % 256;
color.push_back(Scalar(b, g, r));
}
vector<OutputSeg> result;
if (cls.Detect(img, net, result)) {
DrawPred(img, result, cls._className, color);
}
else {
cout << "Detect Failed!" << endl;
}
system("pause");
return 0;
}
template<typename _Tp>
int yolov5_onnx(_Tp& cls, Mat& img, string& model_path)
{
if (cls.ReadModel(model_path, false)) {
cout << "read net ok!" << endl;
}
else {
return -1;
}
//生成随机颜色
vector<Scalar> color;
srand(time(0));
for (int i = 0; i < 80; i++) {
int b = rand() % 256;
int g = rand() % 256;
int r = rand() % 256;
color.push_back(Scalar(b, g, r));
}
vector<OutputSeg> result;
if (cls.OnnxDetect(img, result)) {
DrawPred(img, result, cls._className, color);
}
else {
cout << "Detect Failed!" << endl;
}
system("pause");
return 0;
}
template<typename _Tp>
int video_demo(_Tp& cls, string& model_path)
{
vector<Scalar> color;
srand(time(0));
for (int i = 0; i < 80; i++) {
int b = rand() % 256;
int g = rand() % 256;
int r = rand() % 256;
color.push_back(Scalar(b, g, r));
}
vector<OutputSeg> result;
cv::VideoCapture cap(0);
if (!cap.isOpened())
{
std::cout << "open capture failured!" << std::endl;
return -1;
}
Mat frame;
#ifdef VIDEO_OPENCV
Net net;
if (cls.ReadModel(net, model_path, true)) {
cout << "read net ok!" << endl;
}
else {
cout << "read net failured!" << endl;
return -1;
}
#else
if (cls.ReadModel(model_path, true)) {
cout << "read net ok!" << endl;
}
else {
cout << "read net failured!" << endl;
return -1;
}
#endif
while (true)
{
cap.read(frame);
if (frame.empty())
{
std::cout << "read to end" << std::endl;
break;
}
result.clear();
#ifdef VIDEO_OPENCV
if (cls.Detect(frame, net, result)) {
DrawPred(frame, result, cls._className, color, true);
}
#else
if (cls.OnnxDetect(frame, result)) {
DrawPred(frame, result, cls._className, color, true);
}
#endif
int k = waitKey(10);
if (k == 27) { //esc
break;
}
}
cap.release();
system("pause");
return 0;
}
int main() {
string img_path = "./images/zidane.jpg";
string seg_model_path = "./models/yolov5s-seg.onnx";
string detect_model_path = "./models/yolov5s.onnx";
Mat img = imread(img_path);
Yolov5 task_detect;
Yolov5Seg task_segment;
Yolov5Onnx task_detect_onnx;
Yolov5SegOnnx task_segment_onnx;
Mat temp = img.clone();
yolov5(task_detect, temp, detect_model_path); //Opencv detect
//temp = img.clone();
//yolov5(task_segment, temp, seg_model_path); //opencv segment
//temp = img.clone();
//yolov5_onnx(task_detect_onnx, temp, detect_model_path); //onnxruntime detect
//temp = img.clone();
//yolov5_onnx(task_segment_onnx, temp, seg_model_path); //onnxruntime segment
#ifdef VIDEO_OPENCV
video_demo(task_detect, detect_model_path);
//video_demo(task_segment, seg_model_path);
#else
video_demo(task_detect_onnx, detect_model_path);
//video_demo(task_segment_onnx, seg_model_path);
#endif
return 0;
}