Skip to content

Commit fbda52a

Browse files
committed
Added function for background subtraction
1 parent 9fd1877 commit fbda52a

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

main.py

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
hog = cv2.HOGDescriptor()
1515
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
1616
recognizer = cv2.face.createLBPHFaceRecognizer()
17-
17+
count=0
1818

1919
def detect_people(frame):
2020
"""
@@ -62,7 +62,7 @@ def recognize_face(frame_orginal, faces):
6262
a, b = predict_tuple
6363
predict_label.append(a)
6464
predict_conf.append(b)
65-
print(predict_tuple)
65+
print("Predition label, confidence: " + str(predict_tuple))
6666
return predict_label
6767

6868

@@ -100,6 +100,26 @@ def put_label_on_face(frame, faces, labels):
100100
i += 1
101101
return frame
102102

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
103123

104124
if __name__ == '__main__':
105125
"""
@@ -117,27 +137,45 @@ def put_label_on_face(frame, faces, labels):
117137
recognizer.load("model.yaml")
118138
for video in list_of_videos:
119139
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+
120148
while True:
121149
starttime = time.time()
150+
previous_frame = frame_resized_grayscale
122151
grabbed, frame = camera.read()
123152
if not grabbed:
124153
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))
138175
camera.release()
139176
cv2.destroyAllWindows()
140-
endtime = time.time()
177+
178+
141179
else:
142180
print("model file not found")
143181
list_of_videos = []

0 commit comments

Comments
 (0)