Ask Your Question
0

Blue square not showing oh screen

asked 2015-01-18 18:06:24 -0600

Shaba1 gravatar image

updated 2015-01-20 22:29:19 -0600

berak gravatar image

Hello folk. I just started using opencv--python.

Intially I typed in some code found here.

link text

But when I typed in the code to my windows 7 64 bit computer and ran it. After a few syntax errors and typos on my part it ran just fine. I dectected the web camera that I have plugged in and it displayed the cameras video in a window. But I do not get that blue square that says it has detected my face. I am using a celron 2200 mhz with 2gb of ram so its not the fastest computer . Is it just that my computer is slow or is there something wrong with the code?

Ok here is the code. It was in the youtube video on the link that I posted,

import numpy as np
import cv2

#some comment i a foreign language which I cannot understand. I will put it
#thru Altavista later
face_cascade = cv2.CascadeClassifier('haarscascade_frontalface_default.xml')

video_capture = cv2.VideoCapture(0)

while True:
    ret, frame = video_capture.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #capture test with image.
    #img = cv2.imread("wyld2.jpg")
   # gray = cv2.cvtColor(img, 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)  

    cv2.imshow('video', frame)
    #some more foreign comments
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
video_capture.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

Post your code.

boaz001 gravatar imageboaz001 ( 2015-01-19 02:28:33 -0600 )edit
2

without seeing anything of your stuff, i bet a beer, that it did not find the xml file for the cascade, because the path was wrong.

please try like: cv2.CascadeClassifier('complete/path/to/haarscascade_frontalface_default.xml')

berak gravatar imageberak ( 2015-01-20 21:41:07 -0600 )edit
1

it's most probably in opencv/data/haarcascades

also:

if face_cascade.empty(): raise Exception("cascade file not found !")
berak gravatar imageberak ( 2015-01-21 02:19:21 -0600 )edit

Like @berak said, 98% of cascade errors result from not reading correctly the model and not capturing the return value!

StevenPuttemans gravatar imageStevenPuttemans ( 2015-01-21 04:37:20 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2015-01-21 21:14:20 -0600

Get the cascade and put it in the same directory.

wget https://jviolajones.googlecode.com/files/haarcascade_frontalface_default.xml

Now there was a typo in your code: haarScascade_frontalface_default.xml should be haarcascade_frontalface_default.xml

Change your code to this, make sure the indention is correct and it will work (make sure the cascade xml file is in the same directory):

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)

while True:
    ret, frame = video_capture.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)  
       print str(x)

   cv2.imshow('video', frame)
   if cv2.waitKey(1) & 0xFF == ord('q'):
     break
 video_capture.release()
 cv2.destroyAllWindows()

Tested with Python 2.7.6. This works on my machine. If that still does not work copy the code with static image face detection from here and see if that works.

edit flag offensive delete link more

Comments

YES!!!! Finally something works :) I do not understand HOW it works or what it is doing but that is a goal for further study. Thanks you very much all those who took the time to answer.

Shaba1 gravatar imageShaba1 ( 2015-01-23 19:39:19 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-01-18 18:06:24 -0600

Seen: 341 times

Last updated: Jan 21 '15