Ask Your Question

Revision history [back]

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.