1.安装

首先,必须提前安装cmake、numpy、dlib,其中,由于博主所用的python版本是3.6.4(为了防止不兼容,所以用之前的版本),只能安装19.7.0及之前版本的dlib,所以直接pip install dlib会报错,需要pip install dlib==19.7.0

安装完预备库之后就可以直接pip install face_recognition

2.应用

(1)提取人脸

  1. import face_recognition
  2. from PIL import Image
  3. image = face_recognition.load_image_file("1.jpg")
  4. face_locations = face_recognition.face_locations(image) # top, right, bottom, left
  5. #以下展示提取的人脸
  6. for face_location in face_locations:
  7. # Print the location of each face in this image
  8. top, right, bottom, left = face_location
  9. # You can access the actual face itself like this:
  10. face_image = image[top:bottom, left:right]
  11. pil_image = Image.fromarray(face_image)
  12. pil_image.show()

(2)查找面部特征轮廓线

  1. import face_recognition
  2. from PIL import Image,ImageDraw
  3. image = face_recognition.load_image_file("1.jpg")
  4. face_landmarks_list = face_recognition.face_landmarks(image)
  5. #以下为展示轮廓线
  6. pil_image = Image.fromarray(image)
  7. d = ImageDraw.Draw(pil_image)
  8. for face_landmarks in face_landmarks_list:
  9. facial_features = [
  10. 'chin',
  11. 'left_eyebrow',
  12. 'right_eyebrow',
  13. 'nose_bridge',
  14. 'nose_tip',
  15. 'left_eye',
  16. 'right_eye',
  17. 'top_lip',
  18. 'bottom_lip'
  19. ]
  20. for facial_feature in facial_features:
  21. d.line(face_landmarks[facial_feature], width=5)
  22. del d
  23. pil_image.show()

(3)比较人脸

  1. import face_recognition
  2. known_image = face_recognition.load_image_file("known_person.jpg")
  3. unknown_image = face_recognition.load_image_file("unknown.jpg")
  4. biden_encoding = face_recognition.face_encodings(known_image)[0]
  5. unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
  6. results = face_recognition.compare_faces([biden_encoding], unknown_encoding)

