栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【英特尔杯记录】Python戴口罩人脸识别部分

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【英特尔杯记录】Python戴口罩人脸识别部分

博主参加英特尔杯后,主要完成机器视觉相关代码的编写,首先是人脸识别
采用Dlib作为人脸检测分类器,对于戴口罩人脸也有较好的识别效果,最终将人脸图像上传至百度云人脸库,通过调用百度API进行人脸识别
权重文件需要自己下载

import dlib         # 人脸识别的库 Dlib
import cv2          # 图像处理的库 OpenCV
import numpy as np
import json
import base64
import requests

import time
import os
#调取百度的access_token
get_token = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【找自己的】&client_secret=【找自己的】'
text = requests.get(get_token).json()
# 1. Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()

# 2. Dlib 人脸 landmark 特征点检测器
predictor = dlib.shape_predictor('/home/bj/PycharmProjects/baiduapiface/models/shape_predictor_68_face_landmarks.dat')

# 3. Dlib Resnet 人脸识别模型,提取 128D 的特征矢量
face_reco_model = dlib.face_recognition_model_v1("/home/bj/PycharmProjects/baiduapiface/models/dlib_face_recognition_resnet_model_v1.dat")
cap = cv2.VideoCapture(0)
count = 0
def process():
    global count
    res = 0
    flag, img_rd = cap.read()
    # 定义用户头应该放的区域
    cv2.circle(img_rd, (302, 239), 120, (200, 100, 10), thickness=10)
    k = cv2.waitKey(1)
    if count != 30:
        count += 1
    else:
        count = 0
        faces = detector(img_rd, 0)
        font = cv2.FONT_HERSHEY_SIMPLEX
        # 检测到人脸
        if len(faces) != 0:
            res=1
            # 矩形框 / Show the ROI of faces
            for k, d in enumerate(faces):
                # 计算矩形框大小 / Compute the size of rectangle box
                height = (d.bottom() - d.top())
                width = (d.right() - d.left())
                hh = int(height / 2)
                ww = int(width / 2)
                cv2.rectangle(img_rd,
                              tuple([d.left() - ww, d.top() - hh]),
                              tuple([d.right() + ww, d.bottom() + hh]),
                              (255, 255, 255), 2)
                flag, img_rd = cap.read()
                print("cheese!")
                cv2.imwrite("1.jpg", img_rd)
                return res

    cv2.namedWindow("camera", 1)
    cv2.imshow("camera", img_rd)
#人脸的录入
def face_in():
    API_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token="
    url = API_url + text['access_token']
    with open("1.jpg", "rb") as f:
        pic1 = f.read()
    ID = input("请输入编号:")
    #定义人脸录入的相关参数
    image_data = json.dumps(
        {"image": str(base64.b64encode(pic1), "utf-8"), "image_type": "BASE64", "group_id": "users",
         "user_id": ID,
         "quality_control": "NONE", "liveness_control": "NONE", "action_type": "APPEND"})
    response = requests.post(url, image_data).json()
    print(response)
    if(response['error_code'] == 0):
        print("成功录入!")
#人脸查询
def face_search():
    API_url = "https://aip.baidubce.com/rest/2.0/face/v3/search?access_token="
    url = API_url + text['access_token']
    with open("1.jpg", "rb") as f:
        pic1 = f.read()
    image_data = json.dumps(
        {"image": str(base64.b64encode(pic1), "utf-8"), "image_type": "BASE64", "group_id_list": "users",
         "quality_control": "NONE", "liveness_control": "NONE", "match_threshold": "80"})
    response = requests.post(url, image_data).json()
    print(response)
    if(response['error_code'] == 0):
        print("编号:"+str(response['result']['user_list'][0]['user_id']), "相似度:"+str(response['result']['user_list'][0]['score']))


def main():

    # face_in()
    while True:
        if process():
            face_search()
        else:
            process()


if __name__ == '__main__':
    main()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号