python 基于opencv和face_recognition的人脸识别

python 基于opencv和face_recognition的人脸识别

代码如下:

使用一个photos存放你需要识别的照片,注意一个人一张就行

然后通过下面代码注册用户,之后启动程序,就会调用摄像头进行识别了。

AddPhoto("发哥", "./photos/fage.jpg")

AddPhoto("华仔", "./photos/huazai.jpg")

python 复制代码
# 7人脸检测 和 8人脸识别:7只能检测到人脸,8还可以识别出是谁
import face_recognition
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont

known_face_names = []
known_face_encodings = []


def IsDuplicateName(name):
    if name in known_face_names:
        return True
    return False


def AddPhoto(name, filename):
    image = face_recognition.load_image_file(filename)
    # 用 128 维的向量表示 1 张人脸
    face_encoding = face_recognition.face_encodings(image)
    if len(face_encoding) != 1:
        return False
    known_face_encodings.insert(0, face_encoding[0])
    known_face_names.insert(0, name)
    return True


def PutCNText(image, strs, local, sizes, colour):
    """
    在 frame 中添加文字
    """
    cv2img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    pilimg = Image.fromarray(cv2img)
    draw = ImageDraw.Draw(pilimg)
    font = ImageFont.truetype("./simhei.ttf", sizes, encoding="utf-8")
    draw.text(local, strs, colour, font=font)
    return cv2.cvtColor(np.array(pilimg), cv2.COLOR_RGB2BGR)


def FaceRecognition(frame):
    # 尺寸缩放为原来的 1/4,参数 (0, 0) 原意表示输出图像的大小
    # 当指定为 (0, 0) 时,输出图像的大小会根据 fx 和 fy 参数进行计算
    # 缩小图像 4 倍是为了加速人脸检测过程
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # np.ascontiguousarray() 确保数组在内存中的存储是连续的
    rgb_small_frame = np.ascontiguousarray(small_frame[:, :, ::-1])

    # 得到检测到的人脸位置信息 face_locations
    face_locations = face_recognition.face_locations(rgb_small_frame)

    # 对 rgb_small_frame 中人脸进行编码,得到人脸向量 face_encodings
    # 这个编码信息将用于后续的人脸比对和识别
    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.4)
        # known_face_encodings 为列表,相同人脸为 True
        name = ""

        if True in matches:
            first_match_index = matches.index(True)
            name = known_face_names[first_match_index]
        else:
            name='未录入人员'

        face_names.append(name)

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4
        if  name =='未录入人员':
              cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
              frame = PutCNText(frame, name, (left + 6, bottom - 24), 20, (0, 255, 255))
        else:


            cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
            frame = PutCNText(frame, name, (left + 6, bottom - 24), 20, (255, 255, 255))

    return frame


if __name__ == "__main__":
    # 注册缓存 人名 和 人脸向量,用于后续人脸识别
    AddPhoto("发哥", "./photos/fage.jpg")
    AddPhoto("华仔", "./photos/huazai.jpg")



    video_capture = cv2.VideoCapture(0)
    # 设置视频帧的 宽度 和 高度
    # video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1024) # 3
    # video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 768) # 4
    # video_capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc("M", "J", "P", "G"))

    while video_capture.isOpened():
        # 读取 1 帧视频图像
        ret, frame = video_capture.read()
        # print("frame.shape:", frame.shape)  # frame.shape = (720, 1280, 3) 就是一张图片

        # 如果读取失败,进入下一循环
        if ret == False:
            continue

        frame = FaceRecognition(frame)
        cv2.imshow("Face Recognition", frame)

        # 退出条件
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    # 释放资源
    video_capture.release()
    cv2.destroyAllWindows()
相关推荐
牛奔5 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路8 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly20240610 小时前
Bootstrap 警告框
开发语言
2601_9491465311 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧11 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
KYGALYX11 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb010311 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder11 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
智驱力人工智能12 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
猫头虎12 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven