forked from MuhanadNabeel/TrafficLight-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectDetection.cpp
143 lines (126 loc) · 4.15 KB
/
objectDetection.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
#include <opencv2/objdetect.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <string>
#include <sys/stat.h>
#include <cstdlib>
using namespace std;
using namespace cv;
/** Function Headers */
void detectAndDisplay(Mat& frame, int nr);
/** Global variables */
const cv::String WINDOW_NAME("Camera video");
/** place the xml in the home directory */
const cv::String CASCADE_FILE("/home/haar_xml_07_19.xml");
cv::CascadeClassifier *cascade_classifier;
cv::Mat frame, traffic_template;
cv::Mat trLightROI;
void showFrame(cv::Mat &frame)
{
cv::imshow(WINDOW_NAME, frame);
int c = waitKey(50);
if ((char)c == 30) { exit(0);}
}
/** Circle Detection after detecting a traffic light */
void DetectCircles(cv::Mat &traffic_template, int nr){
std::vector<cv::Vec3f> VectorCir;
std::vector<cv::Vec3f>::iterator iterCircles;
Mat resultImg;
//Save mat of detected circle in traffic lights
string circle_name = "circle";
string type_circle = ".jpg";
string folderNameCircle = "DetectedCircles";
stringstream ssfn_circle;
ssfn_circle << folderNameCircle << "/" << circle_name << (nr) << type_circle;
string fullpath_circle = ssfn_circle.str();
ssfn_circle.str("");
//Apply color map to search for certian color
applyColorMap(traffic_template,traffic_template,COLORMAP_SUMMER);
cv::inRange(traffic_template,cv::Scalar(0,90,90),cv::Scalar(204,255,255),resultImg);
cv::GaussianBlur(resultImg,resultImg,cv::Size(9,9),0.5,0.5);
cv::HoughCircles(resultImg,
VectorCir,
CV_HOUGH_GRADIENT,
2,
90,
50,
20,
4,
10);
for(iterCircles = VectorCir.begin(); iterCircles !=VectorCir.end(); iterCircles++) {
cv::circle(traffic_template,
cv::Point((int)(*iterCircles)[0],(int)(*iterCircles)[1]),
3,
cv::Scalar(255,0,0),
CV_FILLED);
cv::circle(traffic_template,cv::Point((int)(*iterCircles)[0],(int)(*iterCircles)[1]),
(int)(*iterCircles)[2],
cv::Scalar(0,0,255),
3);
imwrite(fullpath_circle,trLightROI);
}
// showing the the process of detecting circles
//imshow("processed",resultImg);
}
/** Detect trafficlights */
void detectAndDisplay(Mat& frame, int nr){
std::vector<cv::Rect> trLights;
Mat haar_detection;
string haar_name = "haar";
string type_haar = ".jpg";
string folderNameHaar = "DetectedHAAR";
stringstream ssfn_haar;
ssfn_haar <<folderNameHaar << "/" << haar_name << (nr) << type_haar;
string fullpath_haar = ssfn_haar.str();
ssfn_haar.str("");
//-- Detect TrafficLights through cascade classifier
cascade_classifier->detectMultiScale(frame, trLights, 1.1, 0, CV_HAAR_SCALE_IMAGE | CV_HAAR_FEATURE_MAX, Size(24, 24));
frame.copyTo(traffic_template);
for (size_t i = 0; i < trLights.size(); i++){
Rect trLights_i = trLights[i];
haar_detection = frame(trLights_i);
trLightROI = traffic_template(trLights[i]);
imwrite(fullpath_haar,haar_detection);
}
waitKey(10);
if(trLights.empty()){
return;
}
DetectCircles(trLightROI, nr);
//Use if you want to see the frame
//imshow("show", trLightROI);
}
int main(void){
//Create folders for the detected trafficlights
const int dir_Circle = mkdir("DetectedCircles", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
const int dir_Haar = mkdir("DetectedHAAR", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
//Capture function for the camera 0 or 1 works
VideoCapture capture(0);
// Calling the xml trained haar
cascade_classifier = new cv::CascadeClassifier(CASCADE_FILE);
// counter for the saved MATs
int nr = 0;
if(!capture.isOpened()){
printf("--(!)Error opening video capture\n");
return -1;
}
if (cascade_classifier->empty()){
std::cout << "Error creating cascade classifier. Make sure the file \n"
"\t" << CASCADE_FILE << "\n"
"is in working directory.\n";
exit(1);
}
while (true){
//while the frames are running start the detection
capture >> frame;
detectAndDisplay(frame,nr);
//Show the original frame
//showFrame(frame);
nr++;
}
delete cascade_classifier;
return 0;
}