首页 编程教程正文

人工智能几行代码实现换脸

piaodoo 编程教程 2020-02-22 22:15:14 1205 0 python教程

本文来源吾爱破解论坛

人工智能几行代码实现换脸

下面来介绍一种对照片的简单换脸方法。

本篇介绍的换脸方法需要借助Face++,关于Face++的API,大家可自行查看说明文档,都比较简单。

文档地址:https://console.faceplusplus.com.cn/documents/20813963
效果:

2019-08-31_100715.jpg (152.3 KB, 下载次数: 6)

下载附件  保存到相册

2019-8-31 10:12 上传



[Python] 纯文本查看 复制代码
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"

merge_face(image2, image1, image, 90)


本帖被以下淘专辑推荐: · 鱼木收集|主题: 2120, 订阅: 1667 · 高质量资源|主题: 832, 订阅: 913 · 好帖|主题: 149, 订阅: 14

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/7979.html

评论

搜索