Beispiel #1
0
def main():
    #paramaters
    WINDOW_NAME = ("WEBCAM")
    webcam = cv2.VideoCapture(0)
    frame = 0
    faceInit = []
    initFrames = 5
    nearRatio = 1.5
    farRatio = 0.7
    start = False
    state = 0  #face distance state
    previousX = 0
    previousY = 0
    previousW = 0
    previousH = 0
    #Read Shooting images
    while 1:
        #get face
        ret, webImage = webcam.read()
        faceIndex = face.faceDetection(webImage, previousX, previousY,
                                       previousW, previousH)

        cv2.imshow(WINDOW_NAME, webImage)

        if (faceIndex != None):

            #print(faceInit)
            if (start == False):  #init
                faceInit.append(faceIndex)
                if (len(faceInit) == initFrames):
                    original = compute.computeThreshold(np.asarray(faceInit))
                    start = True

            else:  #compute state
                faceArea = faceIndex[2] * faceIndex[3]
                state = compute.stateCompute(faceArea, state, original,
                                             nearRatio, farRatio)
            frame += 1

            previousX = faceIndex[0]
            previousY = faceIndex[1]
            previousW = faceIndex[2]
            previousH = faceIndex[3]

            yield state
        else:
            previousX = 0
            previousY = 0
            previousW = 0
            previousH = 0
            yield state

        k = cv2.waitKey(20) & 0xFF
        if k == 27:
            break

    #press ESC leave
    cv2.destroyAllWindows()
import numpy as np
import cv2
import os

import face as fr


test_img=cv2.imread(r'Test_Image.jpg')      #Give path to the image which you want to test


faces_detected,gray_img=fr.faceDetection(test_img)
#print("face Detected: ",faces_detected)


face_recognizer=cv2.face.LBPHFaceRecognizer_create()
face_recognizer.read(r'trainingData.json')  #Give path of where trainingData.yml is saved

name={0:"Sujit",1:"Roshan",2:"Neue"}             #Change names accordingly.  If you want to recognize only one person then write:- name={0:"name"} thats all. Dont write for id number 1. 

for face in faces_detected:
    (x,y,w,h)=face
    roi_gray=gray_img[y:y+h,x:x+h]
    label,confidence=face_recognizer.predict(roi_gray)
    print ("Confidence :",confidence)
    print("label :",label)
    fr.draw_rect(test_img,face)
    predicted_name=name[label]
    fr.put_text(test_img,predicted_name,x,y)

resized_img=cv2.resize(test_img,(1000,700))