face_recognition人脸识别框架

 

2018年08月13日 15:44:34

阅读数:18

一、环境搭建

1.系统环境

 
  1. Ubuntu 17.04

  2. Python 2.7.14

  3. pycharm 开发工具

2.开发环境,安装各种系统包

  • 人脸检测基于dlib,dlib依赖Boost和cmake
 
  1. $ sudo apt-get install build-essential cmake

  2. $ sudo apt-get install libgtk-3-dev

  3. $ sudo apt-get install libboost-all-dev

  • 其他重要的包
 
  1. $ pip install numpy

  2. $ pip install scipy

  3. $ pip install opencv-python

  4. $ pip install dlib

  • 安装 face_recognition
 
  1. # 安装 face_recognition

  2. $ pip install face_recognition

  3. # 安装face_recognition过程中会自动安装 numpy、scipy 等


二、使用教程

1、facial_features文件夹

此demo主要展示了识别指定图片中人脸的特征数据,下面就是人脸的八个特征,我们就是要获取特征数据

 
  1. 'chin',

  2. 'left_eyebrow',

  3. 'right_eyebrow',

  4. 'nose_bridge',

  5. 'nose_tip',

  6. 'left_eye',

  7. 'right_eye',

  8. 'top_lip',

  9. 'bottom_lip'

运行结果:

自动识别图片中的人脸,并且识别它的特征

原图:


特征数据,数据就是运行出来的矩阵,也就是一个二维数组

代码:

 
  1. # -*- coding: utf-8 -*-

  2. # 自动识别人脸特征

  3. # filename : find_facial_features_in_picture.py

  4.  
  5. # 导入pil模块 ,可用命令安装 apt-get install python-Imaging

  6. from PIL import Image, ImageDraw

  7. # 导入face_recogntion模块,可用命令安装 pip install face_recognition

  8. import face_recognition

  9.  
  10. # 将jpg文件加载到numpy 数组中

  11. image = face_recognition.load_image_file("chenduling.jpg")

  12.  
  13. #查找图像中所有面部的所有面部特征

  14. face_landmarks_list = face_recognition.face_landmarks(image)

  15.  
  16. print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

  17.  
  18. for face_landmarks in face_landmarks_list:

  19.  
  20. #打印此图像中每个面部特征的位置

  21. facial_features = [

  22. 'chin',

  23. 'left_eyebrow',

  24. 'right_eyebrow',

  25. 'nose_bridge',

  26. 'nose_tip',

  27. 'left_eye',

  28. 'right_eye',

  29. 'top_lip',

  30. 'bottom_lip'

  31. ]

  32.  
  33. for facial_feature in facial_features:

  34. print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

  35.  
  36. #让我们在图像中描绘出每个人脸特征!

  37. pil_image = Image.fromarray(image)

  38. d = ImageDraw.Draw(pil_image)

  39.  
  40. for facial_feature in facial_features:

  41. d.line(face_landmarks[facial_feature], width=5)

  42.  
  43. pil_image.show()

2、find_face文件夹

不仅能识别出来所有的人脸,而且可以将其截图挨个显示出来,打印在前台窗口

原始的图片

这里写图片描述

识别的图片

这里写图片描述

代码:

 
  1. # -*- coding: utf-8 -*-

  2. # 识别图片中的所有人脸并显示出来

  3. # filename : find_faces_in_picture.py

  4.  
  5. # 导入pil模块 ,可用命令安装 apt-get install python-Imaging

  6. from PIL import Image

  7. # 导入face_recogntion模块,可用命令安装 pip install face_recognition

  8. import face_recognition

  9.  
  10. # 将jpg文件加载到numpy 数组中

  11. image = face_recognition.load_image_file("yiqi.jpg")

  12.  
  13. # 使用默认的给予HOG模型查找图像中所有人脸

  14. # 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速

  15. # 另请参见: find_faces_in_picture_cnn.py

  16. face_locations = face_recognition.face_locations(image)

  17.  
  18. # 使用CNN模型

  19. # face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

  20.  
  21. # 打印:我从图片中找到了 多少 张人脸

  22. print("I found {} face(s) in this photograph.".format(len(face_locations)))

  23.  
  24. # 循环找到的所有人脸

  25. for face_location in face_locations:

  26.  
  27. # 打印每张脸的位置信息

  28. top, right, bottom, left = face_location

  29. print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

  30. # 指定人脸的位置信息,然后显示人脸图片

  31. face_image = image[top:bottom, left:right]

  32. pil_image = Image.fromarray(face_image)

  33. pil_image.show()

 3、know_face文件夹

