face_swap(百度智能云)

参考来源:百度智能云-人脸融合 + 实现

复制代码
import requests
import base64
import json
 
# 获取token
def get_token(client_id, client_secret):
# client_id为官网获取的API Key,client_secret为官网获取的Secret Key.将下行client_id=后的....换为你的API Key,client_secret=后的....换为你的Secret Key
    url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=@@@@@&client_secret=@@@@@"
    response = requests.get(url)
    resultJson = response.json()
    return resultJson['access_token']
 
# 根据图片名读取图片,并转换成base64
def read_photo(name):
    with open('%s' % name, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        bd = base64_data.decode()
        return bd
 
 
# 调用百度的接口,实现融合图片
def face_fusion(token, template, target):
    url = 'https://aip.baidubce.com/rest/2.0/face/v1/merge'
    request_url = url + '?access_token=' + token
    params = {
        "image_template": {
            "image": template,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "image_target": {
            "image": target,
            "image_type": "BASE64",
            "quality_control": "NORMAL"
        },
        "merge_degree": "HIGH"
    }
    params = json.dumps(params)
    headers = {'content-type': 'application/json'}
    result = requests.post(request_url, data=params, headers=headers).json()
    if result['error_code'] == 0:
        res = result["result"]["merge_image"]
        down_photo(res)
    else:
        print(str(result['error_code'])+result['error_msg'])
 
# 下载融合后图片
def down_photo(data):
    imagedata = base64.b64decode(data)
    file = open('D:\Pics\\result.jpg', "wb")    #修改为自己的路径,'wb'不改
    file.write(imagedata)
 
# 主程序
if __name__ == '__main__':
    # 这里的融合用一个男一个女的效果比较不错,所以用boy和girl命名
    # 路径按照自己的图片路径来
    boy = read_photo('D:\Pics\\zr.jpg')
    girl = read_photo('D:\Pics\\cc.jpg')
    token = get_token('@@@@', '@@@@')    # 第一个改为API Key,第二个改为Secret Key
    face_fusion(token, boy, girl)
复制代码

 

posted @   xms_667  阅读(362)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示