Python实现AI换脸功能

所属分类: 脚本专栏 / python 阅读数: 1451
收藏 0 赞 0 分享

需要用到的接口:

获取人脸信息的接口: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写一个量化股票提醒系统

这篇文章主要介绍了小白用Python写了一个股票提醒系统,迷你版量化系统,完美的实现了实时提醒功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Python绘制的二项分布概率图示例

这篇文章主要介绍了Python绘制的二项分布概率图,涉及Python基于numpy、math的数值运算及matplotlib图形绘制相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python Learning 列表的更多操作及示例代码

这篇文章主要介绍了Python Learning-列表的更多操作,需要的朋友可以参考下
收藏 0 赞 0 分享

关于python列表增加元素的三种操作方法

这篇文章主要介绍了关于python列表增加元素的几种操作方法,主要有insert方法,extend方法和append方法,每种方法给大家介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

如何在python字符串中输入纯粹的{}

这篇文章主要介绍了如何在python字符串中输入纯粹的{}以及python字符串连接的三种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

浅谈Django的缓存机制

这篇文章主要介绍了浅谈Django的缓存机制,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Django 限制用户访问频率的中间件的实现

这篇文章主要介绍了Django 限制用户访问频率的中间件的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

示例详解Python3 or Python2 两者之间的差异

这篇文章主要介绍了Python3 or Python2?示例详解两者之间的差异,在本文中给大家介绍的非常详细,需要的朋友可以参考下
收藏 0 赞 0 分享

Python wxpython模块响应鼠标拖动事件操作示例

这篇文章主要介绍了Python wxpython模块响应鼠标拖动事件操作,结合实例形式分析了Python使用wxpython模块创建窗口、绑定事件及相应鼠标事件相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

使用Python实现一个栈判断括号是否平衡

栈(Stack)在计算机领域是一个被广泛应用的集合,栈是线性集合,访问都严格地限制在一段,叫做顶(top)。这篇文章主要介绍了使用Python实现一个栈判断括号是否平衡,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多