人脸检测识别对比

人脸检测与人脸识别

编辑:笨小古 2022/10/17

1. 利用opencv进行人脸检测与人脸识别

1.1 YuNet

基于卷积网络的YuNet模型与2018年12月发布,2019年3月开源。它是一个强大的、轻量级的人脸检测模型,人脸检测速度很快,并且可以检测很多较难检测的对象,如被遮挡的人脸、侧脸等。YuNet比基于Haar特征的级联分类器进行人脸检测的效果更好。

下载YuNet模型及代码:

git clone https://github.com/opencv/opencv_zoo.git

1.2 install git-lfs

从 https://git-lfs.github.com/ 下载安装git-lfs

有两种方法:

(1)直接Install

(2)点击Download下载安装包,解压后找到install.sh,在终端输入下面命令进行安装:

sudo ./install.sh

1.3 pull models

cd opencv_zoo
git lfs install
git lfs pull

1.4 运行YuNet人脸检测

cd opencv_zoo/models/face_detection_yunet/
python demo.py

注意:

如果有下面问题,应该是第2、3两步没有完成,检查一下,成功完成2、3两步是不会有下面这个问题的

cv2.error: OpenCV(4.6.0) /io/opencv/modules/dnn/src/onnx/onnx_importer.cpp:260: error: (-210:Unsupported format or combination of formats) Failed to parse ONNX model: face_detection_yunet_2022mar.onnx in function 'ONNXImporter'

1.5 运行YuNet人脸识别

cd opencv_zoo/models/face_recognition_sface/
python demo.py --input1 first.jpg --input2 second.jpg

2.利用百度AI接口进行人脸检测与人脸对比

2.1 下载python-aip

pip3 install baidu-aip

2.2 人脸检测

两种调用方法:

https://ai.baidu.com/ai-doc/FACE/ek37c1qiz#%E4%BA%BA%E8%84%B8%E6%A3%80%E6%B5%8B

https://ai.baidu.com/ai-doc/FACE/yk37c1u4t

image = "取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串"

imageType = "BASE64"

""" 调用人脸检测 """
client.detect(image, imageType);

""" 如果有可选参数 """
options = {}
options["face_field"] = "age"
options["max_face_num"] = 2
options["face_type"] = "LIVE"
options["liveness_control"] = "LOW"

""" 带参数调用人脸检测 """
client.detect(image, imageType, options)

2.3 人脸对比

两种调用方法:

https://ai.baidu.com/ai-doc/FACE/ek37c1qiz#%E4%BA%BA%E8%84%B8%E5%AF%B9%E6%AF%94

https://ai.baidu.com/ai-doc/FACE/Lk37c1tpf

图片通过json格式上传

result = client.match([
    {
        'image': base64.b64encode(open('1.jpg', 'rb').read()),
        'image_type': 'BASE64',
    },
    {
        'image': base64.b64encode(open('2.jpg', 'rb').read()),
        'image_type': 'BASE64',
    }
])

方法一:

from aip import AipFace
import base64


class FaceRecognition:
    """https://ai.baidu.com/ai-doc/FACE/ek37c1qiz"""

    def __init__(self):
        self.app_id = '27913782'
        self.api_key = 'PqNtUCkOdhPb5I5jbGxaT5qz'
        self.secret_key = '8vbTysHrBBoRphukZP9i69CQsMtlv5Nt'
        self.client = AipFace(self.app_id, self.api_key, self.secret_key)
        # super(FaceRecognition, self).__init__()

    def face_run(self):
        image1 = r'/home/sundawn/photo/face3.jpg'
        image2 = r'/home/sundawn/photo/photo.jpg'
        outcome = self.client.match([
            {
                'image': str(base64.b64encode(open(image1, 'rb').read()),'utf-8'),
                'image_type': 'BASE64',
            },
            {
                'image': str(base64.b64encode(open(image2, 'rb').read()),'utf-8'),
                'image_type': 'BASE64',
            }
        ])
        result = {}
        # if outcome['error_msg'] == 'SUCCESS':
        #     score = outcome['result']['score']
        #     print(score)
        # else:
        #     print('错误信息:', result['error_msg'])
        if outcome['error_code'] == 0:
            result = {}
            result['score'] = outcome['result']['score']
        return result

方法二:

import requests
import base64
import json

'''
人脸对比
'''
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/match"

image1 = r'/home/sundawn/photo/face3.jpg'
image2 = r'/home/sundawn/photo/face6.jpg'
# 二进制方式打开图片文件
f1 = open(image1, 'rb')
img1 = base64.b64encode(f1.read())        # base64编码
# print(img1)
f2 = open(image2, 'rb')
img2 = base64.b64encode(f2.read())        # base64编码
# print(img2)
params = json.dumps([
    {
        "image": str(img1, 'utf-8'),           # b --> str
        "image_type": "BASE64",
        "face_type": "LIVE",
        "quality_control": "LOW"
    },
    {
        "image": str(img2, 'utf-8'),
        "image_type": "BASE64",
        "face_type": "LIVE",
        "quality_control": "LOW"
    }
])

def get_access_token(AK='PqNtUCkOdhPb5I5jbGxaT5qz',SK='8vbTysHrBBoRphukZP9i69CQsMtlv5Nt'):

    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + AK + '&client_secret=' + SK
    
    response = requests.get(host)
    if response:
        # print(response.json())
        result = response.json()
        # print(result['refresh_token'])
        response_result = result['access_token']
    return response_result

# print(params, type(params))
access_token = get_access_token()
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    # print("response:", response.json())
    print(response.json()['result']['score'])
    if response.json()['result']['score'] > 90:
        print("same")

百度的人脸识别中的功能还有许多,除了人脸识别还有人体分析,可以自己去看看

猜你喜欢

转载自blog.csdn.net/weixin_51995147/article/details/127404815
今日推荐