注意修改 --replace--
变为自己的路径和参数
实现功能
1.SDK 活体检测-照片
2.HTTP 人脸对比-图片
3.HTTP 活体检测-视频
4.HTTP 实名认证
5.HTTP 身份证与名字比对
pip install baidu-aip requests
import requests
from aip import AipFace
import base64
import json
class Start:
def __init__(self):
""" 你的 APPID AK SK """
self.APP_ID = '--replace--'
self.API_KEY = '--replace--'
self.SECRET_KEY = '--replace--'
self.access_token_info = {}
def live_check_img(self):
'''
活体检测-照片
url: https://ai.baidu.com/ai-doc/FACE/ek37c1qiz
'''
with open("--replace--", 'rb') as f:
base64_data = base64.b64encode(f.read()).decode()
print(base64_data)
client = AipFace(self.APP_ID,
self.API_KEY,
self.SECRET_KEY)
imageType = "BASE64"
""" 如果有可选参数 """
options = {}
options[
"face_field"] = "age,beauty,expression,gender,glasses,emotion,face_type,face_shape"
options["max_face_num"] = 1
options["face_type"] = "LIVE"
options["liveness_control"] = "LOW"
result = client.detect(base64_data, imageType, options)
for i in result:
print(i, result[i])
def face_contrast(self):
'''
人脸对比-图片
url: https://ai.baidu.com/ai-doc/FACE/Lk37c1tpf
'''
request_url = "--replace--", 'rb') as f:
base64_data_1 = base64.b64encode(f.read()).decode()
with open("--replace--", 'rb') as f:
base64_data_2 = base64.b64encode(f.read()).decode()
faceList = [{"image": base64_data_1, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"},
{"image": base64_data_2, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"}]
access_token = self.access_token_info['access_token']
request_url = f"""{request_url}?access_token={access_token}"""
headers = {'content-type': 'application/json'}
response = requests.post(request_url, json=faceList, headers=headers)
if response:
print(response.json())
def get_access_token_info(self):
'''
http请求获取access_token
'''
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = f"""https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={self.API_KEY}&client_secret={self.SECRET_KEY}"""
access_token_res = requests.get(host)
self.access_token_info = json.loads(access_token_res.text)
print(self.access_token_info, 'self.access_token_info')
def live_check_video(self):
'''
活体检测-视频
url: https://ai.baidu.com/ai-doc/FACE/lk37c1tag
'''
# 此接口主要用于生成随机码
faceliveness_headers = {'content-type': 'application/x-www-form-urlencoded'}
sessioncode_data = {
# 'type': '1'
}
sessioncode_req = requests.post(
url=f"""https://aip.baidubce.com/rest/2.0/face/v1/faceliveness/sessioncode?access_token={
self.access_token_info['access_token']}""",
headers=faceliveness_headers, data=sessioncode_data)
faceliveness_extract_json = json.loads(sessioncode_req.text)
print(faceliveness_extract_json)
if faceliveness_extract_json['err_no'] == 0:
faceliveness_result = faceliveness_extract_json['result']
with open("--replace--",
'rb') as f:
base64_data = base64.b64encode(f.read()).decode()
# print(base64_data)
verify_data = {
# 'type_identify': 'action',
'video_base64': base64_data,
'session_id': faceliveness_result['session_id'],
# 'lip_identify': 'COMMON'
}
verify_req = requests.post(
url=f"""https://aip.baidubce.com/rest/2.0/face/v1/faceliveness/verify?access_token={
self.access_token_info['access_token']}""",
headers=faceliveness_headers, data=verify_data)
print(verify_req.text)
def reality_check(self):
'''
实名认证
url: https://ai.baidu.com/ai-doc/FACE/7k37c1ucj
经常出现问题[{
'errCode':6,
'meg':'no permission to access data',
'info':'常见问题是有V3版本权限,调用的是v2版本接口;需要企业认证的,开通企业认证后一个小时左右即可使用。'
}]
'''
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/person/verify"
headers = {'content-type': 'application/json'}
with open("/Users/hayashishihime/Documents/pycharmProjects/outProjects/人脸识别/static/yu.jpg", 'rb') as f:
base64_data_1 = base64.b64encode(f.read()).decode()
data = {
"image": base64_data_1,
"image_type": "BASE64",
"face_type": "LIVE",
"quality_control": "LOW", # 图片质量控制
"liveness_control": "", # 活体检测控制
"id_card_number": "--replace--",
"name": "--replace--",
"spoofing_control": "LOW"
}
access_token = self.access_token_info['access_token']
request_url = f"""{request_url}?access_token={access_token}"""
print(request_url)
response = requests.post(request_url, json=data, headers=headers)
if response:
print(response.json())
def idcard_check(self):
'''
身份证与名字比对
url: https://ai.baidu.com/ai-doc/FACE/Tkqahnjtk
经常出现问题[{
'errCode':6,
'meg':'no permission to access data',
'info':'常见问题是有V3版本权限,调用的是v2版本接口;需要企业认证的,开通企业认证后一个小时左右即可使用。'
}]
'''
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/person/verify"
headers = {'content-type': 'application/json'}
data = {
"id_card_number": "--replace--",
"name": "--replace--",
}
access_token = self.access_token_info['access_token']
request_url = f"""{request_url}?access_token={access_token}"""
response = requests.post(request_url, data=data, headers=headers)
if response:
print(response.json())
def main(self):
# -------------------- SDK --------------------------
# self.live_check_img()
# -------------------- http --------------------------
self.get_access_token_info()
# 人脸对比-图片
# self.face_contrast()
# 活体检测-视频
# self.live_check_video()
# 实名认证
# self.reality_check()
# 身份证与名字比对
self.idcard_check()
if __name__ == '__main__':
start = Start()
start.main()