简体   繁体   English

注册 face_recognition

[英]register for face_recognition

I'm trying to do an basic register for my face_recognition app using python我正在尝试使用 python 为我的 face_recognition 应用程序做一个基本的注册

#Here I'm trying to register faces more easier than need to add every face editing my script 

I need some way to add persons, register that and then can be recognized Any help is appreciated, thanks我需要一些方法来添加人员,注册然后可以识别任何帮助表示赞赏,谢谢

Thats all my code test, I'm trying to do whats my friend here say to I do but I new in python so I will need a more help.这就是我所有的代码测试,我正在尝试做我的朋友对我说的话,但我是 Python 新手,所以我需要更多帮助。

This is a face_recognition code, I'm studing python for aprimorate this code for help me in the university!这是一个face_recognition 代码,我正在学习python 为aprimorate 此代码以帮助我在大学! so I great for all yours helps所以我非常适合你的帮助

import face_recognition
import cv2
from PIL import Image


video_capture = cv2.VideoCapture(0)

image_of_patricia = face_recognition.load_image_file("./img/known/Patricia.jpg")
patricia_face_encoding = face_recognition.face_encodings(image_of_patricia)[0]

image_of_victor = face_recognition.load_image_file("./img/known/Victor.jpg")
victor_face_encoding = face_recognition.face_encodings(image_of_victor)[0]

image_of_henrique = face_recognition.load_image_file("./img/known/Henrique_.PNG")
henrique_face_encoding = face_recognition.face_encodings(image_of_henrique)[0]

# create array of encoddings and names

known_face_encodings = [
     patricia_face_encoding,
     victor_face_encoding,
     ricardo_face_encoding,
]

known_face_names = [
     "Patricia",
     "Victor",
     "Henrique",
     "Ricardo"
]

face_encodings = []
face_locations = []
faces_names = []
face_landmarks_list = []
process_this_frame = True


while True:
    ret, frame = video_capture.read()

    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    rgb_small_frame = small_frame[:, :, ::-1]

    if process_this_frame:
        face_locations = face_recognition.face_locations(rgb_small_frame,)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        face_names = []
        for face_encoding in face_encodings:
            matches = face_recognition.compare_faces(Known_face_encodings, face_encoding, tolerance=0.65)


            name = "Unknown"

            if True in  matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]


            face_names.append(name)

    process_this_frame = not process_this_frame



    for(top, right, bottom, left), name in zip(face_locations, face_names):

        top *= 4 
        right *= 4
        bottom *= 4
        left *= 4

        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)

        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 0), cv2.FILLED)
        #font = cv2.FONT_HERSHEY_DUPLEX
        #cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
        if name == "Victor":
            print("Victor")

        elif name == "Patricia":
            print("Patricia")

        elif name == "Ricardo":
            print("Ricardo")

        elif name == "Henrique":
            print("Henrique")
        elif name == "Desconhecido":
            #cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
            #cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
            #cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
            print("desconhecido")


    cv2.imshow("Reconhecimento Facial", frame)


    if cv2.waitKey(1) & 0xFF ==ord("q"):
        break


video_capture.release()

cv2.destroyAllWindows()

Following your code, you could do something like this:按照您的代码,您可以执行以下操作:

class KnowledgeBase:

    def __init__(self):
        self.names = []
        self.faces = []


    def register_face(self, name, img_path):
        image = face_recognition.load_image_file(img_path) 
        encoded_face = face_recognition.face_encodings(image)[0]
        self.names.append(name)
        self.faces.append(encoded_face)

Then you just need to instantiate a KnowledgeBase element and add new data to it.然后您只需要实例化一个 KnowledgeBase 元素并向其添加新数据。 This can be in a separate file if you want:如果需要,这可以在一个单独的文件中:

kb = KnowledgeBase()
kb.register_face('Patricia', './img/known/Patricia.jpg')
kb.register_face('Victor', './img/known/Victor.jpg')
kb.register_face('Henrique', './img/known/Henrique_.PNG')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM