使用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 ...
随机推荐
- sqlserver修改某列为自增
sqlserver如果建表的时候不设自增,之后是没法直接修改的,需要先删再重设: alter table 表名 drop column ID alter table 表名 add ID int ide ...
- SVN解除与服务器的关联
1.随便找个地方新建个文本文档 2.粘贴如下内容到文档里,建议使用notepad++ Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\ ...
- OutOfMemoryError异常
1.Java堆溢出 Java堆用于存储对象实例,只要不断地创建对象,并且保证GC Roots到对象之间有可达路径来避免垃圾回收机制清除这些对象,就会在对象数量达到最大堆的容量限制后产生内存溢出异常. ...
- eclipse创建文件携带作者时间
windows–>preference Java–>Code Style–>Code Templates code–>new Java files 编辑它 ${filecomm ...
- linux中awk的应用
1.awk的基本认识和使用方法,参考下面链接 https://www.cnblogs.com/timxgb/p/4658631.html 2.awk中关于条件判断的用法,如 https://blog. ...
- MySql数据库,查询数据导出时会出现重复的记录(数据越多越明显)
在查询数据时,数据量多的时候,我们会使用分页功能. 每页显示多少数据. 这种情况下,一半看不出什么问题. 而导出数据时,有时就是通过分页的方法,逐步讲数据追加到导出文件中. 当全部数据都导出之后,就有 ...
- springBoot 使用redis 和 StringRedisTemplate 常用操作
spring boot 使用 redis : 1,pom 引入 redis,貌似springboot 1.5以上的版本,引入redis必须加 <version></version&g ...
- Java基础篇 - 强引用、弱引用、软引用和虚引用
Java基础篇 - 强引用.弱引用.软引用和虚引用 原创零壹技术栈 最后发布于2018-09-09 08:58:21 阅读数 4936 收藏展开前言Java执行GC判断对象是否存活有两种方式其中一种是 ...
- nm命令介绍
一.参考文章 网址1:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/nm.html 参考2: man nm 参考3:<linux ...
- js实现新闻滚动-单行滚动或者多行滚动
注明:都是转载. 先说单行滚动: --------直接复制以下代码即可试验 转载http://www.3lian.com/edu/2011/06-30/4986.html----------- < ...