#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Python cv.HaarDetectObjects() cannot get return value


    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
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I have a usb camera that works upon testing, then tried devices 0 through 9 with your code. I presume frame is None.
    frame = cv.QueryFrame(capture)

    $ python p.py 0
    Press Esc to exit...
    Device Found
    inside while loop


    Anyway, if you have some ideas how to have cv recognize my camera on linux system I'll investigate the issue.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    thank you for testing my code.
    but using my laptop build in webcam is not work...

    is it need usb webcam or compile on linux system only can work?
    Sorry i forgot to mention, i'm using window 7 64 bit and build in webcam

IMN logo majestic logo threadwatch logo seochat tools logo