python视频换脸例子_python视频换脸例子_超简单使用Python换脸实例

本文介绍了如何使用Python和Face++ API进行简单的照片换脸操作。首先通过Face++获取人脸关键点,然后利用这些信息进行脸部融合,实现换脸效果。此技术在电影制作中有实际应用,但也可能引发伦理问题。提供的代码示例展示了如何实现这一过程。
摘要由CSDN通过智能技术生成

换脸!

这段时间,deepfakes搞得火热,比方说把《射雕英雄传》里的朱茵换成了杨幂,看下面的图!毫无违和感!

c36016b8ee3c872eaa5d5cf4ec0fa7d8.png

其实早在之前,基于AI换脸的技术就得到了应用,比方说《速度与激情7》里面的演员保罗.沃克,由于发生意外,

后期的视频都是由他的兄弟完成拍摄,然后再对其换脸,最终也就是我们影院看到的效果。

当然,也有人把这项技术应用在其他的领域,例如把某种电影的女主换成了盖尔.加朵(《神奇女侠》的扮演者),,,

7700c07c45652acdcd3e587be6e7fa9d.png

这真的是对“技术是一把又刃剑”阐述的十分到位。。。

关于deepfakes这里不再详细说明,好吧,小编承认是来蹭热度的。。。

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

本篇介绍的换脸方法需要借助Face++,关于Face++的API,大家可自行查看说明文档,都比较简单,小编在这里就不做具体

说明了:

1.配置工具:

pip install requests

pip install simplejson

2.获取Face++ api_key和secret

3.换脸步骤

调用的库

import requests

import simplejson

import json

import base64

第一步,获取人脸关键点,代码如下说述:

def find_face(imgpath):

print("finding")

http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'

data = {"api_key": '自己申请',

"api_secret": '自己申请', "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)

faces = this_json2['faces']

list0 = faces[0]

rectangle = list0['face_rectangle']

# print(rectangle)

return rectangle

第二步,换脸,其中图片的大小应不超过2M,代码如下所述:

#number表示换脸的相似度

def merge_face(image_url_1,image_url_2,image_url,number):

ff1 = find_face(image_url_1)

ff2 = find_face(image_url_2)

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'])

url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"

f1 = open(image_url_1, 'rb')

f1_64 = base64.b64encode(f1.read())

f1.close()

f2 = open(image_url_2, 'rb')

f2_64 = base64.b64encode(f2.read())

f2.close()

data = {"api_key": '自己申请', "api_secret": '自己申请',

"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_con = response.content.decode('utf-8')

req_dict = json.JSONDecoder().decode(req_con)

result = req_dict['result']

imgdata = base64.b64decode(result)

file = open(image_url, 'wb')

file.write(imgdata)

file.close()

测试

def test():

image1 = r"F:\GXT.bmp"

image2 = r"F:\MSC.bmp"

image = r"F:\MEG.bmp"

merge_face(image2,image1,image,90)

测试结果:

左图:关某某

右图:马某某

中图:MERGE

34ee3123291813ad694a70c367f12587.png

33d76965a51b2e2bbc342f9e47ce2916.png

35a9f865ecc55e50ae6ce57e68e01e3b.png

以上所述是小编给大家介绍的Python换脸实例详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值