2020-09-22:Python:face_recognition人脸识别代码

首先安装库

pip install face_recognition

有少库的可以参考之前文章:文章地址

直接上代码:有注释,可以自行修改代码应用:

import face_recognition
import cv2
import os
import time


def get_encode(path):
    # 存储知道人名列表
    known_names = []
    # 存储知道的特征值
    known_encodings = []
    for image_name in os.listdir(path):
        load_image = face_recognition.load_image_file(os.path.join(path,image_name))  # 加载图片
        image_face_encoding = face_recognition.face_encodings(load_image)[0]  # 获得128维特征值
        known_names.append(image_name.split(".")[0])
        known_encodings.append(image_face_encoding)
    return known_encodings,known_names


def face(known_encodings,known_names):

    # 打开摄像头,0表示内置摄像头
    video_capture = cv2.VideoCapture(0)
    process_this_frame = True

    a = 0

    while True:
        ret, frame = video_capture.read()
        # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
        rgb_frame = frame[:, :, ::-1]
        if process_this_frame:
            start=time.time()
            face_locations = face_recognition.face_locations(rgb_frame)  # 获得所有人脸位置
            print("11111",time.time()-start)
            start1 = time.time()
            face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)  # 获得人脸特征值
            print("2222", time.time() - start1)
            face_names = []  # 存储出现在画面中人脸的名字
            for face_encoding in face_encodings:
                start2 = time.time()
                matches = face_recognition.compare_faces(known_encodings, face_encoding, tolerance=0.5)
                print("3333", time.time() - start2)
                name = "unknown"
                print(matches)
                if True in matches:
                    first_match_index = matches.index(True)
                    name = known_names[first_match_index]
                face_names.append(name)

        # if a > 100:
        #     process_this_frame = True
        #     a = 0
        # else:
        #     a += 1
        # process_this_frame = not process_this_frame

        # 将捕捉到的人脸显示出来
            for (top, right, bottom, left), name in zip(face_locations, face_names):
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)  # 画人脸矩形框
                # 加上人名标签
                cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    known_encodings,known_names = get_encode("./imgs/")  # 存放已知图像路径
    face(known_encodings,known_names)

欢迎讨论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值