Skip to content

Commit 66780f8

Browse files
committed
提交代码
1 parent eb555ef commit 66780f8

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

xianhuan/changeFace/changeface.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
@author: 闲欢
5+
"""
6+
import base64
7+
import requests
8+
import json
9+
import simplejson
10+
11+
# 第一步,获取人脸关键点
12+
api_key = '你的key'
13+
api_secret = '你的secret'
14+
15+
def find_face(imgpath):
16+
http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
17+
data = {"api_key": api_key,
18+
"api_secret": api_secret,
19+
"image_url": imgpath,
20+
"return_landmark": 1 # 是否检测并返回人脸关键点
21+
}
22+
files = {"image_file": open(imgpath, "rb")}
23+
response = requests.post(http_url, data=data, files=files)
24+
req_con = response.content.decode('utf-8')
25+
req_dict = json.JSONDecoder().decode(req_con)
26+
this_json = simplejson.dumps(req_dict)
27+
this_json2 = simplejson.loads(this_json)
28+
faces = this_json2['faces']
29+
list0 = faces[0]
30+
rectangle = list0['face_rectangle']
31+
return rectangle
32+
33+
# 第二步,换脸
34+
# number表示换脸的相似度
35+
def merge_face(image_url1, image_url2, image_url, number):
36+
ff1 = find_face(image_url1)
37+
ff2 = find_face(image_url2)
38+
39+
rectangle1 = str(str(ff1['top']) + "," + str(ff1['left']) + "," + str(ff1['width']) + "," + str(ff1['height']))
40+
rectangle2 = str(ff2['top']) + "," + str(ff2['left']) + "," + str(ff2['width']) + "," + str(ff2['height'])
41+
42+
url_add = "https://api-cn.faceplusplus.com/imagepp/v1/mergeface"
43+
f1 = open(image_url1, 'rb')
44+
f1_64 = base64.b64encode(f1.read())
45+
f1.close()
46+
f2 = open(image_url2, 'rb')
47+
f2_64 = base64.b64encode(f2.read())
48+
f2.close()
49+
50+
data = {"api_key": api_key,
51+
"api_secret": api_secret,
52+
"template_base64": f1_64,
53+
"template_rectangle": rectangle1,
54+
"merge_base64": f2_64,
55+
"merge_rectangle": rectangle2,
56+
"merge_rate": number
57+
}
58+
response = requests.post(url_add, data=data)
59+
req_con1 = response.content.decode('utf-8')
60+
req_dict = json.JSONDecoder().decode(req_con1)
61+
result = req_dict['result']
62+
imgdata = base64.b64decode(result)
63+
file = open(image_url, 'wb')
64+
file.write(imgdata)
65+
file.close()
66+
67+
image1 = r"C:\Users\xx\Downloads\tmp\pic/2.jpg"
68+
image2 = r"C:\Users\xx\Downloads\tmp\pic/3.jpg"
69+
image = r"C:\Users\xx\Downloads\tmp\pic/5.jpg"
70+
71+
merge_face(image1, image2, image, 90)

0 commit comments

Comments
 (0)