调用公共API

免费的,但识别几百张图片后容易掉线

import requests

# 百度识别API
url = 'https://cloud.baidu.com/aidemo'
# 字典form的image键对应的值为 "data:image/jpeg;base64," + "图片的64位编码"
form = {'image':"data:image/jpeg;base64,图片的64位编码", 
       'type':'animal',
        'show':True }
            
r = requests.post(url, data=form)

调用私人API

每天500次免费调用机会,之后0.8元/千次。但是也会出现掉线的情况

1. 获取access_token通行证
# 获取access_token,一个月需要重新获取一次
import requests
import json
 
# client_id 为官网获取的AK, client_secret 为官网获取的SK
url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=私人AK&client_secret=私人SK'
# header = ('Content-Type', 'application/json; charset=UTF-8')
r = requests.post(url)
access_token = json.loads(r.text)['access_token']
print(access_token)
>>> 24.84c06e1e11db18c78de511a89947cb8e.2592000.1615567923.282335-46323162
import requests
# url + access_token
url = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/animal?access_token=' + access_token
form = {'image':"图片的64位编码",
        'top_num':5}  # top_num,最多显示5只可能的动物

r = requests.post(url, data=form)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