1. 获取access_token:控制台—>图像识别—>创建实例—>获取APIkey和Secret key

    输入:

    In [ ]:

    1. # encoding:utf-8
    2. import requests
    3. # client_id 为官网获取的AK, client_secret 为官网获取的SK
    4. host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=3tUxLSVriWQCxtzXakrvFS00&client_secret=RfWIiepXS8rcfseADS8gS8GxaGQKOiEx'
    5. response = requests.get(host)
    6. if response:
    7. print(response.json())

    输出:

    1. {'refresh_token': '25.a95408b8c8eeeed5abbe0e8a8f9881a1.315360000.1909362833.282335-21148718', 'expires_in': 2592000, 'session_key': '9mzdDoHMrzI6asIzHq1n8H+0/JetVtxHJWl7O6LWgBTiG3MWojrUf3Ubb1Vq28SXwzrNYel0hslIhfSYQdJpO3u8bRTNug==', 'access_token': '24.531ac64d623525742e6b945eab4c8c21.2592000.1596594833.282335-21148718', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理', 'session_secret': '111f0ec1fada3c72729088a09258149d'}

    2.对动物图片进行识别

    1. # encoding:utf-8
    2. import requests
    3. import base64
    4. '''
    5. 通用物体和场景识别
    6. '''
    7. request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
    8. # 二进制方式打开图片文件
    9. f = open('物体.jpg', 'rb')
    10. img = base64.b64encode(f.read())
    11. params = {"image":img}
    12. access_token = '24.17de60306e1d633d58404513b4f9d5a0.2592000.1596939595.282335-21148718'
    13. request_url = request_url + "?access_token=" + access_token
    14. headers = {'content-type': 'application/x-www-form-urlencoded'}
    15. response = requests.post(request_url, data=params, headers=headers)
    16. if response:
    17. print (response.json())
    1. {'log_id': 8102246538943441546, 'result_num': 5, 'result': [{'score': 0.668051, 'root': '植物-蔷薇科', 'keyword': '苹果'}, {'score': 0.52596, 'root': '非自然图像-艺术画', 'keyword': '油画'}, {'score': 0.380701, 'root': '植物-其它', 'keyword': '姬娜果'}, {'score': 0.238192, 'root': '商品-水果', 'keyword': '水果摊'}, {'score': 0.041621, 'root': '非自然图像-艺术画', 'keyword': '水彩画'}]}