-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopSignDetectorForVideo.cc
115 lines (91 loc) · 3.12 KB
/
stopSignDetectorForVideo.cc
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
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/* Function Headers */
void detectAndDisplay(Mat frame);
/* Global Variables */
String stop_sign_cascade_name = "cascade.xml"; //Will need to modify this for path or put this code in the same direct
String test_video = "cropstopsignvid2.mp4";
CascadeClassifier stop_sign_cascade;
String window_name = "Capture - Stop Sign Detection";
int main(int argc, const char ** argv)
{
Mat stop_sign_img;
Mat empty_mat;
std::vector<Rect> stopSigns;
Mat frame_gray;
CvCapture* capture;
Mat frame;
//LOAD THE CASCADE
if(!stop_sign_cascade.load(stop_sign_cascade_name))
{
printf("--Error loading trained cascade\n");
return -1;
}
//FOR LOADING VIDEOS
VideoCapture cap(test_video);
if(!cap.isOpened())
{
printf("--Error loading video\n");
return -1;
}
//FOR LOADING PICTURES
//stop_sign_img = imread("infiniti.png");
//imshow("manhan", stop_sign_img);
//GRAB VIDEO FRAME
while(cap.read(frame))
{
//imshow("manhan", frame);
if(!frame.empty())
{
//detectAndDisplay(frame);
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
stop_sign_cascade.detectMultiScale(frame_gray, stopSigns, 1.1, 4, 0|CV_HAAR_SCALE_IMAGE, Size(50, 50));
for(std::vector<Rect>::size_type i = 0; i != stopSigns.size(); i++)
{
cv::Rect rect(stopSigns[i].x, stopSigns[i].y, stopSigns[i].width, stopSigns[i].height);
cv::rectangle(frame, rect, cv::Scalar(0, 255, 0), 12);
}
imshow("stopsigndetect", frame);
waitKey(1);
}
else
{
printf("-- No captured frame -- Break!\n");
break;
}
}
//detectAndDisplay(stop_sign_img);
//waitKey(0);
}
void detectAndDisplay(Mat frame)
{
std::vector<Rect> stopSigns;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
cout << "Size: " << stopSigns.size() << '\n';
stop_sign_cascade.detectMultiScale(frame_gray, stopSigns, 1.1, 3, 0|CV_HAAR_SCALE_IMAGE, Size(50, 50));
cout << "Size: " << stopSigns.size() << '\n';
//cout << stopSigns[0].x << " " << stopSigns[0].y << " " << stopSigns[0].width << " " << stopSigns[0].height << '\n';
//cv::Rect rect(stopSigns[0].x, stopSigns[0].y, stopSigns[0].width, stopSigns[0].height);
//cv::rectangle(frame, rect, cv::Scalar(0, 255, 0), 12);
for(std::vector<Rect>::size_type i = 0; i != stopSigns.size(); i++)
{
cv::Rect rect(stopSigns[i].x, stopSigns[i].y, stopSigns[i].width, stopSigns[i].height);
cv::rectangle(frame, rect, cv::Scalar(0, 255, 0), 12);
}
//cv::namedWindow("stopsigndetect", WINDOW_NORMAL);
imshow("stopsigndetect", frame); //1024, 768
//cv::resizeWindow("stopsigndetect", 1024, 768);
waitKey(0);
}
//TO BUILD: g++ `pkg-config --cflags --libs opencv` stopSignDetectorForVideo.cc
//TO MOVE TO CLASSIFIER FOLDER: mv a.out classifier/
///Volumes/Macintosh SD/arshadhusain/Desktop/Computer Vision and Autonomous Car Work/All Computer Vision Projects/traffic_signs/stop_sign_detection/opencv_cctraining_real/classifier