通过设定的人脸图片识别未知图片中的人脸

 
  1. # -*- coding: utf-8 -*-

  2. # 识别人脸鉴定是哪个人

  3.  
  4. # 导入face_recogntion模块,可用命令安装 pip install face_recognition

  5. import face_recognition

  6.  
  7. #将jpg文件加载到numpy数组中

  8. chen_image = face_recognition.load_image_file("chenduling.jpg")

  9. #要识别的图片

  10. unknown_image = face_recognition.load_image_file("sunyizheng.jpg")

  11.  
  12. #获取每个图像文件中每个面部的面部编码

  13. #由于每个图像中可能有多个面,所以返回一个编码列表。

  14. #但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。

  15. chen_face_encoding = face_recognition.face_encodings(chen_image)[0]

  16. print("chen_face_encoding:{}".format(chen_face_encoding))

  17. unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]

  18. print("unknown_face_encoding :{}".format(unknown_face_encoding))

  19.  
  20. known_faces = [

  21. chen_face_encoding

  22. ]

  23. #结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果

  24. results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

  25.  
  26. print("result :{}".format(results))

  27. print("这个未知面孔是 陈都灵 吗? {}".format(results[0]))

  28. print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))

4、video文件夹

通过调用电脑摄像头动态获取视频内的人脸,将其和我们指定的图片集进行匹配,可以告知我们视频内的人脸是否是我们设定好的

实现:

代码:

 
  1. # -*- coding: utf-8 -*-

  2. # 摄像头头像识别

  3. import face_recognition

  4. import cv2

  5.  
  6. video_capture = cv2.VideoCapture(0)

  7.  
  8. # 本地图像

  9. chenduling_image = face_recognition.load_image_file("chenduling.jpg")

  10. chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]

  11.  
  12. # 本地图像二

  13. sunyizheng_image = face_recognition.load_image_file("sunyizheng.jpg")

  14. sunyizheng_face_encoding = face_recognition.face_encodings(sunyizheng_image)[0]

  15.  
  16. # 本地图片三

  17. zhangzetian_image = face_recognition.load_image_file("zhangzetian.jpg")

  18. zhangzetian_face_encoding = face_recognition.face_encodings(zhangzetian_image)[0]

  19.  
  20. # Create arrays of known face encodings and their names

  21. # 脸部特征数据的集合

  22. known_face_encodings = [

  23. chenduling_face_encoding,

  24. sunyizheng_face_encoding,

  25. zhangzetian_face_encoding

  26. ]

  27.  
  28. # 人物名称的集合

  29. known_face_names = [

  30. "michong",

  31. "sunyizheng",

  32. "chenduling"

  33. ]

  34.  
  35. face_locations = []

  36. face_encodings = []

  37. face_names = []

  38. process_this_frame = True

  39.  
  40. while True:

  41. # 读取摄像头画面

  42. ret, frame = video_capture.read()

  43.  
  44. # 改变摄像头图像的大小,图像小,所做的计算就少

  45. small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

  46.  
  47. # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。

  48. rgb_small_frame = small_frame[:, :, ::-1]

  49.  
  50. # Only process every other frame of video to save time

  51. if process_this_frame:

  52. # 根据encoding来判断是不是同一个人,是就输出true,不是为flase

  53. face_locations = face_recognition.face_locations(rgb_small_frame)

  54. face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

  55.  
  56. face_names = []

  57. for face_encoding in face_encodings:

  58. # 默认为unknown

  59. matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

  60. name = "Unknown"

  61.  
  62. # if match[0]:

  63. # name = "michong"

  64. # If a match was found in known_face_encodings, just use the first one.

  65. if True in matches:

  66. first_match_index = matches.index(True)

  67. name = known_face_names[first_match_index]

  68. face_names.append(name)

  69.  
  70. process_this_frame = not process_this_frame

  71.  
  72. # 将捕捉到的人脸显示出来

  73. for (top, right, bottom, left), name in zip(face_locations, face_names):

  74. # Scale back up face locations since the frame we detected in was scaled to 1/4 size

  75. top *= 4

  76. right *= 4

  77. bottom *= 4

  78. left *= 4

  79.  
  80. # 矩形框

  81. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

  82.  
  83. #加上标签

  84. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)

  85. font = cv2.FONT_HERSHEY_DUPLEX

  86. cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

  87.  
  88. # Display

  89. cv2.imshow('monitor', frame)

  90.  
  91. # 按Q退出

  92. if cv2.waitKey(1) & 0xFF == ord('q'):

  93. break

  94.  
  95. video_capture.release()

  96. cv2.destroyAllWindows()

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值