2

I have a code in python to recognize my face, but now I want to recognize my face and put my name, as stored in a database.

My question is, how to do it in python? anyone have any example?

This is the code:

import sys
import cv

class FaceDetect():
    def __init__(self):
        cv.NamedWindow ("CamDemo", 1)
        device = 0
        self.capture = cv.CaptureFromCAM(device)
        capture_size = (320,200)
        cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_WIDTH, capture_size[0])
        cv.SetCaptureProperty(self.capture, cv.CV_CAP_PROP_FRAME_HEIGHT, capture_size[1])

    def detect(self):
        cv.CvtColor(self.frame, self.grayscale, cv.CV_RGB2GRAY)

        # histograma
        cv.EqualizeHist(self.grayscale, self.grayscale)

        # detecta objetos
        faces = cv.HaarDetectObjects(image=self.grayscale, cascade=self.cascade, storage=self.storage, scale_factor=1.2,\
                                     min_neighbors=2, flags=cv.CV_HAAR_DO_CANNY_PRUNING)

        if faces:
            #print 'cara detectada!'
            for i in faces:
                if i[1] > 10:
                    cv.Circle(self.frame, ((2*i[0][0]+i[0][2])/2,(2*i[0][1]+i[0][3])/2), (i[0][2]+i[0][3])/4, (128, 255, 128), 2, 8, 0)

    def run(self):
        # verificar si el dispositivo de captura esta bien
        if not self.capture:
            print "Error opening capture device"
            sys.exit(1)

        self.frame = cv.QueryFrame(self.capture)
        self.image_size = cv.GetSize(self.frame)

        # create grayscale version
        self.grayscale = cv.CreateImage(self.image_size, 8, 1)

        # creando almacenamiento(storage)
        self.storage = cv.CreateMemStorage(128)
        self.cascade = cv.Load('haarcascade_frontalface_default.xml')

        while 1:
            # bucle infinito -> captura siempre
            # captura cara
            self.frame = cv.QueryFrame(self.capture)
            if self.frame is None:
                break

            # ventana
            cv.Flip(self.frame, None, 1)

            # deteccion de cara
            self.detect()

            # mostrar webcam imagen
            cv.ShowImage('CamDemo', self.frame)
            # gestionar eventos
            k = cv.WaitKey(10)

            if k == 0x1b: # ESC
                print ' Saliendo ...'
                break
                sys.exit(1)

if __name__ == "__main__":
    print "Esc para salir"
    face_detect = FaceDetect()
    face_detect.run() `
4

1 回答 1

4

I don't know if you figured it out but have a look at:

http://docs.opencv.org/trunk/modules/contrib/doc/facerec/tutorial/facerec_video_recognition.html

it is definately what you are looking for and also easy to implement. It's a "new" adition to the opencv project so you might have to update your opencv version.

Cheers

于 2013-06-16T03:06:38.407 回答