Hello everybody, this is the coding use Haar to detect face from webcam.
My question is i cannot get return value from cv.HaarDetectObjects() method...
how to solve it ??
i struggle this problem few days already
Code:
import sys
import cv2.cv as cv
def detect(image):
image_size = cv.GetSize(image)
print image_size
#create grayscale version
grayscale = cv.CreateImage(image_size,8,1)
cv.CvtColor(image,grayscale,cv.CV_BGR2GRAY)
#create storage
print "Before Create Storage"
storage = cv.CreateMemStorage(0)
#cv.ClearMemStorage(storage)
#equlize histogram
print "Before Equlize histogram"
cv.EqualizeHist(grayscale,grayscale)
#detect objects
print "Before Detect Objects"
cascade = cv.Load('C:/OpenCV2.43/data/haarcascades/haarcascade_frontalface_alt.xml')
faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2,0,(20,20))
print "After Detect Objects"
if faces:
print'face detected'
for i in faces:
print i
cv.Rectangle(image,(i[0][0],1[0][1]),
(int(i[0][0] + i[0][2]), int(i[0][1] + i[0][3])),
cv.RGB(0,255,0),3,8,0)
if __name__ == "__main__":
#print "OpenCV version : %s (%d,%d,%d)" %(cv.CV_VERSION,
# cv.CV_MAJOR_VERSION,
# cv.CV_MINOR_VERSION,
# cv.CV_SUBMINOR_VERSION)
print "Press Esc to exit..."
#create windows
cv.NamedWindow('Camera',cv.CV_WINDOW_AUTOSIZE)
#create capture device
device = 0 # assume we want first device
capture = cv.CreateCameraCapture(0)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_WIDTH,640)
cv.SetCaptureProperty(capture,cv.CV_CAP_PROP_FRAME_HEIGHT,480)
# check capture device is ok
if not capture:
print"Error Opening Capture Device"
sys.exit(1)
else:
print "Device Found"
while True:
print "inside while loop"
frame = cv.QueryFrame(capture)
if frame is None:
break
#mirror
cv.Flip(frame,None,1)
print "before detect"
#face Detection
detect(frame)
print "after detect"
#display webcam image
cv.ShowImage('Camera',frame)
#handle events
k = cv.WaitKey(10)
if k == 0x1b: #ESC
print'ESC pressed. Exiting software...'
break