其实早在之前,基于AI换脸的技术就得到了应用,比方说《速度与激情7》里面的演员保罗.沃克,由于发生意外,后期将其换脸,就达成我们在影院看到的效果
当然,也有人把这项技术应用在其他的领域,例如把某种电影的女主换成了盖尔.加朵……
让我们看看这次换脸的小宝哥原图
代码参考:
import requests
import simplejson
import json
import base64
#Face++网址:[url]https://console.faceplusplus.com.cn/dashboard[/url]
#第一步,获取人脸关键点
def find_face(imgpath):
print("正在查找……")
http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
data = {"api_key": '自己申请',
"api_secret": '自己申请TLJC',
"image_url": imgpath, "return_landmark": 1}
files = {"image_file": open(imgpath, "rb")}
response = requests.post(http_url, data=data, files=files)
req_con = response.content.decode('utf-8')
req_dict = json.JSONDecoder().decode(req_con)
this_json = simplejson.dumps(req_dict)
this_json2 = simplejson.loads(this_json)
print(this_json2)
faces = this_json2['faces']
list0 = faces[0]
rectangle = list0['face_rectangle']
#print(rectangle)
return rectangle
#第二步,换脸,其中图片的大小应不超过2M
# number表示换脸的相似度
def merge_face(image_url1, image_url2, image_url, number):
ff1 = find_face(image_url1)
ff2 = find_face(image_url2)
rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height']))
rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height'])
print(rectangle2)
url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
f1 = open(image_url1, 'rb')
f1_64 = base64.b64encode(f1.read())
f1.close()
f2 = open(image_url2, 'rb')
f2_64 = base64.b64encode(f2.read())
f2.close()
data = {"api_key": '自己申请',
"api_secret": '自己申请TLJC',
"template_base64": f1_64, "template_rectangle": rectangle1,
"merge_base64": f2_64, "merge_rectangle": rectangle2, "merge_rate": number}
response = requests.post(url_add, data=data)
req_con1 = response.content.decode('utf-8')
#print(req_con1)
req_dict = json.JSONDecoder().decode(req_con1)
result = req_dict['result']
imgdata = base64.b64decode(result)
file = open(image_url, 'wb')
file.write(imgdata)
file.close()
image1 = r"E:\test\img01.jpg"
image2 = r"E:\test\img02.jpg"
image = r"E:\test\imgh.jpg"
网恋奔现对方长的好看就如同中彩票大奖一样开心,人生最开心的时候也仅此而已,愿天下的网恋男女奔现都遇到帅哥美女,加油。
①Python入门必备《三剑客》电子书
②Python开发环境安装包详细教程视频
③Python最新入门教程视频(共200G!)
④程序员必备英语单词词汇
⑤Python 2021 学习路线图
⑥1000项目源码案例分享
如果你用得到的话可以直接拿走,在我的QQ技术交流群里群号:517740534(纯技术交流和资源共享,广告勿入)