吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4198|回复: 8
收起左侧

[Python 转载] python 实现人脸识别、以及语音提示识别结果

  [复制链接]
ljl9090 发表于 2021-9-29 21:43
import dlib
import numpy as np
import cv2
import pyttsx3
import time

def rect_to_bb(rect): # 获得人脸矩形的坐标信息
    x = rect.left()
    y = rect.top()
    w = rect.right() - x
    h = rect.bottom() - y
    return (x, y, w, h)

def shape_to_np(shape, dtype="int"): # 将包含68个特征的的shape转换为numpy array格式
    coords = np.zeros((68, 2), dtype=dtype)
    for i in range(0, 68):
        coords[i] = (shape.part(i).x, shape.part(i).y)
    return coords


def resize(image, width=1200):  # 将待检测的image进行resize
    r = width * 1.0 / image.shape[1]
    dim = (width, int(image.shape[0] * r))
    resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
    return resized


def feature(gray,detector,predictor,face_reco_model):

    rects = detector(gray, 1)
    shape = predictor(gray, rects[0])
    res_128 = face_reco_model.compute_face_descriptor(gray, shape)
    return res_128,rects[0]


def main(paths,cosis=True):
    detector = dlib.get_frontal_face_detector()
    predictor = dlib.shape_predictor("./data/data_dlib/shape_predictor_68_face_landmarks.dat")
    #人脸识别
    face_reco_model = dlib.face_recognition_model_v1("D:/download/windows/Dlib_face_recognition_from_camera-master/Dlib_face_recognition_from_camera-master/data/data_dlib/dlib_face_recognition_resnet_model_v1.dat")

    cap = cv2.VideoCapture(0)              # Get video stream from camera
    cap.set(3, 480)
    # try:
    if cap.isOpened():
        flag, img_rd = cap.read()

    # #人脸检测
    # cv2.imshow("faces",img_rd)
    # cv2.waitKey(0)
    img_rd = resize(img_rd, width=1200)
    # img_rd = cv2.imread("C:/Users/ljl/Pictures/0112.jpg")
    img_other_array,rect2 = feature(img_rd,detector,predictor,face_reco_model)
    cap.release()
    # except Exception as e:
    #     print(e)
    #初始化语音播报
    engine = pyttsx3.init()
    res_sim_dict = {}
    for name,path in paths.items():
        image = cv2.imread(path)
        image = resize(image, width=1200)
        img_array,rect1  = feature(image,detector,predictor,face_reco_model)
        A = np.array(img_array)
        B = np.array(img_other_array)
        if cosis:
            #余弦相似度
            org_new = np.dot(A,B)
            orginal = np.linalg.norm(A)
            new = np.linalg.norm(B)

            res_sim = org_new/(orginal*new)
            res_sim_dict[name] = res_sim
        else:
            #欧式距离相似度
            res_sim = np.sqrt(np.sum((A-B)**2))
            res_sim_dict[name] = 1/(1+res_sim)
            # res_sim_list.append(1/(1+res_sim))  #为了流程统一,取距离加一的倒数来度量 人脸的相似度
    # sorted(别名相似度字典.items(), key = lambda kv:(kv[1], kv[0]),reverse=True)
    res_sim_sort = sorted(res_sim_dict.items(),key=lambda x:x[1],reverse=True)[:1]
    for name,res_sim in res_sim_dict.items():
        print(res_sim)
        if name==res_sim_sort[0][0]:
            res_content = "{}".format(name)
        else:
            res_content = "检测未通过"
        engine.say(res_content)
        # 等待语音播报完毕
        engine.runAndWait()
    cv2.destroyAllWindows()
    return res_sim_dict

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
观鱼 + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

chunhwa 发表于 2021-9-30 00:12
谢谢分享。
明次 发表于 2021-10-12 14:01
 楼主| ljl9090 发表于 2021-10-12 17:49
明次 发表于 2021-10-12 14:01
还是用第三方api香

对的,自己使用resnet的训练效果不如人家已经训练好的,很多模型自己的训练结果往往比不了大厂的接口。
这里主要是对识别出来的参数,进行相似度计算以及语音提示等的封装。
z1989b01l04 发表于 2021-10-13 15:42
哇偶哇偶哇偶哇偶
gusong125 发表于 2021-10-18 11:19
哎没有数据集啊,只能看看!
 楼主| ljl9090 发表于 2021-10-18 12:33
gusong125 发表于 2021-10-18 11:19
哎没有数据集啊,只能看看!

其实打码运行可以准备一下,首先有摄像头,同时可以准备几张照片或者身份证照片都可以(不同人的)然后程序运行后,不要离开,摄像头打开后会采集你的人脸(注意光线),然后就会有语音提示是否检查通过。

[Python] 纯文本查看 复制代码
if __name__ == "__main__":
    # feature()
    path = {"纯阳三少":"C:/Users/ljl/Pictures/jxj.jpg","小李江湖":"C:/Users/ljl/Pictures/022.png","_安伟":"C:/Users/ljl/Pictures/0111.png","靓女":"C:/Users/ljl/Pictures/011.jpg"}
    main(path,cosis=False)
Rey2022 发表于 2022-11-17 11:33
ljl9090 发表于 2021-10-18 12:33
其实打码运行可以准备一下,首先有摄像头,同时可以准备几张照片或者身份证照片都可以(不同人的)然后程 ...

失败

失败
  

成功

成功

我试了一下 发现path路径不能有中文 不然回报错
 楼主| ljl9090 发表于 2022-12-25 18:49
Rey2022 发表于 2022-11-17 11:33
我试了一下 发现path路径不能有中文 不然回报错

这个是cv2 的问题,也有很多解决方案
image = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_COLOR)
# image = cv2.imread(path)
网上解决方案很多,由于测试没有问题也就没有管了。
大兄弟很不错,真的去实践运行了。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 11:35

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表