还是想测试一下效果,腾讯的早开始收费了……

0x00.前言

SDK包实在是在方便了,也不用自己去请求api了……

0x01.开工

地址,创建应用,应用名称我设的“人脸识别”,记录AppIDAPI KeySecret Key之后SDK包初始化需要用到
pip3 install baidu-aip

人脸库为了方便管理写的“年级名”

0x02.新建AipFace

详见文档

1
2
3
4
5
6
7
8
from aip import AipFace

""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'

client = AipFace(APP_ID, API_KEY, SECRET_KEY)

人脸检测的返回数据倒是可以存一下,虽然以后也不一定会用上,接口有限制的话就sleep(1)一下即可,代码不详细贴了。

0.03.人脸注册

真正意义上的人脸识别需要先人脸注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
dir = '***'

from aip import AipFace
""" 你的 APPID AK SK """
APP_ID = '***'
API_KEY = '***'
SECRET_KEY = '***'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
groupId = '2015'

""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()

""" 如果有可选参数 """
options = {}
options['action_type'] = 'replace'

if os.path.exists(dir):
dirs = os.listdir(dir)
for dirc in dirs:
if 'py' not in dirc:
print(dirc)
uid = dirc[:-4]
userInfo = ''
image = get_file_content()
client.addUser(uid, userInfo, groupId, image, options)
else :
print('dir not exists')

0.04.人脸识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
groupId = "group1,group2"

""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()

image = get_file_content('example.jpg')

""" 调用人脸识别 """
client.identifyUser(groupId, image);

""" 如果有可选参数 """
options = {}
options["ext_fields"] = "faceliveness"
options["user_top_num"] = 3

""" 带参数调用人脸识别 """
client.identifyUser(groupId, image, options)