实现前需要先pip install baidu-aip 模块
在百度开发者的平台注册,网站如下
https://cloud.baidu.com/product/imagerecognition
创建应用,自己选择,创建完成后他会给你三个码,需要填到下面的代码当中去
from aip import AipImageClassify
import os
# 百度应用的app_id、api_key和secret_key
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
# 创建通用图像识别客户端
client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
def get_file_content(file_path):
with open(file_path, 'rb') as f:
return f.read()
# 获取图片对象
# 我是将图片直接放在项目根目录的images文件夹中的,记得修改自己的图片路径
image = get_file_content(os.getcwd()+'/批注 2020-03-31 001630.png')
# 通用图像识别
print(client.carDetect(image,options={"top_num":1})["result"][0]["name"])
运行结果