This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
orm_main.cpp
177 lines (137 loc) · 5.08 KB
/
orm_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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* 2018 Gal Zaidenstein.
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <thread>
#include <time.h>
#include <sys/time.h>
//OpenCV Headers
#include <opencv2/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/opencv.hpp>
#define OPENCV //Enable OpenCV for yolo
#include <yolo_v2_class.hpp> //yolo cpp wrapper
using namespace std;
using namespace cv;
struct timeval t1, t2;
int fps = 0;
unsigned int cone_num = 0;
VideoWriter video_out;
//draw aמ isosceles triangle on a using x,y,w,h values
void triangle(Mat mat_img,int x, int y, int w, int h , Scalar color){
line(mat_img,Point((x+w/2),(y)),Point((x),(y+h)),color,1,8,0);
line(mat_img,Point((x+w/2),(y)),Point((x+w),(y+h)),color,1,8,0);
line(mat_img,Point((x),(y+h)),Point((x+w),(y+h)),color,1,8,0);
}
void draw_cones(Mat mat_img, vector<bbox_t> result_vec, vector<string> obj_names, unsigned int wait_msec = 0) {
Scalar color(60, 160, 260);
Scalar fpscolor(215, 20, 107);
for (auto &i : result_vec) {
//basically counts cones
if(i.track_id>cone_num){
cone_num= i.track_id;
}
triangle(mat_img,i.x,i.y,i.w,i.h,color);
// rectangle(mat_img, Rect(i.x, i.y, i.w, i.h), color, 3);
// if(obj_names.size() > i.obj_id)
// putText(mat_img, obj_names[i.obj_id], Point2f(i.x, i.y - 10), FONT_HERSHEY_COMPLEX_SMALL, 1, color);
//tracking id
if(i.track_id > 0)
putText(mat_img, to_string(i.track_id), Point2f(i.x+i.w+5, i.y + i.h/2), FONT_HERSHEY_COMPLEX_SMALL, 0.7, color);
//only print up to 2 digits for probability
stringstream stream;
stream << fixed << setprecision(2) << i.prob;
string probString = stream.str();
putText(mat_img, probString, Point2f(i.x+5, (i.y + 15)+i.h), FONT_HERSHEY_COMPLEX_SMALL, 0.7, color);
}
putText(mat_img, "Cones: "+to_string(cone_num), Point2f(mat_img.cols-220, 22), FONT_HERSHEY_PLAIN, 2, fpscolor,2);
putText(mat_img, "FPS: "+to_string(fps), Point2f(0,22), FONT_HERSHEY_PLAIN , 2, fpscolor,2);
imshow("window name", mat_img);
waitKey(wait_msec);
}
//Print the results
void show_result(vector<bbox_t> const result_vec, vector<string> const obj_names) {
for (auto &i : result_vec) {
if (obj_names.size() > i.obj_id) cout << obj_names[i.obj_id] << " - ";
cout <<"FPS: "<<fps<< ", obj_id = " << i.obj_id<< ", tracking id = "<< i.track_id<< ", x = " << i.x << ", y = " << i.y
<< ", w = " << i.w << ", h = " << i.h
<< setprecision(3) << ", prob = " << i.prob << endl;
}
}
//parse object names
vector<string> objects_names_from_file(string const filename) {
ifstream file(filename);
vector<string> file_lines;
if (!file.is_open()) return file_lines;
for(string line; file >> line;) file_lines.push_back(line);
cout << "object names loaded \n";
return file_lines;
}
int main(void){
//Paths
string cfg_path = "yolov3-tiny-obj.cfg";
string weights_path = "yolov3-tiny-obj_3100.weights";
string names_path = "cones.names";
string file_path = "Test3.mp4";
//Confidence threshold
float confThreshold = 0.4;
//Setup output video
string output_file_path = file_path;
output_file_path.replace(output_file_path.end()-4, output_file_path.end(), "-yolo.avi");
//Initialize the detector
Detector detector(cfg_path,weights_path);
auto obj_names = objects_names_from_file(names_path);
int wait_ms = 1; //How long to wait before previewing each frame in [ms]
//Create a capture session
Mat frame;
Mat resized_frame;
detector.nms = 0.02;
//Capture each frame until we finish the video
VideoCapture cap(file_path);
//Open the output video
video_out.open(output_file_path, VideoWriter::fourcc('M','J','P','G'), 28, Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)));
//Start processing the frames
while(cap.isOpened()) {
cap >> frame;
if (frame.empty()){
cout << "Video completed !" << endl;
break;
}
//start timer
gettimeofday(&t1, NULL);
//detect objects in frames with threshold over confThreshold
vector<bbox_t> result_vec = detector.detect(frame, confThreshold);
result_vec = detector.tracking_id(result_vec); // comment it - if track_id is not required
//stop timer
gettimeofday(&t2, NULL);
//calculate fps
fps =1/((t2.tv_usec - t1.tv_usec)/pow(10,6)) ;
//draw the cones
draw_cones(frame, result_vec, obj_names, wait_ms);
// print results
show_result(result_vec, obj_names);
//write out to file
video_out.write(frame);
}
video_out.release();
return 0;
}