Baidu AI and face recognition: aip-python-sdk-2.2.15
Install and use the Python SDK as follows :
- If pip is installed,
pip install baidu-aip
just execute it. - If setuptools has been installed,
python setup.py install
just execute it.
from aip import AipOcr
from aip import AipFace
""" 你的 APPID AK SK """
APP_ID = 'XXXXXXXX'
API_KEY = 'XXXXXXXXXXXXXXXXXXXX'
SECRET_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
'''
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content(r'D:\study\python\temp\openCV\1.png')
""" 调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image);
""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
""" 带参数调用通用文字识别, 图片参数为本地图片 """
test01 = client.basicGeneral(image, options)
print(test01)
'''
'''
url = "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=4095383231,3213649836&fm=11&gp=0.jpg"
""" 调用通用文字识别, 图片参数为远程url图片 """
client.basicGeneralUrl(url);
""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"
""" 带参数调用通用文字识别, 图片参数为远程url图片 """
test02 = client.basicGeneralUrl(url, options)
print(test02)
'''
# 调用百度API完成人脸识别
import base64
import json
from aip import AipFace
img01 = r'D:\study\python\temp\openCV\5.png'
img02 = r'D:\study\python\temp\openCV\4.jpg'
def face_detect(filepath1,filepath2): #人脸对比
result = client.match([
{
'image': base64.b64encode(open(filepath1, 'rb').read()),
'image_type': 'BASE64',
},
{
'image': base64.b64encode(open(filepath2, 'rb').read()),
'image_type': 'BASE64',
}
])
#print(result) #打印出所有的信息
a=result['result']['score']
print(a) #单独显示出相似度 其他的类似
if a>80:
return 1
print('1')
else:
return 0
print('0')
face_detect(img01,img02)
'''
def face_search(filepath): #人脸库搜索 222207
with open(filepath, 'rb') as fp:
image=base64.b64encode(fp.read())
imageType="BASE64"
groupIdList="你的用户组名称"
result=client.search(image,imageType,groupIdList)
print(result) #打印出所有信息
# print(result['result']['user_list'][0]['score']) #打印出相似度其他信息类似
# print(result['result']['face_token'])
# print(result['error_code'])
def face_add(filepath,unit,num): #人脸库增加 地址 组 用户
with open(filepath,'rb') as fp:
image=image=base64.b64encode(fp.read())
imageType="BASE64"
groupid=unit
userid=num
result=client.addUser(image,imageType,groupid,userid)
if result['error_code']==0:
print("增加人脸成功")
else:
print("增加人脸失败")
# print(result)
def face_delete(filepath): #删除人脸库
userId = "用户名称"
groupId = "用户组名称"
result = client.deleteUser(groupId, userId); #其实这里不用按照官方的demo三个参数 每张照片单独的token不用也可以的!
print(result)
if result['error_code']==0:
print("删除人脸成功")
else:
print("删除人脸失败")
if __name__=='__main__':
# face_detect('你照片的路径','你另一张照片的路径')
# face_search('照片的路径')
# face_delete('照片的路径')
# face_add('照片的路径','用户组名称','用户名称')
'''
Modify Baidu face recognition SDK error
[Python] json error xx is not JSON serializable
cd to the sdk directory aip: find the face.py file and modify it:
'''尾部添加'''
class DateEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytes):
return str(obj, encoding='utf-8')
return json.JSONEncoder.default(self, obj)
Replace all: json.dumps(images, ensure_ascii=False ,cls=DateEncoder )