基于face++的人脸检测(一)

毕设刚开始搞,所有还是参考了一些网友的现有代码。
首先可以先去face++上注册,有免费试用的,我觉得还不错。注册之后会有个key和secret
这个是我注册得到的,你们也可以自己去申请

参考了http://blog.csdn.net/tinyzhao/article/details/55224183 的API调用代码,可以实现输出检测结果的字典。
这个是我的图片的检测结果
我打算使用opencv把人脸表示出来。
下面是完整的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/1 16:41
# @Author  : He Hangjiang
# @Site    : 
# @File    : face++例程.py
# @Software: PyCharm

import requests
from json import JSONDecoder
import cv2

http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
key = "2PX56nNhKpVd57QNKc9sQt9ASRczWlIr" 
secret = "7gwhn-gVp6J3we_vdj_bIlL1bGpuZdlp"
filepath = "face.jpg"

data = {"api_key": key, "api_secret": secret, "return_landmark": "1"}
files = {"image_file": open(filepath, "rb")}
response = requests.post(http_url, data=data, files=files)

req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)

print(req_dict)

#进过测试前面的程序会返回一个字典,其中指出了人脸所在的矩形的位置和大小等,所以直接进行标注
# mydict = eval(req_dict)
faces = req_dict["faces"]
faceNum = len(faces)
print("识别到了%d个人脸"%(faceNum))

img = cv2.imread(filepath)

for i in range(faceNum):
    face_rectangle = faces[i]['face_rectangle']
    width =  face_rectangle['width']
    top =  face_rectangle['top']
    left =  face_rectangle['left']
    height =  face_rectangle['height']
    start = (left, top)
    end = (left+width, top+height)
    color = (55,255,155)
    thickness = 3
    cv2.rectangle(img, start, end, color, thickness)

cv2.namedWindow("识别后")
cv2.imshow("识别后", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

# print type(resp)

参考博客:
http://blog.csdn.net/tinyzhao/article/details/55224183
http://blog.csdn.net/hongbin_xu/article/details/74981819

猜你喜欢

转载自blog.csdn.net/hehangjiang/article/details/78948777