Python实现人脸识别

face_recognition(face_recognition中文使用说明)号称是世界上最简单的开源人脸识别库,可以通过Python或命令行识别和操作人脸。本项目采用face_recognition开源库,利用网络摄像头(DroidCam,官网)实现人脸检测、人脸关键点提取、人脸识别对比、人脸识别之后在原图上标注姓名并语音播报识别成功后人的姓名,未识别的人实现报警提示。

1.安装dlib

dlib版本需要和Python版本相对应。由于我的Python版本是3.8.10,所以我去搜寻dlib-cp38。dlib版本如下:
dlib版本
如我将whl文件保存在 D:\data 中,则在命令行输入:

pip install D:\data\dlib-19.19.0-cp38-cp38-win_amd64.whl

1.安装face_recognition

使用pip安装(使用了国内镜像源),在命令行输入:

pip install face_recognition -i https://pypi.douban.com/simple
import face_recognition
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont

import win32com.client as win
import winsound
import time

video_capture = cv2.VideoCapture(0)

hxh_image = face_recognition.load_image_file("./photo/张三.jpg")
hxh_face_encoding = face_recognition.face_encodings(hxh_image)[0]

hmy_image = face_recognition.load_image_file("./photo/王五.jpg")
hmy_face_encoding = face_recognition.face_encodings(hmy_image)[0]

hyx_image = face_recognition.load_image_file("./photo/李四.jpg")
hyx_face_encoding = face_recognition.face_encodings(hyx_image)[0]

zcy_image = face_recognition.load_image_file("./photo/赵四.jpg")
zcy_face_encoding = face_recognition.face_encodings(zcy_image)[0]

wq_image = face_recognition.load_image_file("./photo/小明.jpg")
wq_face_encoding = face_recognition.face_encodings(wq_image)[0]

known_face_encodings = [
    hxh_face_encoding,
    hmy_face_encoding,
    hyx_face_encoding,
    zcy_face_encoding,
    wq_face_encoding

]
known_face_names = [
    "张三",
    "王五",
    "李四",
    "赵四",
    "小明"
]

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

def addText(img, text, left, bottom):

    if (isinstance(img, np.ndarray)):
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))

    draw = ImageDraw.Draw(img)

    fontStyle = ImageFont.truetype("./font/SIMYOU.TTF", 20, encoding="utf-8")

    draw.text((left + 40, bottom + 25), text, (255, 255, 255), font=fontStyle)

    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

def show_landmarks(imge, landmarks):
    for landmarks_dict in landmarks:
        for landmarks_key in landmarks_dict.keys():
            for point in landmarks_dict[landmarks_key]:
                cv2.circle(imge, point, 2, (0, 0, 255), -1)
    return imge

while True:

    ret, frame = video_capture.read()

    if process_this_frame:

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

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

        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)
            name = "非法闯入"

            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)

            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]

            face_names.append(name)

        face_marks = face_recognition.face_landmarks(frame, None, "large")

        frame = show_landmarks(frame, face_marks)
    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, 0, 255), 2)

        frame = addText(frame, name, left, bottom)

        speak = win.Dispatch("SAPI.SpVoice")
        if(name == "非法闯入"):
            speak.Speak(name)
            time.sleep(1)
            winsound.Beep(frequency=1440, duration=1000)
        else:
            speak = win.Dispatch("SAPI.SpVoice")
            speak.Speak(name)
            speak.Speak("欢迎回家")

    cv2.imshow('Video', frame)

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

video_capture.release()
cv2.destroyAllWindows()

本项目为新手学习的一点记录,不足之处请各路大神指点!

Original: https://blog.csdn.net/qq_36666864/article/details/126643994
Author: Yellow Small Tiger
Title: Python实现人脸识别

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/717255/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球