在python中读取视频以进行人脸识别时出错

时间:2017-07-10 07:06:34

标签: python opencv

我是使用opencv的新手。我用我必须识别的图像训练我的模型,但是当我运行我的脚本来识别视频中的脸部时

recognizer = cv2.createLBPHFaceRecognizer()
recognizer.load('/home/atul/Desktop/Atul/face reg/Face-Recognition-
                master/trainer/trainer.yml')
cascadePath = "/home/atul/Desktop/Atul/face reg/Face-Recognition-
master/Classifiers/face.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
path = '/home/cdi/Downloads/training_data/Dhoni'
cam = cv2.VideoCapture('/home/atul/Downloads/M.S Dhoni.mp4')
font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) #Creates a font
while True:
    ret, im = cam.read()
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    faces=faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE)
    for(x,y,w,h) in faces:
        nbr_predicted= recognizer.predict(gray[y:y+h,x:x+w])
        cv2.rectangle(im,(x-50,y-50),(x+w+50,y+h+50),(225,0,0),2)
        if(nbr_predicted==7):
             nbr_predicted='Obama'
        elif(nbr_predicted==2):
             nbr_predicted='Dhoni'
        cv2.cv.PutText(cv2.cv.fromarray(im),str(nbr_predicted)+"--"+str(conf), (x,y+h),font, 255) #Draw the text
        cv2.imshow('im',im)
        cv2.waitKey(10)

它给了我错误。

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /home/travis/miniconda/conda-bld/work/opencv-2.4.11/modules/imgproc/src/color.cpp, line 3739
Traceback (most recent call last):
  File "detector.py", line 16, in <module>
    gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
cv2.error: /home/travis/miniconda/conda-bld/work/opencv 2.4.11/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor

帮我解决这个问题我正在使用ubuntu 16.04和opencv 2.4.11。 感谢

2 个答案:

答案 0 :(得分:0)

尝试像这样修改你的脚本:

if ret is True: 
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
else:
  continue

答案 1 :(得分:0)

添加异常处理是一件好事。



    ################# added ####################    
    # create window
    # but Maybe you did.
    cv2.namedWindow('im')
    ############################################

    recognizer = cv2.createLBPHFaceRecognizer()
    recognizer.load('/home/atul/Desktop/Atul/face reg/Face-Recognition-
                    master/trainer/trainer.yml')
    cascadePath = "/home/atul/Desktop/Atul/face reg/Face-Recognition-
    master/Classifiers/face.xml"
    faceCascade = cv2.CascadeClassifier(cascadePath);
    path = '/home/cdi/Downloads/training_data/Dhoni'
    cam = cv2.VideoCapture('/home/atul/Downloads/M.S Dhoni.mp4')

    ################# added ####################
    # check the CAM
    if cam.isOpened() == False: 
        print ('Can\'t open the CAM')
        exit()
    ############################################    

    font = cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0, 1, 1) #Creates a font

    while True:
        ret, im = cam.read()

        ################# added ####################    
        # if im is null break
        if im is None:
            break
        ############################################    
        gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
        faces=faceCascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5, minSize=(100, 100), flags=cv2.CASCADE_SCALE_IMAGE)
        for(x,y,w,h) in faces:
            nbr_predicted= recognizer.predict(gray[y:y+h,x:x+w])
            cv2.rectangle(im,(x-50,y-50),(x+w+50,y+h+50),(225,0,0),2)
            if(nbr_predicted==7):
                 nbr_predicted='Obama'
            elif(nbr_predicted==2):
                 nbr_predicted='Dhoni'
            cv2.cv.PutText(cv2.cv.fromarray(im),str(nbr_predicted)+"--"+str(conf), (x,y+h),font, 255) #Draw the text
            cv2.imshow('im',im)
            cv2.waitKey(10)

    ################# added ####################
    #close cam handle 
    cam.release()

    #close window of all 
    cv2.destroyAllWindows()
    ############################################