开发环境和用到的库:

  • Ubuntu
  • jupyter notebook(python3.6)
  • OpenCV
  • Dlib
  • face_recognition

实现效果如下(视频截图):

 #coding=utf-8
#人脸识别类 - 使用face_recognition模块
import cv2
import face_recognition
import os path = "img/face_recognition" # 模型数据图片目录
cap = cv2.VideoCapture(0)
total_image_name = []
total_face_encoding = []
for fn in os.listdir(path): #fn 表示的是文件名q
print(path + "/" + fn)
total_face_encoding.append(
face_recognition.face_encodings(
face_recognition.load_image_file(path + "/" + fn))[0])
fn = fn[:(len(fn) - 4)] #截取图片名(这里应该把images文件中的图片名命名为为人物名)
total_image_name.append(fn) #图片名字列表
while (1):
ret, frame = cap.read()
# 发现在视频帧所有的脸和face_enqcodings
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
# 在这个视频帧中循环遍历每个人脸
for (top, right, bottom, left), face_encoding in zip(
face_locations, face_encodings):
# 看看面部是否与已知人脸相匹配。
for i, v in enumerate(total_face_encoding):
match = face_recognition.compare_faces(
[v], face_encoding, tolerance=0.5)
name = "Unknown"
if match[0]:
name = total_image_name[i]
break
# 画出一个框,框住脸
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 255), 2)
# 画出一个带名字的标签,放在框下
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 255, 255),
cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0,
(255, 255, 255), 1)
# 显示结果图像
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break cap.release()
cv2.destroyAllWindows()

最新文章

  1. Spket在Eclipse下的安装和配置(图文教程)
  2. ueditor .net版本上传图片功能配置
  3. Mac Pro Office Word 2011 个性化设置
  4. django 模版语法及使用
  5. (DFS)hdoj1241-Oil Deposit
  6. SQLite在多线程环境下的应用
  7. Samsung Galaxy S II GT-I9100 指令全集 部分指令请慎用
  8. HDU4864:Task(贪心)
  9. 在eclipse中将java导出为可执行文件.
  10. [IDEs]Eclipse自动格式化代码
  11. vc操作电脑之常用命令
  12. windows server 2012 R2汉化 -- 玩转Microsoft Azure
  13. redis 一些操作命令
  14. 我的idea突然没有SVN了是怎么回事
  15. git的介绍
  16. SharePoint 中时间轴 Timeline的实现
  17. box-shadow 边框样式
  18. autocomplete 自动填充 combobox
  19. 微软Silverlight欲攻占iPhone和Android手机
  20. Jupyter notebook用法

热门文章

  1. linux的CentOS、Ubuntu、Debian三个比较异同
  2. sql优化案例1
  3. Dev常用控件
  4. Spring Bean 的加载过程
  5. PHP实现笛卡尔积算法
  6. JavaSE---用户交互---获取键盘输入
  7. mysql添加索引和sql分析
  8. 【Tomcat】使用Tomcat部署Spring Boot项目生成的jar包
  9. Linux桌面与命令行切换
  10. 团队冲刺DAY7