Python实现AI换脸功能

作者:_wish_ 时间:2021-02-11 19:39:08 

需要用到的接口:

获取人脸信息的接口:https://api-cn.faceplusplus.com/facepp/v3/detect

实现换脸的接口 :https://api-cn.faceplusplus.com/imagepp/v1/mergeface

代码分为三步

代码:


import requests
import json
import simplejson
import base64

#第一步:获取人脸关键点
def find_face(imgpath):
"""
:param imgpath: 图片的地址
:return: 一个字典类型的人脸关键点 如:{'top': 156, 'left': 108, 'width': 184, 'height': 184}
"""
http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect' #获取人脸信息的接口
data = {
"api_key":"x2NyKaa6vYuArYwat4x0-NpIbM9CrwGU",#访问url所需要的参数
"api_secret":"OuHx-Xaey1QrORwdG7QetGG5JhOIC8g7",#访问url所需要的参数
"image_url":imgpath, #图片地址
"return_landmark":1
}

files = {'image_file':open(imgpath,'rb')} #定义一个字典存放图片的地址
response = requests.post(http_url,data=data,files=files)
res_con1 = response.content.decode('utf-8')
res_json = simplejson.loads(res_con1)
faces = res_json['faces']
list = faces[0]
rectangle = list['face_rectangle']
return rectangle

#第二步:实现换脸
def merge_face(image_url1,image_url2,image_url,number):
"""
:param image_url1: 被换脸的图片路径
:param image_url2: 换脸的图片路径
:param image_url: 换脸后生成图片所保存的路径
:param number: 换脸的相似度
"""
#首先获取两张图片的人脸关键点
face1 = find_face(image_url1)
face2 = find_face(image_url2)
#将人脸转换为字符串的格式
rectangle1 = str(str(face1['top']) + "," + str(face1['left']) + "," + str(face1['width']) + "," + str(face1['height']))
rectangle2 = str(str(face2['top']) + "," + str(face2['left']) + "," + str(face2['width']) + "," + str(face2['height']))
#读取两张图片
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()

url_add = 'https://api-cn.faceplusplus.com/imagepp/v1/mergeface' #实现换脸的接口
data={
"api_key": "x2NyKaa6vYuArYwat4x0-NpIbM9CrwGU",
"api_secret": "OuHx-Xaey1QrORwdG7QetGG5JhOIC8g7",
"template_base64":f1_64,
"template_rectangle":rectangle1,
"merge_base64":f2_64,
"merge_rectangle":rectangle2,
"merge_rate":number
}
response1 = requests.post(url_add,data=data)
res_con1 = response1.content.decode('utf-8')
res_dict = json.JSONDecoder().decode(res_con1)
result = res_dict['result']
imgdata = base64.b64decode(result)
file=open(image_url,'wb')
file.write(imgdata)
file.close()

if __name__ == '__main__':
image1 = r"meizi1.jpg"
image2 = r"meizi.jpg"
image3 = r"face1.jpg"
merge_face(image1,image2,image3,100)

效果:

换脸前

Python实现AI换脸功能

要换的脸:

Python实现AI换脸功能

换脸后:

Python实现AI换脸功能

来源:https://blog.csdn.net/cl18154399536/article/details/105387239

标签:Python,AI,换脸
0
投稿

猜你喜欢

  • 如何利用python和DOS获取wifi密码

    2021-12-15 20:10:50
  • Python实现基于标记的分水岭分割算法

    2023-11-03 08:27:53
  • Python3安装Pillow与PIL的方法

    2022-09-27 10:21:28
  • 详解Python当中的字符串和编码

    2021-05-26 07:07:39
  • pytorch torchvision.ImageFolder的用法介绍

    2023-05-29 17:20:49
  • Python将列表中的元素转化为数字并排序的示例

    2023-07-06 11:16:11
  • Python使用crontab模块设置和清除定时任务操作详解

    2023-02-07 15:13:37
  • 解决python3 整数数组转bytes的效率问题

    2023-08-09 19:39:41
  • Django 生成登陆验证码代码分享

    2021-07-31 06:48:21
  • 用Python的urllib库提交WEB表单

    2023-06-11 00:14:52
  • PHP加密函数 Javascript/Js 解密函数

    2023-06-15 18:03:03
  • Python的组合模式与责任链模式编程示例

    2023-05-08 07:03:43
  • tensorflow求导和梯度计算实例

    2023-08-09 14:07:38
  • Python : turtle色彩控制实例详解

    2022-09-22 13:01:46
  • python学习之新式类和旧式类讲解

    2021-02-27 11:10:38
  • 怎样在MySQL数据库中导出整个数据库

    2008-12-31 15:13:00
  • python执行scp命令拷贝文件及文件夹到远程主机的目录方法

    2023-07-10 09:12:19
  • ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能示例

    2023-11-17 02:54:39
  • Oracle字符集修改查看方法

    2009-11-05 21:45:00
  • Python中Continue语句的用法的举例详解

    2023-04-04 13:12:37
  • asp之家 网络编程 m.aspxhome.com