I recorded a video to test out the cascade classifier. I wrote a script as follow to record how many times the cascade classifier recognize my face in the short video.
import cv2
import numpy as np
face_cascade = cv2.CascadeClassifier('./data/cascade.xml')
cap = cv2.VideoCapture('/home/jianyepa/opencv-learning/output.avi')
count = 0
print cap.isOpened()
while 1:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h,x:x+w]
roi_color = frame[y:y+h,x:x+w]
count += 1
print 'face detected'
cv2.imshow('frame',frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
print count
cap.release()
cv2.destroyAllWindows()
Somehow, if I play the video to end, it will show the following output
True
face detected
face detected
face detected
OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor, file /home/jianyepa/opencv-3.1.0/modules/imgproc/src/color.cpp, line 7456 Traceback (most recent call last): File "cascade.py", line 11, in gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error: /home/jianyepa/opencv-3.1.0/modules/imgproc/src/color.cpp:7456: error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor
However, if I terminate the video before it end, the output is like expected
True
face detected
face detected
2
Can someone give some idea on what is happening? Thanks.
ret, frame = cap.read()
, check ifframe is not None
– ZdaRif frame is not None:
thencontinue
? – Jeff Pangcontinue
,print "Frame empty"
or maintain a counter of empty frames, etc. But it is very unlikely that you may get empty frame in middle of stream. – ZdaR