第一步(创建API)
登录或者注册百度智能云,百度智能云官网地址,也可以直接搜“百度智能云”就OK了。登录成功后,在产品服务搜索人脸识别。在人脸识别产品页面创建自己的应用。
个人注册后的有免费的资源,如人脸搜索、人脸检测、人脸库管理等,其实这些功能就可以实现人脸认证了,各位根据自己需求选择。
创建完自己的应用,查看自己API信息
创建人脸库用户组
到这里就OK了
第二步(Python-SDK调用)
大家可以参考官方的技术文档,各类参数功能写得很详细——技术文档地址
先在终端下载baidu-aip
pip install baidu-aip
话不多说,放代码,哈哈
from aip import AipFace
import base64
# 百度智能云
class baiduyun:
def __init__(self):
# 百度智能云 APPID AK SK
self.APP_ID = '01' # 根据自己的API信息填
self.API_KEY = '01' # 根据自己的API信息填
self.SECRET_KEY = '01' # 根据自己的API信息填
self.client = AipFace(self.APP_ID, self.API_KEY, self.SECRET_KEY)
# 人脸检测
def face_detection(self, image):
image = image_to_base64(image)
imageType = "BASE64"
options = {}
options["face_field"] = "age,blur"
options["max_face_num"] = 10
options["face_type"] = "LIVE"
options["liveness_control"] = "NONE"
result = self.client.detect(image, imageType, options)
# print(result)
return result
# 人脸注册
def face_login(self, image, userId, groupId="test"):
result = self.face_detection(image)
image = image_to_base64(image)
if result["error_code"] == 0:
imageType = "BASE64"
options = {}
options["user_info"] = ""
options["quality_control"] = "NORMAL"
options["liveness_control"] = "NONE"
options["action_type"] = "REPLACE"
self.client.addUser(image, imageType, groupId, userId, options)
# 人脸搜索
def face_search(self, image, groupIdList="test"):
image = image_to_base64(image)
imageType = "BASE64"
options = {}
options["match_threshold"] = 70
options["quality_control"] = "NONE"
options["liveness_control"] = "NONE"
result = self.client.search(image, imageType, groupIdList, options)
return result
# 删除用户
def face_delete(self, userId, groupId='test'):
self.client.deleteUser(groupId, userId)
# 检测是否报错
def extract_params(self, result):
if result["error_code"] == 0:
return [result["result"]["user_list"]]
else:
print(result["error_code"], result["error_msg"])
return None
# 读取 JPEG 图像文件转base64格式
def image_to_base64(file_path):
with open(file_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
return encoded_string.decode('utf-8')
代码中需要自己的信息填入API_ID、API_KEY、SECRET_KEY、groupId。就可以当做一个人脸认证库使用了。只需要实例类,就可以调用其中的人脸注册、人脸搜索等方法,我已经自动将JPEG转base64,所以image只需要JPEG格式就好了。