【英特尔杯记录】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()

口罩识别

import paddlehub as hub
import cv2

mask_detector = hub.Module(name="pyramidbox_lite_server_mask")
def maskknow(img):
    result = mask_detector.face_detection(images=img)
    for i in range(len(result[0]['data'])):
        if result[0]['data'][i]['label'] == 'NO MASK':
            cv2.putText(img,result[0]['data'][i]['label'],(result[0]['data'][i]['left']-20,result[0]['data'][i]['top']-25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        else:
            cv2.putText(img, result[0]['data'][i]['label'],
                        (result[0]['data'][i]['left'] - 20, result[0]['data'][i]['top'] - 25), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
                        (0, 255, 0), 2)
        cv2.rectangle(img,(result[0]['data'][i]['left']-20,result[0]['data'][i]['top']-20), (result[0]['data'][i]['right']+20,result[0]['data'][i]['bottom']+20),(200,200,40),2)
        return img
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值