一、准备
百度云登录,没注册的先注册
在产品服务那搜索“图像识别”
创建一个新应用,应用名称随便写
创建成功后会生成AppID,API Key,Secret Key
二、安装接口
pip3 install baidu-aip
如果安装慢可以用镜像pip3 install baidu-aip -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
三、代码
from aip import AipImageClassify
APP_ID='your APP_ID'
API_KEY='your API_KEY'
SECRET_KEY='your SECRET_KEY'
client = AipImageClassify(APP_ID,API_KEY,SECRET_KEY)
def get_file_content(filePath):
with open(filePath,'rb') as fp:
return fp.read()
image = get_file_content('/Users/yiqin/Desktop/timg.jpg')#选取本地的一张图像
result = client.advancedGeneral(image)
print(result)
返回结果:{'log_id': 9207453940322342499, 'result_num': 5, 'result': [{'score': 0.819571, 'root': '公众人物', 'keyword': '张韶涵'}, {'score': 0.600638, 'root': '人物-人物特写', 'keyword': '人物特写'}, {'score': 0.463741, 'root': '公众人物', 'keyword': '钟欣潼'}, {'score': 0.172726, 'root': '人物-人物特写', 'keyword': '美女'}, {'score': 0.080837, 'root': '商品-香烟', 'keyword': 'ARA'}]}
result_num:数组中的元素个数
score:置信度0-1
root:识别结果的上层标签
keyword:图片中的物体或场景名称