使用百度AI接口进行人脸识别(Python SDK V3版本实现) | 您所在的位置:网站首页 › 百度人脸识别找人怎样关闭 › 使用百度AI接口进行人脸识别(Python SDK V3版本实现) |
文章目录
1.在百度AI开放平台创建接口2.安装人脸识别 Python SDK3.算法思路3.1首先通过python SDK中的AipFace类获取一个客户端对象3.2通过获取的客户端对象client进行操作。
4.完整代码
1.在百度AI开放平台创建接口
https://blog.csdn.net/qq_42363032/article/details/108652945 2.安装人脸识别 Python SDK首先在当前的python环境中使用pip install baidu-aip安装人脸识别 Python SDK。 当前代码所在目录有两张jpg图片 通过客户端的match方法进行对比操作,观察打印出的result值 result = client.match([ { 'image': str(base64.b64encode(open('liu.jpg', 'rb').read()), 'utf-8'), 'image_type': 'BASE64', }, { 'image': str(base64.b64encode(open('liu1.jpg', 'rb').read()), 'utf-8'), 'image_type': 'BASE64', } ]) print(result) """ { 'error_code': 0, 'error_msg': 'SUCCESS', 'log_id': 9405352011017, 'timestamp': 1592828860, 'cached': 0, 'result': { 'score': 94.24956512, 'face_list': [ { 'face_token': '37829d5d5ab0e810173cff9d8e78a4db' }, { 'face_token': '85b64922c3f2d5fc7aedfbae67c6f37e' } ] } } """发现result为一个json类型的数据,可以通过字典方式进行获取。如:先通过result[‘error_msg’]判断是否对比成功,成功则输出result[‘result’][‘score’]为对比完的相似度分数;否则打印出错误信息。 if result['error_msg'] == 'SUCCESS': score = result['result']['score'] print(score) 4.完整代码 from aip import AipFace import base64 """ 你的APPID,API_KEY和SECRET_KEY """ APP_ID = '你的APP_ID' API_KEY = '你的API_KEY ' SECRET_KEY = '你的SECRET_KEY' pic1 = "test11.jpg" pic2 = "test1.jpg" # 封装成函数,返回获取的client对象 def get_client(APP_ID, API_KEY, SECRET_KEY): """ 返回client对象 :param APP_ID: :param API_KEY: :param SECRET_KEY: :return: """ return AipFace(APP_ID, API_KEY, SECRET_KEY) client = get_client(APP_ID, API_KEY, SECRET_KEY) result = client.match([ { 'image': str(base64.b64encode(open(pic1, 'rb').read()), 'utf-8'), 'image_type': 'BASE64', }, { 'image': str(base64.b64encode(open(pic2, 'rb').read()), 'utf-8'), 'image_type': 'BASE64', } ]) print(result) if result['error_msg'] == 'SUCCESS': score = result['result']['score'] print('两张图片相似度:', score) else: print('错误信息:', result['error_msg']) import matplotlib.pyplot as plt pc1 = plt.imread(pic1) pc2 = plt.imread(pic2) plt.imshow(pc1) plt.show() plt.imshow(pc2) plt.show() |
CopyRight 2018-2019 实验室设备网 版权所有 |