使用Python進行面部合成
一. 準備工作
1. 此程式使用的是 Face++ 的API,所以需要去Face++官網註冊賬號:
https://www.faceplusplus.com.cn/
2. 建立應用,獲取 key 和 secret
3. 下載 simplejson 模組 ,使用pip就可以下載了
pip install simplejson
二. 程式思路
1. 使用 decect 介面,獲取人臉關鍵點
介面詳細文件:
https://console.faceplusplus.com.cn/documents/4888373
* return_landmark 引數 不能為 0 不然不會返回人臉關鍵點
return_landmark |
Int |
是否檢測並返回人臉關鍵點。合法值為:
注:本引數預設值為 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)