基于face++的人脸检测(一)
程序员文章站
2022-07-12 20:46:45
...
毕设刚开始搞,所有还是参考了一些网友的现有代码。
首先可以先去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
推荐阅读
-
一个基于Net Core3.0的WPF框架Hello World实例
-
C#编写了一个基于Lucene.Net的搜索引擎查询通用工具类:SearchEngineUtil
-
python中使用OpenCV进行人脸检测的例子
-
创建基于ASP.NET core 3.1 的RazorPagesMovie项目(一)-创建和使用默认的模板
-
一个可交互的并发ping检测脚本
-
在Ubuntu上搭建一个基于webrtc的多人视频聊天服务实例代码详解
-
一个可分页的基于文本的PHP留言板源码第1/2页
-
统一通信与基于服务的云计算结合
-
一个检测OpenSSL心脏出血漏洞的Python脚本分享
-
一行代码在Linux服务器上搭建基于.Net Core的博客