Face detection and recognition comparison

Face Detection and Face Recognition

Edit: Ben Xiaogu 2022/10/17

1. Using opencv for face detection and face recognition

1.1 YuNet

The YuNet model based on the convolutional network was released in December 2018 and open sourced in March 2019. It is a powerful and lightweight face detection model. The face detection speed is very fast, and it can detect many difficult objects, such as occluded faces, side faces, etc. YuNet performs better than Haar feature-based cascade classifiers for face detection.

Download the YuNet model and code:

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

1.2 install git-lfs

Download and install git-lfs from https://git-lfs.github.com/

There are two methods:

(1) Install directly

(2) Click Download to download the installation package, find install.sh after decompression , and enter the following command in the terminal to install:

sudo ./install.sh

1.3 pull models

cd opencv_zoo
git lfs install
git lfs pull

1.4 Run YuNet face detection

cd opencv_zoo/models/face_detection_yunet/
python demo.py

Notice:

If you have the following problems, it should be that steps 2 and 3 have not been completed. Check it out. If you successfully complete steps 2 and 3, you will not have the following problems.

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 Run YuNet face recognition

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

2. Use Baidu AI interface for face detection and face comparison

2.1 Download python-aip

pip3 install baidu-aip

2.2 Face detection

Two calling methods:

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 Face comparison

Two calling methods:

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

Upload images in json format

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',
    }
])

method one:

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

Method Two:

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")

There are many functions in Baidu's face recognition, in addition to face recognition and human body analysis, you can check it yourself

Guess you like

Origin blog.csdn.net/weixin_51995147/article/details/127404815