百度OCR与人脸识别:aip-python-sdk-2.2.15

百度AI与人脸识别:aip-python-sdk-2.2.15

安装使用Python SDK有如下方式

  • 如果已安装pip,执行pip install baidu-aip即可。
  • 如果已安装setuptools,执行python setup.py install即可。
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('照片的路径','用户组名称','用户名称')
'''

修改百度人脸识别SDK错误
[Python]json 错误xx is not JSON serializable

cd至sdk目录aip下:找到face.py文件修改:

'''尾部添加'''
class DateEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8')
        return json.JSONEncoder.default(self, obj)

替换所有:json.dumps(images, ensure_ascii=False,cls=DateEncoder)    

 

 

你可能感兴趣的:(python,运维,硬件)