14
14
hog = cv2 .HOGDescriptor ()
15
15
hog .setSVMDetector (cv2 .HOGDescriptor_getDefaultPeopleDetector ())
16
16
recognizer = cv2 .face .createLBPHFaceRecognizer ()
17
-
17
+ count = 0
18
18
19
19
def detect_people (frame ):
20
20
"""
@@ -62,7 +62,7 @@ def recognize_face(frame_orginal, faces):
62
62
a , b = predict_tuple
63
63
predict_label .append (a )
64
64
predict_conf .append (b )
65
- print (predict_tuple )
65
+ print ("Predition label, confidence: " + str ( predict_tuple ) )
66
66
return predict_label
67
67
68
68
@@ -100,6 +100,26 @@ def put_label_on_face(frame, faces, labels):
100
100
i += 1
101
101
return frame
102
102
103
+ def background_subtraction (previous_frame , frame_resized_grayscale , min_area ):
104
+ """
105
+ This function returns 1 for the frames in which the area
106
+ after subtraction with previous frame is greater than minimum area
107
+ defined.
108
+ Thus expensive computation of human detection face detection
109
+ and face recognition is not done on all the frames.
110
+ Only the frames undergoing significant amount of change (which is controlled min_area)
111
+ are processed for detection and recognition.
112
+ """
113
+ frameDelta = cv2 .absdiff (previous_frame , frame_resized_grayscale )
114
+ thresh = cv2 .threshold (frameDelta , 25 , 255 , cv2 .THRESH_BINARY )[1 ]
115
+ thresh = cv2 .dilate (thresh , None , iterations = 2 )
116
+ im2 , cnts , hierarchy = cv2 .findContours (thresh .copy (), cv2 .RETR_EXTERNAL , cv2 .CHAIN_APPROX_SIMPLE )
117
+ temp = 0
118
+ for c in cnts :
119
+ # if the contour is too small, ignore it
120
+ if cv2 .contourArea (c ) > min_area :
121
+ temp = 1
122
+ return temp
103
123
104
124
if __name__ == '__main__' :
105
125
"""
@@ -117,27 +137,45 @@ def put_label_on_face(frame, faces, labels):
117
137
recognizer .load ("model.yaml" )
118
138
for video in list_of_videos :
119
139
camera = cv2 .VideoCapture (os .path .join (path , video ))
140
+ grabbed , frame = camera .read ()
141
+ frame_resized = imutils .resize (frame , width = min (800 , frame .shape [1 ]))
142
+ frame_resized_grayscale = cv2 .cvtColor (frame_resized , cv2 .COLOR_BGR2GRAY )
143
+ print (frame_resized .shape )
144
+
145
+ # defining min cuoff area
146
+ min_area = (3000 / 800 )* frame_resized .shape [1 ]
147
+
120
148
while True :
121
149
starttime = time .time ()
150
+ previous_frame = frame_resized_grayscale
122
151
grabbed , frame = camera .read ()
123
152
if not grabbed :
124
153
break
125
- frame_orginal = imutils .resize (frame , width = min (500 , frame .shape [1 ]))
126
- frame_orginal1 = cv2 .cvtColor (frame_orginal , cv2 .COLOR_BGR2GRAY )
127
- frame_processed = detect_people (frame_orginal1 )
128
- faces = detect_face (frame_orginal )
129
- if len (faces ) > 0 :
130
- frame_processed = draw_faces (frame_processed , faces )
131
- label = recognize_face (frame_orginal , faces )
132
- frame_processed = put_label_on_face (frame_processed , faces , label )
133
-
134
- cv2 .imshow ("window" , frame_processed )
135
- key = cv2 .waitKey (1 ) & 0xFF
136
- if key == ord ("q" ):
137
- break
154
+ frame_resized = imutils .resize (frame , width = min (800 , frame .shape [1 ]))
155
+ frame_resized_grayscale = cv2 .cvtColor (frame_resized , cv2 .COLOR_BGR2GRAY )
156
+ temp = background_subtraction (previous_frame , frame_resized_grayscale , min_area )
157
+ if temp == 1 :
158
+ frame_processed = detect_people (frame_resized )
159
+ faces = detect_face (frame_resized )
160
+ if len (faces ) > 0 :
161
+ frame_processed = draw_faces (frame_processed , faces )
162
+ label = recognize_face (frame_resized , faces )
163
+ frame_processed = put_label_on_face (frame_processed , faces , label )
164
+
165
+ cv2 .imshow ("Detected Human and face" , frame_processed )
166
+ key = cv2 .waitKey (1 ) & 0xFF
167
+ if key == ord ("q" ):
168
+ break
169
+ endtime = time .time ()
170
+ print ("Time to process a frame: " + str (starttime - endtime ))
171
+ else :
172
+ count = count + 1
173
+
174
+ print ("Number of frame skipped in the video= " + str (count ))
138
175
camera .release ()
139
176
cv2 .destroyAllWindows ()
140
- endtime = time .time ()
177
+
178
+
141
179
else :
142
180
print ("model file not found" )
143
181
list_of_videos = []
0 commit comments