# encoding:utf-8
import requests
import base64
import json
# client_id 为官网获取的AK， client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=e1G3wD3Ud1o15qGxgrgEUGhZ&client_secret=0ZKbZBbGszmfms83SzyjhnsgHnC0o3et'
response = requests.get(host)
print(response.json()['access_token'])
if response:
    print(response.json())
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg"
# 二进制方式打开图片文件
f = open("image/123.jpg", 'rb')
img = base64.b64encode(f.read())
params = {"image": img}
access_token = response.json()['access_token']
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
if response:
    print(response.json())
    imgdata = base64.b64decode(response.json()['foreground'])
    file = open("image/koutu.png", 'wb')
    file.write(imgdata)
    file.close()