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)

浙公网安备 33010602011771号