人脸变化变老变男变女(百度ai接口)

该博客展示了如何利用Python代码结合百度AI的人脸编辑API,将图片中的人物变老、变年轻、变性等。用户可以选择图片并指定编辑方向,程序会返回处理后的图片并在GUI窗口中展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()

-->

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Gjanuary

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值