使用face_recognition进行人脸特征检测
效果图
调用face_recognition.face_landmarks()方法即可得到人脸特征点, 返回一个字典, 下图是返回的数据, 包括chin(下巴), left_eye(左眼)等.
我画了两种图, 一种是遍历所有的点, 直接给点画图的图(点用实心圆绘制). 第二个是单独画下巴, 连成线, 用的是polylines方法.
我是4.10版本的opencv. 查阅官方py文档, 这是链接
完整代码:
import face_recognition
import numpy as np
import cv2
image = face_recognition.load_image_file("./data/奥巴马.png")
image2 = image.copy()
face_landmarks_list = face_recognition.face_landmarks(image)
# print(face_landmarks_list)
for each in face_landmarks_list:
print(each)
for i in each.keys():
print(i, end=': ')
print(each[i])
for any in each[i]:
image = cv2.circle(image, any, 3, (0,0,255), -1)
cv2.imshow("奥巴马", image)
# 单独画下巴
for each in face_landmarks_list:
pts = np.array(each['chin'])
pts = pts.reshape((-1, 1, 2))
cv2.polylines(image2, [pts], False, (0, 255, 255)) # false 参数使其不闭合
cv2.imshow("奥巴马2", image2)
cv2.waitKey(0)
cv2.destroyAllWindows()
在线摄像机版本:
import face_recognition
import numpy as np
import cv2
camera = cv2.VideoCapture(0)
while True:
ret, image = camera.read()
image = cv2.flip(image, 1)
image2 = image.copy()
face_landmarks_list = face_recognition.face_landmarks(image)
# print(face_landmarks_list)
for each in face_landmarks_list:
print(each)
for i in each.keys():
print(i, end=': ')
print(each[i])
for any in each[i]:
image = cv2.circle(image, any, 3, (0,0,255), -1)
cv2.imshow("奥巴马", image)
# 单独画下巴
for each in face_landmarks_list:
pts = np.array(each['chin'])
pts = pts.reshap 大专栏 使用face_recognition进行人脸特征检测e((-1, 1, 2))
cv2.polylines(image2, [pts], False, (0, 255, 255)) # false 参数使其不闭合
cv2.imshow("奥巴马2", image2)
if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
break
cv2.destroyAllWindows()
camera.release()
附一份在线的人脸搜索代码, 人脸数据保存在相对路径./data/mans 下
import cv2
import face_recognition
import numpy as np
import os
import re
# 人脸数据, 文件, 编码, 名字
files = os.listdir("./data/mans")
face_images = [0]*len(files)
face_encodings = [0]*len(files)
face_names = [0]*len(files)
# 获取编码和名称
for i in range(len(files)):
face_images[i] = face_recognition.load_image_file('./data/mans/' + files[i])
face_encodings[i] = face_recognition.face_encodings(face_images[i])
if len(face_encodings[i]) > 0:
face_encodings[i] = face_encodings[i][0]
else:
face_encodings[i] = None
face_names[i] = re.findall(r'(.*)..*', files[i])[0]
print(face_names)
# 人脸比较
# results = face_recognition.compare_faces(face_encodings[0], face_encodings[1])
# print(results)
# 人脸距离
# face_distances = face_recognition.face_distance(face_encodings[0], face_encodings[1])
# index = np.argmin(face_distances)
# print(index)
# camera = cv2.VideoCapture('./data/test.avi') # 从视频文件
camera = cv2.VideoCapture(0) # 从摄像头
while True:
ret, img = camera.read()
img = cv2.flip(img, 1)
# img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 灰度处理
locations = face_recognition.face_locations(img)
for top, right, bottom, left in locations:
cv2.rectangle(img, (left, top), (right, bottom), (255, 0, 0), 2)
sub_img = img[top:bottom, left:right]
sub_img_code = face_recognition.face_encodings(sub_img)
if len(sub_img_code) != 0:
face_distances = face_recognition.face_distance(face_encodings, sub_img_code[0])
print(face_distances)
index = np.argmin(face_distances)
name = face_names[index]
cv2.putText(img, name, (left, top - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, 255, 2)
cv2.imshow('Face', img)
if cv2.waitKey(1000 // 12) & 0xff == ord("q"):
break
cv2.destroyAllWindows()
camera.release()
使用face_recognition进行人脸特征检测的更多相关文章
- OpenCV教程(41) 人脸特征检测
在OpenCV中,自带着Harr分类器人脸特征训练的文件,利用这些文件,我们可以很方面的进行人脸,眼睛,鼻子,表情等的检测. 人脸特征文件目录: ../opencv2.46/op ...
- 人工智能之基于face_recognition的人脸检测与识别
不久乘高铁出行,看见高铁火车站已经实现了"刷脸进站",而且效率很高,很感兴趣,今天抽时间研究一下,其实没那么复杂. 我基本上是基于https://github.com/ageitg ...
- OpenCV4.1.0实践(2) - Dlib+OpenCV人脸特征检测
待更! 参考: python dlib opencv 人脸68点特征检测
- Ubuntu下使用face_recognition进行人脸识别
Face Recognition是一个基于Python的人脸识别库,在github上地址如下:https://github.com/ageitgey/face_recognition. 看着挺好玩,本 ...
- face_recognition开源人脸识别库:离线识别率高达99.38%
基于Python的开源人脸识别库:离线识别率高达99.38%——新开源的用了一下感受一下 原创 2017年07月28日 21:25:28 标签: 人脸识别 / 人脸自动定位 / 人脸识别开源库 / f ...
- AI人工智能之基于OpenCV+face_recognition实现人脸识别
因近期公司项目需求,需要从监控视频里识别出人脸信息.OpenCV非常庞大,其中官方提供的人脸模型分类器也可以满足基本的人脸识别,当然我们也可以训练自己的人脸模型数据,但是从精确度和专业程度上讲Open ...
- 在win10上安装face_recognition(人脸识别)
github上有个项目face_recognition,是用于人脸识别的 主要是window上安装这个项目会繁琐些,linux上据项目文档上介绍是妥妥的. 项目地址: https://github. ...
- face_recognition实时人脸识别
具体安装移步:https://www.cnblogs.com/ckAng/p/10981025.html 更多操作移步:https://github.com/ageitgey/face_recogni ...
- Python 人工智能之人脸识别 face_recognition 模块安装
Python人工智能之人脸识别face_recognition安装 face_recognition 模块使用系统环境搭建 系统环境 Ubuntu / deepin操作系统 Python 3.6 py ...
随机推荐
- osi七层模型专题
OSI模型,即开放式通信系统互联参考模型,是国际标准化组织提出的一个试图是各种计算机或者通信系统在世界范围内互联为网络的标准框架.整个模型分为七层,物理层,数据链路层,网络层,传输层,会话层,表示层, ...
- fastDFS 文件系统搭建
1.单机版 https://www.cnblogs.com/chiangchou/p/fastdfs.html#_label3_0 2.集群版
- python3下scrapy爬虫(第八卷:循环爬取网页多页数据)
之前我们做的数据爬取都是单页的现在我们来讲讲多页的 一般方式有两种目标URL循环抓取 另一种在主页连接上找规律,现在我用的案例网址就是 通过点击下一页的方式获取多页资源 话不多说全在代码里(因为刚才写 ...
- jest 测试入门(一)
说实话,作为前端来说,单元测试,并不是一种必须的技能,但是确实一种可以让你加法的技能 之前我一个库添加了单元测试,加完之后感悟颇深,所以写下这篇文章来记录 环境搭建 一般来说,普通的库,如果没有添加 ...
- 日期控件 My97DatePicker WdatePicker 日期格式
WdatePicker()只显示日期 WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})显示日期和时间 WdatePicker({dateFmt:'yyyy-MM ...
- MTF的倾斜边缘计算方法
光学系统性能的衡量方法有很多,常见的有点扩散函数法.瑞利判断法.点 列图法.光学传递函数(MTF)法等,其中 MTF 法在光学系统和镜头加工制造中 使用 最为广泛.MTF 曲线真实的反映了成像系统将物 ...
- remove_if 的效率测试
#include <iostream> #include <functional> #include <vector> #include <algorithm ...
- winform上传文件,利用http,form-data格式上传
/// <summary> /// 上传文件 /// </summary> /// <param name="url">服务地址</par ...
- python-jenkins 操作
Python-Jenkins Doc:http://python-jenkins.readthedocs.io/en/latest/index.html 实例代码: import jenkins je ...
- [ZJOI2019]浙江省选(半平面交)
一眼看上去就应该能用半平面交去做. 首先考虑怎么求可能得第1名的人:每个人的函数为直线,就是在所有人的半平面交中的上边界者即可获得第一名,这个可以单调队列求解. 再考虑如何求可能得第2名的人:满足2个 ...