人脸关键点||face++&&python
2019-08-09 本文已影响0人
Vincy_ivy
import cv2
import requests
import json
import base64
def file_base64(file_name):
with open(file_name,'rb') as fin:
file_data=fin.read()
base64_data=base64.b64encode(file_data)
return base64_data
url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
files = {'Image_file': open('3.jpg', 'rb')}
payload = {'api_key': 'TLTaqO4hSCZtNoxg4stjChq7o-0FPDYB',
'api_secret': '9ptXes4ObjXHwHdjOVq-mekjoN-LlqRO',
#'image_url': 'https://image.so.com/view?q=%E5%8D%83%E7%8E%BA&listsrc=sobox&listsign=c8f2dd5542c5c82c1e1116ad3425cce3&src=360pic_normal&correct=%E5%8D%83%E7%8E%BA&ancestor=list&cmsid=7cd680ddc9676663d354b03c861bf133&cmran=0&cmras=6&cn=0&gn=0&kn=50&fsn=130&adstar=0&clw=262#id=e8bfc493703e8be5669f2c133326a43f&currsn=0&ps=121&pc=121',
'return_landmark': 1,
'return_attributes': 'none',
#'image_file':files,
'image_base64': file_base64('3.jpg')
}
r = requests.post(url, files=files, data=payload)
data = json.loads(r.text)
# %%
# print request content,you can also use r.+tab to see more things.
print(r.text)
width = data['faces'][0]['face_rectangle']['width']
top = data['faces'][0]['face_rectangle']['top']
height = data['faces'][0]['face_rectangle']['height']
left = data['faces'][0]['face_rectangle']['left']
img = cv2.imread("3.jpg")
vis = img.copy()
# draw face rectangle
# cv2.rectangle(vis, (left, top), (left+width, top+height),(0, 255, 0), 1)
# %%
# draw face landmarks
for j in (0, len(data['faces']) - 1):
for i in data['faces'][j]['landmark']:
cor = data['faces'][j]['landmark'][i]
x = cor["x"]
y = cor["y"]
cv2.circle(vis, (x, y), 2, (0, 255, 0), -1)
# %%
cv2.imshow("Image", vis)
cv2.waitKey(0)
# save image with landmarks
cv2.imwrite("Image", vis)
cv2.destroyAllWindows()
弄了一晚上终于可以睡觉了,参考了好多的文档,结果发现faces一直报错,楼主说是因为图片大小,后来发现是少了一个参数——'image_base64'