0

I'm working on Face Detection in OpenCV(2.4.6) and python(2.7). I have a very simple code, but its not giving me the desired output.

This is my code:

import numpy as np
import cv2
cam = cv2.VideoCapture(0)
name = 'detect'
face_cascade = cv2.CascadeClassifier('C:\opencv\data\haarcascades\haarcascade_frontalface_default.xml')
cv2.namedWindow(name, cv2.WINDOW_AUTOSIZE)
while True:
    s, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(img, 1.3, 5)
    for (x,y,w,h) in faces:
        img = cv2.rectangle(gray,(x,y),(x+w,y+h),(255,0,0),2)
        cv2.imshow(name, img)    
    k = cv2.waitKey(0)
    if k == 27:
        cv2.destroyWindow(name)
    break

When I run this code, my webcam will start and it will give me a blank window like this

Then the webcam will turn off, and in the editor I will get an error as follows:

%run "D:/6th sem/1.OpenCV + Python/det.py"
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
C:\Users\HP\AppData\Local\Enthought\Canopy32\App\appdata\canopy-1.3.0.1715.win-x86\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
195             else:
196                 filename = fname
--> 197             exec compile(scripttext, filename, 'exec') in glob, loc
198     else:
199         def execfile(fname, *where):

D:\6th sem\1.OpenCV + Python\det.py in <module>()
  7 while True:
  8     s, img = cam.read()
 ----> 9     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 10     faces = face_cascade.detectMultiScale(img, 1.3, 5)
 11     #print s

error: ..\..\..\src\opencv\modules\imgproc\src\color.cpp:3402: error: (-215) scn == 3 || scn == 4

Any suggestions are welcome. Thanks in advance.

2
  • 1
    Your line: img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) will draw a rectangle in the image, but the return value will be None, so img changes to None and cannot be drawn. Try cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) Also, that cv2.imshow(name, img) must be outside the for loop Commented May 10, 2014 at 10:51
  • @VipulSharma I changed my code according to your suggestion, now like the cam is on and the frames are freezing. I'm using Enthought canopy for this, is it the problem with the IDE? Commented May 12, 2014 at 6:09

1 Answer 1

3

some webcams need a warmup time, and deliver empty frames on startup. you want to check for that.

also, who said , that cv2.rectangle returns anything ? where did you get that idea ? from SO ?

while cap.isOpened():
    s, img = cam.read()
    if s == None:
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5) #hmm, 5 required neighbours is actually a lot.
        for (x,y,w,h) in faces:
            cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) # if you want colors, don't paint into a grayscale...
        cv2.imshow(name, img)    
    k = cv2.waitKey(0)
    if k == 27:
        cv2.destroyWindow(name)
        break
Sign up to request clarification or add additional context in comments.

1 Comment

yes, cv2.rectangle does not return anything. I also found this same code somewhere in the official documentation of opencv few months ago but now I cannot find it where it was given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.