python快速实现简单的图像人脸融合
生活随笔
收集整理的這篇文章主要介紹了
python快速实现简单的图像人脸融合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
所需資源:requests\simplejson\json\base64庫、人臉圖片、Face++中的相關url、apikey、apisecret
代碼如下:
import requests import simplejson import json import base64def find_face(imgpath):print("finding")http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'data = {"api_key": '..........',"api_secret": '...........', "image_url": imgpath, "return_landmark": 2}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": '.................', "api_secret": '....................',"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() def test():image1 = r"D:\A.png"image2 = r"D:\B.png"image = r"D:\C.png"merge_face(image2,image1,image,90) test()省略號部分填寫自己的key和secret
直接運行,結果如下:
A圖:
B圖:?
?
C圖:?
?
總結
以上是生活随笔為你收集整理的python快速实现简单的图像人脸融合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hsk内网穿透+SERV-U+搭建FTP
- 下一篇: 【CIPS 2016】(17章)文字识别