利用python进行人脸识别的基本开发(3)

经过前两篇入门级的介绍,不知道各位和我一样的小白,感受到py做人脸识别的乐趣没有?上一篇中的识别效果满意吗?答案是不满意啊!
通过费力搭建开发环境,发现识别效果差强人意。怎么办?

我们可以通过GITHUB来寻找相关的实验项目。

下面上一个可以调节识别阈值的代码段,大家看看会出现什么!!!

# -*- coding: utf-8 -*-
# 摄像头头像识别
import face_recognition
import cv2

from os import listdir

source = "×××××"  # 摄像头的rtsp地址
cam = cv2.VideoCapture(0)

filepath = 'C:/Users/Administrator\Desktop/recognitiongface1/face_photos'  # 已知人脸图片文件夹 注意 如果会员图片后缀不是jpg 需要进行修改
filename_list = listdir(filepath)
known_face_names = []
known_face_encodings = []
a = 0

for filename in filename_list:  # 依次读入列表中的内容
    a += 1
    if filename.endswith('jpg'):  # 后缀名'jpg'匹对
        known_face_names.append(filename[:-4])  # 把文件名字的后四位.jpg去掉获取人名
        file_str = filepath + '/' + filename
        a_images = face_recognition.load_image_file(file_str)
        # print(file_str)
        a_face_encoding = face_recognition.face_encodings(a_images)[0]
        known_face_encodings.append(a_face_encoding)
print(known_face_names, a)

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

while (cam.isOpened()):
    # 读取摄像头画面
    ret, frame = cam.read()
    if not ret:
        # 等同于 if ret is not none
        break

    # 改变摄像头图像的大小,图像小,所做的计算就少
    small_frame = cv2.resize(frame, (0, 0), fx=0.33, fy=0.33)

    # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # 根据encoding来判断是不是同一个人,是就输出true,不是为flase
        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:
            # 默认为unknown
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.38)
            # 阈值太低容易造成无法成功识别人脸,太高容易造成人脸识别混淆 默认阈值tolerance为0.6
            # print(matches)
            name = "Unknown"

            # if match[0]:
            #     name = "michong"
            # If a match was found in known_face_encodings, just use the first one.
            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):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        # 由于我们检测到的帧被缩放到1/4大小,所以要缩小面位置
        top *= 3
        right *= 3
        bottom *= 3
        left *= 3

        # 矩形框
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # 引入ft2中的字体
        # 加上标签
        cv2.rectangle(frame, (left, bottom - 20), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.8, (255, 255, 255), 1)

        # frame = ft.draw_text(frame,(left + 6, bottom - 6), name, 1.0, (255, 255, 255))
        # def draw_text(self, image, pos, text, text_size, text_color)
    # Display

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

cam.release()
cv2.destroyAllWindows()

这样之后呢?注意阅读代码注释,修改:
matches =face_recognition.compare_faces(known_face_encodings, face_encoding, tolerance=0.38)

是不是识别率发生变化了?注意、阈值设定太低的话,识别不出人脸哟。阈值太高的话、识别准确率很低的。

1、掏出你82年的手机,自拍一张大头贴,对准摄像头,看看能识别出来不?
2、上述方式也能识别的话,是不是有些儿戏,有些不严谨,有漏洞。。。
3、怎么办呢?预知详情,请听下回分解!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值