最近在做一个人脸识别检测的项目,然后了解到face++有免费的可以直接调用,当然是基于web的,离线的SDK价格太高,学生党用不起,无奈之下选择face++,虹软据说有免费的离线SDK使用,不过只可以检测人脸,无法判断这张脸究竟是谁的。
首先需要去face++申请注册为开发者,申请网址为https://www.faceplusplus.com.cn,然后在官网的应用管理界面记下你的API Key和API Secret,官网提供的历程只有python2的版本,对于直接上手python3的同学来说,改写还是比较困难的,所以提供给大家以下经过改写的示例代码:
import requests
from json import JSONDecoder
import datetime
import cv2
import os
http_url ="https://api-cn.faceplusplus.com/humanbodypp/beta/gesture"
key ="Your Key"
secret ="Your Secret"
filepath1 ="E:\\face++\\image\\gesture.jpg"
data = {"api_key":key, "api_secret": secret, "return_gesture": "1"}
files = {"image_file": open(filepath1, "rb")}
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
# cv2.resize(frame,frame,320,240,0)
# show a frame
cv2.imshow("capture", frame)
cv2.imwrite("E:\\face++\\image\\gesture.jpg", frame)
files = {"image_file": open(filepath1, "rb")}
starttime = datetime.datetime.now()
response = requests.post(http_url, data=data, files=files)
endtime = datetime.datetime.now()
print((endtime - starttime).seconds)
req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)
print(req_dict)
cv2.waitKey(10)