python3.5调用face++

来源:互联网 发布:淘宝待发货 编辑:程序博客网 时间:2024/05/22 08:08

python3.5调用face++

  • 最近在做一个人脸识别检测的项目,然后了解到face++有免费的可以直接调用,当然是基于web的,离线的SDK价格太高,学生党用不起,无奈之下选择face++,虹软据说有免费的离线SDK使用,不过只可以检测人脸,无法判断这张脸究竟是谁的。

  • 首先需要去face++申请注册为开发者,申请网址为https://www.faceplusplus.com.cn,然后在官网的应用管理界面记下你的API Key和API Secret,官网提供的历程只有python2的版本,对于直接上手python3的同学来说,改写还是比较困难的,所以提供给大家以下经过改写的示例代码:

import requestsfrom json import JSONDecoderimport datetimeimport cv2import oshttp_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)
  • 在上述代码中,因为项目需要做图像处理,所以导入了cv2模块,也就是opencv,这是一个用来做手势识别的代码,大家可根据官网提供的说明对人脸检测、识别、对比进行编写并应用到自己的项目当中。因为做的是实时检测,所以调用了opencv,没采集一帧就上传至face++进行识别判断,如果只需要识别单张图片可以直接去掉opencv的内容。
  • 假设要检测人脸,那么只需要把上述的url改为 https://api-cn.faceplusplus.com/facepp/v3/detect
原创粉丝点击