0

I wrote and looked over this code multiple times and it seems right to me especially that it's very simple and straight forward (Took snippet out of opencv tutorial), however it is still giving me an error. I've checked the error and I understand that it's missing channels, however, I don't know how to fix it. Any help would be appreciated. I'm using Windows x64 and Spyder (from Anaconda distribution) as my IDE. Could it be hardware issue? (webcam)

    #####################################################
    #               Face Detection (Video)              # 
    #####################################################

    import numpy as np
    import cv2

    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

    video = cv2.VideoCapture(0)
    ret, frame = video.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]
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)


        cv2.imshow('Video', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video.release()
    cv2.destroyAllWindows()

Here's the error I get:

error: ..\..\..\..\opencv\modules\imgproc\src\color.cpp:5731:
error: (-215)    scn == 3 || scn == 4 in function cv::cvtColor
Shideh
  • 117
  • 2
  • 10
  • the image is empty, it could not read from your capture. please check `ret` and `video.isOpened()`. then again i can already see the next prob coming, so please also check `face_cascade.isOpened()` .... – berak Feb 12 '15 at 14:30
  • @berak You're right. It's not reading the video. Any idea on how to fix it? print video.read() (False, None – Shideh Feb 12 '15 at 14:42
  • some older webcams need a warmup, and deliver empty frames on startup, so, if the capture opened, you could try to read in a few dummy frames, and check again – berak Feb 12 '15 at 14:44
  • @berak I just realized you've answered this question multiple times on opencv answers too. lol Any way, so I added if not ret: continue after read and I see the webcam is on now but nothing's happening. (not sure how long it takes for it to warm up). – Shideh Feb 12 '15 at 15:05
  • @berak Sorry to bother you again, but do you have any idea why this code doesn't work? Now it's giving me the continue is not in the loop error. I added a while loop after read and still nothing. – Shideh Feb 17 '15 at 19:14

1 Answers1

4

i guess, you wanted a 'continuous detection, not a 'single shot' one, right ?

you're just some small changes away:

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
if face_cascade.empty(): raise Exception("your face_cascade is empty. are you sure, the path is correct ?")

eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
if eye_cascade.empty(): raise Exception("your eye_cascade is empty. are you sure, the path is correct ?")

video = cv2.VideoCapture(0)
while(video.isOpened()):
    ret, frame = video.read()
    if frame not None:
        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]
            eyes = eye_cascade.detectMultiScale(roi_gray)
            for (ex,ey,ew,eh) in eyes:
                cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
        cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video.release()
cv2.destroyAllWindows()
berak
  • 39,159
  • 9
  • 91
  • 89