apiKey = 'xxx'
secretKey = 'xxx'
def getToken():
getTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+apiKey+'&client_secret='+secretKey
response = requests.get(getTokenUrl)
data = response.json()
token = data.get('access_token')
return token
def changeFace():
tmpImg = filedialog.askopenfilename(initialdir="./images/getImages")
if len(tmpImg)== 0:
messagebox.showwarning(title='请注意',message='请选择1张图片')
else:
#显示到相应的位置上
actionType = input("请选择你要变化的方向:"+"\n"+"1.变小孩"+"\n"+"2.变老人"+"\n"+"3.变女生"+"\n"+"4.变男生"+"\n"+"5.新版女生"+"\n")
if int(actionType) == 1:
action_type = 'TO_KID'
elif int(actionType) == 2:
action_type = 'TO_OLD'
elif int(actionType) == 3:
action_type = 'TO_FEMALE'
elif int(actionType) == 4:
action_type = 'TO_MALE'
elif int(actionType) == 5:
action_type = 'TO_FEMALE_V2'
else:
action_type = 'TO_KID'
#进行数据准备
access_token = getToken()
request_url = 'https://aip.baidubce.com/rest/2.0/face/v1/editattr'
with open(tmpImg,'rb') as f1:
image1 = f1.read()
b64Image1 = base64.b64encode(image1)
IMAGE_TYPE = 'BASE64'
params = {
"image":str(b64Image1,'utf-8'),
"image_type": IMAGE_TYPE,
"quality_control": "NORMAL",
"action_type": action_type,
}
request_url = request_url+"?access_token="+access_token
headers = {'content-type': 'application/json'}
response = requests.post(request_url, data=params, headers=headers)
if response:
data = response.json()
print(data)
tmpNewImg =base64.b64decode(data['result']['image'])
nu = len(os.listdir('./images/createImages'))
with open('./images/createImages/%s.jpg'%nu,'wb')as newF:
newF.write(tmpNewImg)
window = tk.Tk()
bgImg2 = ImageTk.PhotoImage(file='./images/createImages/'+str(nu)+'.jpg')
bg2 = tk.Label(window,width=300,height=300,image=bgImg2)
bg2.pack()
window.mainloop()
-->