使用Python进行面部合成

简介:

一. 准备工作

1. 此程序使用的是 Face++ 的API,所以需要去Face++官网注册账号:

https://www.faceplusplus.com.cn/

2. 创建应用,获取 key 和 secret

dc917d82e926988e2b8974c7447c684c1436f816

3. 下载 simplejson 模块 ,使用pip就可以下载了

pip install simplejson

二. 程序思路

1. 使用 decect 接口,获取人脸关键点

接口详细文档:

https://console.faceplusplus.com.cn/documents/4888373

* return_landmark 参数 不能为 0 不然不会返回人脸关键点

return_landmark

Int

是否检测并返回人脸关键点。合法值为:

2

检测。返回 106 个人脸关键点。

1

检测。返回 83 个人脸关键点。

0

不检测

注:本参数默认值为 0

核心代码:


def find_face(imgpath):
print("finding")
http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
data = {"api_key": key, "api_secret": secret, "image_url": imgpath, "return_landmark": 1}
files = {"image_file": open(imgpath, "rb")}
req_con = response.content.decode('utf-8')
response = requests.post(http_url, data=data, files=files) req_dict = JSONDecoder().decode(req_con)
faces = this_json2['faces']
this_json = simplejson.dumps(req_dict) this_json2 = simplejson.loads(this_json) list0 = faces[0] rectangle = list0['face_rectangle']
return rectangle
# print(rectangle)

2. 使用 mergeface 接口,合成脸部图像

接口详细文档:

https://console.faceplusplus.com.cn/documents/20813963

* 注意图片文件大小不超过 2 MB

核心代码:

 

# 模板图片地址 合成图片地址 生成图片地址 合成指数0-100
def add_face(image_url_1,image_url_2,image_url,number):
ff1 = find_face(image_url_1)
rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height']))
ff2 = find_face(image_url_2)
rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height'])
# print(rectangle1) # print(rectangle2) url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
f2_64 = base64.b64encode(f2.read())
f1 = open(image_url_1, 'rb') f1_64 = base64.b64encode(f1.read()) f1.close() f2 = open(image_url_2, 'rb') f2.close()
"merge_base64": f2_64, "merge_rectangle": rectangle2, "merge_rate": number}
data = {"api_key": key, "api_secret": secret, "template_base64": f1_64, "template_rectangle": rectangle1, response = requests.post(url_add, data=data) req_con = response.content.decode('utf-8')
file.close()
req_dict = JSONDecoder().decode(req_con) print(req_dict) result = req_dict['result'] imgdata = base64.b64decode(result) file = open(image_url, 'wb')
file.write(imgdata)

3. 示例运行代码

# 单独两张照片的合成示例

 

image_url_1 = r"C:\Users\1.jpg"
image_url_2 = r"C:\Users\2.jpg"
image_url = r'C:\Users\result.jpg'
add_face(image_url_1,image_url_2,image_url,50)

4. 封装一个多张照片的合成函数

用 列表List 储存图片地址,先以最开始的两张进行合成,然后将合成后的图片与列表中的其他图像依次合成

* 程序没有做List的长度验证,注意边界特殊情况

 

def add_many(list_face):
print("正在合成第1-2张")
image_now = r'C:\Users\now.jpg'
add_face(list_face[0], list_face[1], image_now, 50)
print("正在合成第"+str(index+1)+"张")
for index in range(2,len(list_face)):
add_face(image_now, list_face[index], image_now, 50)

5. 成果展示

素材1:

63100bb98ac6e34680f20efd9defb7193cef94a7

素材2:

4f39f8cdb32285c6787a36f623d8601772846ae1

合成结果:

2cdd47455a2e9023d5a29418716ba7f4b988039c

完整代码:

https://github.com/chestnut-egg/Face


原文发布时间为:2018-11-22
本文作者:chestnut_egg
本文来自云栖社区合作伙伴“ Python爱好者社区”,了解相关信息可以关注“ Python爱好者社区”。
相关文章
|
9月前
|
数据处理 Python
|
8月前
|
Python
Python的知识点运用-2(排序&&找差值及修正ts合成顺序)
Python的知识点运用-2(排序&&找差值及修正ts合成顺序)
32 0
|
11月前
|
存储 Ubuntu Linux
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(上)
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(上)
|
11月前
|
存储 API 索引
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(中)
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(中)
|
11月前
|
存储 Ubuntu 物联网
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(下)
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(下)
117 0
|
数据可视化 计算机视觉 索引
人脸检测进阶:使用 dlib、OpenCV 和 Python 检测眼睛、鼻子、嘴唇和下巴等面部五官
人脸检测进阶:使用 dlib、OpenCV 和 Python 检测眼睛、鼻子、嘴唇和下巴等面部五官
729 0
人脸检测进阶:使用 dlib、OpenCV 和 Python 检测眼睛、鼻子、嘴唇和下巴等面部五官
|
机器学习/深度学习 传感器 编解码
人脸检测进阶:使用 dlib、OpenCV 和 Python 检测面部标记
人脸检测进阶:使用 dlib、OpenCV 和 Python 检测面部标记
416 0
人脸检测进阶:使用 dlib、OpenCV 和 Python 检测面部标记
|
存储 Ubuntu 测试技术
Elasticsearch 和 Python构建面部识别系统—Elastic Stack 实战手册
你是否曾经尝试在图像中搜索目标? Elasticsearch 可以帮助你存储,分析和搜索图像或视频中的目标。
694 0
Elasticsearch 和 Python构建面部识别系统—Elastic Stack 实战手册
|
7天前
|
安全 Java 数据处理
Python网络编程基础(Socket编程)多线程/多进程服务器编程
【4月更文挑战第11天】在网络编程中,随着客户端数量的增加,服务器的处理能力成为了一个重要的考量因素。为了处理多个客户端的并发请求,我们通常需要采用多线程或多进程的方式。在本章中,我们将探讨多线程/多进程服务器编程的概念,并通过一个多线程服务器的示例来演示其实现。
|
7天前
|
程序员 开发者 Python
Python网络编程基础(Socket编程) 错误处理和异常处理的最佳实践
【4月更文挑战第11天】在网络编程中,错误处理和异常管理不仅是为了程序的健壮性,也是为了提供清晰的用户反馈以及优雅的故障恢复。在前面的章节中,我们讨论了如何使用`try-except`语句来处理网络错误。现在,我们将深入探讨错误处理和异常处理的最佳实践。

热门文章

最新文章