(4)同时识别多张人脸

  1. ①使用pillow
  2. #使用pillow库
  3. import face_recognition
  4. from PIL import Image, ImageDraw
  5. # Load a second sample picture and learn how to recognize it.
  6. first_image = face_recognition.load_image_file("3.jpg")
  7. first_face_encoding = face_recognition.face_encodings(first_image)[0]
  8. second_image = face_recognition.load_image_file("5.jpg")
  9. second_face_encoding = face_recognition.face_encodings(second_image)[0]
  10. # Create arrays of known face encodings and their names
  11. known_face_encodings = [
  12. first_face_encoding,
  13. second_face_encoding
  14. ]
  15. known_face_names = [
  16. "first",
  17. "second"
  18. ]
  19. # Load an image with an unknown face
  20. unknown_image = face_recognition.load_image_file("1.jpg")
  21. # Find all the faces and face encodings in the unknown image
  22. unknown_face_locations = face_recognition.face_locations(unknown_image)
  23. unknown_face_encodings = face_recognition.face_encodings(unknown_image, unknown_face_locations)
  24. pil_image = Image.fromarray(unknown_image)
  25. # Create a Pillow ImageDraw Draw instance to draw with
  26. draw = ImageDraw.Draw(pil_image)
  27. # Loop through each face found in the unknown image
  28. for (top, right, bottom, left), unknown_face_encoding in zip(unknown_face_locations, unknown_face_encodings):
  29. # See if the face is a match for the known face(s)
  30. matches = face_recognition.compare_faces(known_face_encodings, unknown_face_encoding, tolerance=0.5)
  31. name = "Unknown"
  32. # If a match was found in known_face_encodings, just use the first one.
  33. if True in matches:
  34. first_match_index = matches.index(True)
  35. name = known_face_names[first_match_index]
  36. # Draw a box around the face using the Pillow module
  37. draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))
  38. # Draw a label with a name below the face
  39. text_width, text_height = draw.textsize(name)
  40. draw.rectangle(((left, bottom-text_height-10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
  41. draw.text((left+6, bottom-text_height-3), name, fill=(255, 255, 255, 255))
  42. # Remove the drawing library from memory as per the Pillow docs
  43. del draw
  44. # Display the resulting image
  45. pil_image.show()
  46. ②使用opencv
  47. #使用opencv库
  48. import face_recognition
  49. import cv2
  50. # 人物名称的集合
  51. known_face_names = ["first","second"]
  52. face_locations = []
  53. face_encodings = []
  54. demo_names = []
  55. process_this_demo = True
  56. # 本地图像一
  57. first_image = face_recognition.load_image_file("1.jpg")
  58. first_encoding = face_recognition.face_encodings(first_image)[0]
  59. # 本地图像二
  60. second_image = face_recognition.load_image_file("5.jpg")
  61. second_encoding = face_recognition.face_encodings(second_image)[0]
  62. known_face_encodings = [first_encoding,second_encoding]
  63. # demo
  64. path = "7.jpg"
  65. demo = cv2.imread(path)
  66. demo_image = face_recognition.load_image_file(path)
  67. demo_encodings = face_recognition.face_encodings(demo_image)
  68. rgb_demo = demo[:, :, ::-1]
  69. demo_face_locations = face_recognition.face_locations(rgb_demo)
  70. for demo_encoding in demo_encodings:
  71. # 默认为unknown
  72. matches = face_recognition.compare_faces(known_face_encodings, demo_encoding,tolerance=0.5)
  73. name = "unknown"
  74. if True in matches:
  75. first_match_index = matches.index(True)
  76. name = known_face_names[first_match_index]
  77. demo_names.append(name)
  78. # 将捕捉到的人脸显示出来
  79. for (top, right, bottom, left), name in zip(demo_face_locations, demo_names):
  80. # Scale back up face locations since the demo we detected in was scaled to 1/4 size
  81. # 矩形框
  82. cv2.rectangle(demo, (left, top), (right, bottom), (0, 0, 255), thickness=1)
  83. #加上标签
  84. cv2.rectangle(demo, (left, bottom-15), (right, bottom), (0, 0, 255), cv2.FILLED)
  85. font = cv2.FONT_HERSHEY_DUPLEX
  86. cv2.putText(demo, name, (left+5,bottom-3), font, 0.5, (255, 255, 255), 1 )
  87. # Display
  88. cv2.imshow("CJK's practice", demo)
  89. cv2.waitKey(0)
  90. cv2.destroyAllWindows()

(5)摄像头实时辨别人脸

  1. import face_recognition
  2. import cv2,time
  3. video_capture = cv2.VideoCapture(0)
  4. # 本地图像一
  5. first_image = face_recognition.load_image_file("1.jpg")
  6. first_face_encoding = face_recognition.face_encodings(first_image)[0]
  7. # 本地图像二
  8. second_image = face_recognition.load_image_file("3.jpg")
  9. second_face_encoding = face_recognition.face_encodings(second_image)[0]
  10. # 本地图片三
  11. third_image = face_recognition.load_image_file("5.jpg")
  12. third_face_encoding = face_recognition.face_encodings(third_image)[0]
  13. # Create arrays of known face encodings and their names
  14. # 脸部特征数据的集合
  15. known_face_encodings = [
  16. first_face_encoding,
  17. second_face_encoding,
  18. third_face_encoding
  19. ]
  20. # 人物名称的集合
  21. known_face_names = [
  22. "first",
  23. "second",
  24. "third"
  25. ]
  26. face_locations = []
  27. face_encodings = []
  28. face_names = []
  29. process_this_frame = True
  30. while True:
  31. # 读取摄像头画面
  32. ret, frame = video_capture.read()
  33. # 改变摄像头图像的大小,图像小,所做的计算就少
  34. small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  35. # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
  36. rgb_small_frame = small_frame[:, :, ::-1]
  37. # Only process every other frame of video to save time
  38. if process_this_frame:
  39. # 根据encoding来判断是不是同一个人,是就输出true,不是为flase
  40. face_locations = face_recognition.face_locations(rgb_small_frame)
  41. face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
  42. face_names = []
  43. for face_encoding in face_encodings:
  44. # 默认为unknown
  45. matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
  46. name = "Unknown"
  47. if True in matches:
  48. first_match_index = matches.index(True)
  49. name = known_face_names[first_match_index]
  50. face_names.append(name)
  51. process_this_frame = not process_this_frame
  52. # 将捕捉到的人脸显示出来
  53. for (top, right, bottom, left), name in zip(face_locations, face_names):
  54. # Scale back up face locations since the frame we detected in was scaled to 1/4 size
  55. top *= 4
  56. right *= 4
  57. bottom *= 4
  58. left *= 4
  59. # 矩形框
  60. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  61. #加上标签
  62. cv2.rectangle(frame, (left, bottom-15), (right, bottom), (0, 0, 255), cv2.FILLED)
  63. font = cv2.FONT_HERSHEY_DUPLEX
  64. cv2.putText(frame, name, (left+5, bottom-3), font, 1.0, (255, 255, 255), 1)
  65. # Display
  66. cv2.imshow('monitor', frame)
  67. # 按Q退出
  68. if cv2.waitKey(1) & 0xFF == ord('q'):
  69. break
  70. video_capture.release()
  71. cv2.destroyAllWindows()

python face_recognition安装及各种应用的更多相关文章

  1. 手把手教你用1行代码实现人脸识别 --Python Face_recognition

    环境要求: Ubuntu17.10 Python 2.7.14 环境搭建: 1. 安装 Ubuntu17.10 > 安装步骤在这里 2. 安装 Python2.7.14 (Ubuntu17.10 ...

  2. Python的安装和详细配置

    Python是一种面向对象.解释型计算机程序设计语言.被认为是比较好的胶水语言.至于其他的,你可以去百度一下.本文仅介绍python的安装和配置,供刚入门的朋友快速搭建自己的学习和开发环境.本人欢迎大 ...

  3. python requests 安装

    在 windows 系统下,只需要输入命令 pip install requests ,即可安装. 在 linux 系统下,只需要输入命令 sudo  pip install requests ,即可 ...

  4. Python 的安装与配置(Windows)

    Python2.7安装配置 python的官网地址:https://www.python.org/ 我这里下载的是python2.7.12版本的 下载后点击安装文件,直接点击下一步知道finally完 ...

  5. 初学python之安装Jupyter notebook

    一开始安装python的时候,安装的是最新版的python3.6的最新版.而且怕出问题,选择的都是默认安装路径.以为这样总不会出什么问题.一开始确实这样,安装modgodb等一切顺利.然而在安装jup ...

  6. 转: python如何安装pip和easy_installer工具

    原文地址: http://blog.chinaunix.net/uid-12014716-id-3859827.html 1.在以下地址下载最新的PIP安装文件:http://pypi.python. ...

  7. CentOS 6.5升级Python和安装IPython

    <转自:http://www.noanylove.com/2014/10/centos-6-5-sheng-ji-python-he-an-zhuang-ipython/>自己常用.以做备 ...

  8. python Scrapy安装和介绍

    python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...

  9. window下从python开始安装科学计算环境

    Numpy等Python科学计算包的安装与配置 参考: 1.下载并安装 http://www.jb51.net/article/61810.htm 1.安装easy_install,就是为了我们安装第 ...

随机推荐

  1. 宜宾市黑烟车电子抓拍系统App

    2020.11 - 2021.06负责手机App开发 项目说明:    主要用于管理人员的移动办公,通过与管理平台共享数据库,实现:人工审核.推送交警.账户管理.信息查询.数据统计.点位电子地图.设备 ...

  2. CF708C Centroids(树形DP)

    发现变重心就是往重心上割,所以\(\text{up and down}\),一遍统计子树最大\(size\),一遍最优割子树,\(down\),\(up\)出信息,最后\(DFS\)出可行解 #inc ...

  3. LuoguP3047 [USACO12FEB]附近的牛Nearby Cows(树形DP,容斥)

    \[f[u][step] = \begin{cases} C[u] & step = 0 \\ (\sum{f[v][step - 1]}) - f[u][step - 2] \cdot (d ...

  4. JavaScript 异步编程(一):认识异步编程

    前言 "异步"的大规模流行是在 Web 2.0浪潮中,它伴随着 AJAX 席卷了 Web.前端充斥了各种 AJAX 和事件,这些都是典型的异步应用场景.现在的 Web 应用已经不再 ...

  5. Shell第三章《for循环》

    Shell循环:for 语法结构: for 变量名 [ in 取值列表 ] do 循环体 done 需求:自动创建10个用户 #!/bin/bash read -p "请输入你要创建的用户名 ...

  6. CF914G Sum the Fibonacci (快速沃尔什变换FWT + 子集卷积)

    题面 题解 这是一道FWT和子集卷积的应用题. 我们先设 cnt[x] 表示 Si = x 的 i 的数量,那么 这里的Nab[x]指满足条件的 Sa|Sb=x.Sa&Sb=0 的(a,b)二 ...

  7. C# using()的本质

    " 程序世界没有秘密,所有答案都在源码里 " 01.点明观点 C#中,非托管资源使用之后必须释放,而using()是使用非托管资源的最佳方式,可以确保资源在代码块结束之后被正确释放 ...

  8. KingbaseES 归档日志清理

    WAL是Write Ahead Log的简写,和Oracle的redo日志类似,在R3版本存放在data/sys_log中,R6版本以后在data/sys_wal目录,在数据库访问过程中,任何对数据块 ...

  9. K8S部署超过节点的Pod

    在阿里云上部署了一个K8S集群,一master, 两node: 然后执行 kubectl create -f tomcat.yml yaml如下: apiVersion: apps/v1 kind: ...

  10. 部署Zabbix4.0和Grafana

    部署Zabbix4.0和Grafana 一.Zabbix 1.安装 rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-r ...