Skip to content

Commit 0629f33

Browse files
committed
parameter scaling for detecting smaller faces
1 parent af9f629 commit 0629f33

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

main.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ int main (int argc, const char * argv[])
4545
cap >> frame;
4646
if (frame.empty())
4747
break;
48+
int height;
49+
height=((frame.size().height)*800)/frame.size().width;
50+
resize(frame, frame, Size(800, height));
4851
frame1=detect_people(frame);
4952
faces=detect_faces(frame);
5053
frame2=draw_faces(frame1, faces); /*draw circle around faces*/
5154
label=recognize_face(frame,faces);
5255
put_label_on_face(frame,faces,label);
53-
imshow("human_detection and face_detction", frame);
56+
imshow("human_detection and face_detection", frame);
5457
waitKey(1);
5558
}
5659
return 0;
@@ -60,9 +63,8 @@ Mat detect_people( Mat frame)
6063
{
6164
HOGDescriptor hog;
6265
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
63-
/*resize(frame, frame, Size(),0.5,0.5);*/
6466
vector<Rect> detected, detected_filtered;
65-
hog.detectMultiScale(frame, detected, 0, Size(8,8), Size(32,32), 1.05, 2);
67+
hog.detectMultiScale(frame, detected, 0, Size(8,8), Size(16,16), 1.06, 2);
6668
size_t i, j;
6769
/*checking for the distinctly detected human in a frame*/
6870
for (i=0; i<detected.size(); i++)
@@ -82,7 +84,7 @@ Mat detect_people( Mat frame)
8284
r.width = cvRound(r.width*0.8);
8385
r.y += cvRound(r.height*0.07);
8486
r.height = cvRound(r.height*0.8);
85-
rectangle(frame, r.tl(), r.br(), Scalar(0,255,0), 3);
87+
rectangle(frame, r.tl(), r.br(), Scalar(0,0,255), 2);
8688
}
8789

8890
return frame;
@@ -93,11 +95,9 @@ vector<Rect> detect_faces( Mat frame)
9395
vector<Rect> faces;
9496
Mat frame_gray;
9597
cvtColor( frame, frame_gray, COLOR_BGR2GRAY ); /*converting input image in grayscale form*/
96-
equalizeHist( frame_gray, frame_gray );
97-
98-
/*resize(frame_gray, frame_gray, Size(),0.5,0.5);*/
98+
//equalizeHist( frame_gray, frame_gray );
9999
/*Detecting faces*/
100-
face_cascade1.detectMultiScale( frame_gray, faces, 1.2, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
100+
face_cascade1.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(20, 20) );
101101
return faces;
102102
}
103103

@@ -106,7 +106,7 @@ Mat draw_faces(Mat frame1, vector<Rect> faces)
106106
for ( size_t i = 0; i < faces.size(); i++ )
107107
{
108108
/*Drawing rectangle around faces*/
109-
rectangle(frame1, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 250, 255), 2, LINE_8, 0);
109+
rectangle(frame1, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height), Scalar(0, 255, 0), 2, LINE_8, 0);
110110
}
111111
return frame1;
112112

@@ -121,8 +121,8 @@ int* recognize_face(Mat frame, vector<Rect> faces)
121121
Mat frame_original_grayscale;
122122
for ( size_t i = 0; i < faces.size(); i++ )
123123
{
124-
cv::cvtColor( frame, frame_original_grayscale, COLOR_BGR2GRAY ); /*converting frame to grayscale*/
125-
equalizeHist(frame_original_grayscale,frame_original_grayscale);
124+
cvtColor( frame, frame_original_grayscale, COLOR_BGR2GRAY ); /*converting frame to grayscale*/
125+
//equalizeHist(frame_original_grayscale,frame_original_grayscale);
126126

127127
/*recognizing faces to predict label and confidence factor*/
128128
recognizer->predict(frame_original_grayscale, a,b);
@@ -144,7 +144,7 @@ Mat put_label_on_face(Mat frame,vector<Rect> faces,int* label)
144144
string str_label = ss.str();
145145
/*writing label on the image frame*/
146146
/*putText(InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false )*/
147-
putText(frame, str_label, Point(faces[j].x, faces[j].y), FONT_HERSHEY_SIMPLEX,1, Scalar(0,0,255), 2);
147+
putText(frame, str_label, Point(faces[j].x, faces[j].y), FONT_HERSHEY_SIMPLEX,1, Scalar(255,255,255), 2);
148148
}
149149
return frame;
150150
}

main.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def detect_people(frame):
2424
Returns:
2525
processed frame
2626
"""
27-
(rects, weights) = hog.detectMultiScale(frame, winStride=(4, 4), padding=(16, 16), scale=1.06)
27+
(rects, weights) = hog.detectMultiScale(frame, winStride=(8, 8), padding=(16, 16), scale=1.06)
2828
rects = non_max_suppression(rects, probs=None, overlapThresh=0.65)
2929
for (x, y, w, h) in rects:
3030
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
@@ -40,7 +40,7 @@ def detect_face(frame):
4040
Returns:
4141
coordinates of detected faces
4242
"""
43-
faces = face_cascade.detectMultiScale(frame)
43+
faces = face_cascade.detectMultiScale(frame, 1.1, 2, 0, (20, 20) )
4444
return faces
4545

4646

@@ -138,6 +138,7 @@ def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
138138
for video in list_of_videos:
139139
camera = cv2.VideoCapture(os.path.join(path, video))
140140
grabbed, frame = camera.read()
141+
print(frame.shape)
141142
frame_resized = imutils.resize(frame, width=min(800, frame.shape[1]))
142143
frame_resized_grayscale = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2GRAY)
143144
print(frame_resized.shape)
@@ -156,7 +157,7 @@ def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
156157
temp=background_subtraction(previous_frame, frame_resized_grayscale, min_area)
157158
if temp==1:
158159
frame_processed = detect_people(frame_resized)
159-
faces = detect_face(frame_resized)
160+
faces = detect_face(frame_resized_grayscale)
160161
if len(faces) > 0:
161162
frame_processed = draw_faces(frame_processed, faces)
162163
label = recognize_face(frame_resized, faces)
@@ -170,12 +171,13 @@ def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
170171
print("Time to process a frame: " + str(starttime-endtime))
171172
else:
172173
count=count+1
174+
print("Number of frame skipped in the video= " + str(count))
173175

174-
print("Number of frame skipped in the video= " + str(count))
176+
175177
camera.release()
176178
cv2.destroyAllWindows()
177179

178180

179181
else:
180182
print("model file not found")
181-
list_of_videos = []
183+
list_of_videos = []

0 commit comments

Comments
 (0)