在Python中的OpenCV。问:认出我的脸并说出我的名字。像数据库一样

时间:2013-04-22 11:21:21

标签: python database opencv face-recognition

我在python中有一个代码来识别我的脸,但现在我想识别我的脸并将我的名字存储在数据库中。

我的问题是,如何在python中执行此操作?有谁有任何例子?

这是代码:

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() `

1 个答案:

答案 0 :(得分:4)

我不知道你是否想出来但是看看:

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

它肯定是您正在寻找的并且易于实施。这是对opencv项目的“新”加入,因此您可能需要更新opencv版本。

干杯