0
votes

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.

1
After ret, frame = cap.read(), check if frame is not NoneZdaR
@ZdaR Sorry, i cant get you, is it add if frame is not None: then continue ?Jeff Pang
It depends on you what you want to do if frame is none, you may continue, 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
@ZdaR checked, the frame is not empty. But error still existJeff Pang

1 Answers

0
votes

That happens because you are trying to perform operations on a frame that is not even available.

Just make this change-

while cap.isOpened():
        ret, frame = cap.read()
        if ret:
                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
        else: break

print count