进入网站
然后点击“立即使用—图像分类
点击-我的模型—服务详情—立即使用
点击创建应用
立即创建(以上按个人要求填写)
创建完毕可查询详情
返回应用列表即可查询到
点击飞浆AL_studio
点击AI_studio
点击项目–我的项目–创建项目–
进入项目
下面代码讲相应的部分替换
host=接口地址(在easydl我的模型里可查)
client_id=API_KEY
client_secret=Secret Key
图片路径也需要自己修改
# encoding:utf-8
import os
import urllib.request
import requests
import base64
import json
'''
easydl青春有你2选手图像分类
'''
#获取图像base64编码
with open ( "fimg_2687.jpg", "rb" ) as fin:
image_data = fin.read()
#print(image_data)
base64_data = base64.b64encode(image_data)
#print(base64_data)
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=9HqdasR121e0ZTGitmILZBve&client_secret=YzgYx59v2YGqGxYqucH5VKyE4G2cwf4E'
response = requests.get(host)
if response:
#print(type(response.json()))
access_token = response.json()['access_token']
#print(access_token)
# access_token = json.load(response.json())['access_token']
#复制easyDL应用对外API地址
request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/class_rubbish"
#拼接完整URL
request_url = request_url + "?access_token=" + access_token
#组合参数
params = '"{\"image\":\"' + bytes.decode(base64_data) +'\",\"top_num\":\"5\"}"'
params = params.strip('"') #很重要,否则提示336002 JSON不合法 json格式错误
print(params)
params = bytes(params,"utf-8")
#POST带数据请求
req = urllib.request.Request(url=request_url, data=params)
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req)
content = response.read()
if content:
print(content)