发布于2019-08-05 18:56 阅读(1185) 评论(0) 点赞(0) 收藏(1)
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": "https://console.faceplusplus.com.cn申请'',
"api_secret": 'https://console.faceplusplus.com.cn申请', "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
# 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": 'https://console.faceplusplus.com.cn申请L', "api_secret": 'https://console.faceplusplus.com.cn申请',
"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()
#E:\test\test
image1 = r"E:\test\test\cover1.jpg"
image2 = r"E:\test\test\cover11.jpg"
image = r"E:\test\test\face7.jpg"
merge_face(image2, image1, image, 100)
作者:忘也不能忘
链接:https://www.pythonheidong.com/blog/article/6658/b4f42a180fe716fbf3e5/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!