Skip to content

Commit

Permalink
parameter scaling for detecting smaller faces
Browse files Browse the repository at this point in the history
  • Loading branch information
prsntmaurya committed May 30, 2017
1 parent af9f629 commit 0629f33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
24 changes: 12 additions & 12 deletions main.cpp
Expand Up @@ -45,12 +45,15 @@ int main (int argc, const char * argv[])
cap >> frame;
if (frame.empty())
break;
int height;
height=((frame.size().height)*800)/frame.size().width;
resize(frame, frame, Size(800, height));
frame1=detect_people(frame);
faces=detect_faces(frame);
frame2=draw_faces(frame1, faces); /*draw circle around faces*/
label=recognize_face(frame,faces);
put_label_on_face(frame,faces,label);
imshow("human_detection and face_detction", frame);
imshow("human_detection and face_detection", frame);
waitKey(1);
}
return 0;
Expand All @@ -60,9 +63,8 @@ Mat detect_people( Mat frame)
{
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
/*resize(frame, frame, Size(),0.5,0.5);*/
vector<Rect> detected, detected_filtered;
hog.detectMultiScale(frame, detected, 0, Size(8,8), Size(32,32), 1.05, 2);
hog.detectMultiScale(frame, detected, 0, Size(8,8), Size(16,16), 1.06, 2);
size_t i, j;
/*checking for the distinctly detected human in a frame*/
for (i=0; i<detected.size(); i++)
Expand All @@ -82,7 +84,7 @@ Mat detect_people( Mat frame)
r.width = cvRound(r.width*0.8);
r.y += cvRound(r.height*0.07);
r.height = cvRound(r.height*0.8);
rectangle(frame, r.tl(), r.br(), Scalar(0,255,0), 3);
rectangle(frame, r.tl(), r.br(), Scalar(0,0,255), 2);
}

return frame;
Expand All @@ -93,11 +95,9 @@ vector<Rect> detect_faces( Mat frame)
vector<Rect> faces;
Mat frame_gray;
cvtColor( frame, frame_gray, COLOR_BGR2GRAY ); /*converting input image in grayscale form*/
equalizeHist( frame_gray, frame_gray );

/*resize(frame_gray, frame_gray, Size(),0.5,0.5);*/
//equalizeHist( frame_gray, frame_gray );
/*Detecting faces*/
face_cascade1.detectMultiScale( frame_gray, faces, 1.2, 2, 0|CASCADE_SCALE_IMAGE, Size(30, 30) );
face_cascade1.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CASCADE_SCALE_IMAGE, Size(20, 20) );
return faces;
}

Expand All @@ -106,7 +106,7 @@ Mat draw_faces(Mat frame1, vector<Rect> faces)
for ( size_t i = 0; i < faces.size(); i++ )
{
/*Drawing rectangle around faces*/
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);
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);
}
return frame1;

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

/*recognizing faces to predict label and confidence factor*/
recognizer->predict(frame_original_grayscale, a,b);
Expand All @@ -144,7 +144,7 @@ Mat put_label_on_face(Mat frame,vector<Rect> faces,int* label)
string str_label = ss.str();
/*writing label on the image frame*/
/*putText(InputOutputArray img, const String& text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false )*/
putText(frame, str_label, Point(faces[j].x, faces[j].y), FONT_HERSHEY_SIMPLEX,1, Scalar(0,0,255), 2);
putText(frame, str_label, Point(faces[j].x, faces[j].y), FONT_HERSHEY_SIMPLEX,1, Scalar(255,255,255), 2);
}
return frame;
}
Expand Down
12 changes: 7 additions & 5 deletions main.py
Expand Up @@ -24,7 +24,7 @@ def detect_people(frame):
Returns:
processed frame
"""
(rects, weights) = hog.detectMultiScale(frame, winStride=(4, 4), padding=(16, 16), scale=1.06)
(rects, weights) = hog.detectMultiScale(frame, winStride=(8, 8), padding=(16, 16), scale=1.06)
rects = non_max_suppression(rects, probs=None, overlapThresh=0.65)
for (x, y, w, h) in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
Expand All @@ -40,7 +40,7 @@ def detect_face(frame):
Returns:
coordinates of detected faces
"""
faces = face_cascade.detectMultiScale(frame)
faces = face_cascade.detectMultiScale(frame, 1.1, 2, 0, (20, 20) )
return faces


Expand Down Expand Up @@ -138,6 +138,7 @@ def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
for video in list_of_videos:
camera = cv2.VideoCapture(os.path.join(path, video))
grabbed, frame = camera.read()
print(frame.shape)
frame_resized = imutils.resize(frame, width=min(800, frame.shape[1]))
frame_resized_grayscale = cv2.cvtColor(frame_resized, cv2.COLOR_BGR2GRAY)
print(frame_resized.shape)
Expand All @@ -156,7 +157,7 @@ def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
temp=background_subtraction(previous_frame, frame_resized_grayscale, min_area)
if temp==1:
frame_processed = detect_people(frame_resized)
faces = detect_face(frame_resized)
faces = detect_face(frame_resized_grayscale)
if len(faces) > 0:
frame_processed = draw_faces(frame_processed, faces)
label = recognize_face(frame_resized, faces)
Expand All @@ -170,12 +171,13 @@ def background_subtraction(previous_frame, frame_resized_grayscale, min_area):
print("Time to process a frame: " + str(starttime-endtime))
else:
count=count+1
print("Number of frame skipped in the video= " + str(count))

print("Number of frame skipped in the video= " + str(count))

camera.release()
cv2.destroyAllWindows()


else:
print("model file not found")
list_of_videos = []
list_of_videos = []

0 comments on commit 0629f33

Please sign in to comment.