文章目录

  • 前言
  • 一、实现人脸识别所需要的库
    • 1.face_recognition库
    • 2.opencv-python
  • 二、下载对应的库文件
    • 1.下载face_recognition库
    • 2.安装opencv-python
  • 三、代码实现
  • 总结

前言

本文为作者学习记录,如有大佬,不喜勿喷,感谢
文章为记录学习的一些流程以及出现的问题解决方法
由于是学习使用,只是使用windows系统作为测试使用

一、实现人脸识别所需要的库

1.face_recognition库

本项目是世界上最简洁的人脸识别库,你可以使用Python和命令行工具提取、识别、操作人脸。

本项目的人脸识别是基于业内领先的C++开源库 dlib中的深度学习模型,用Labeled Faces in the Wild人脸数据集进行测试,有高达99.38%的准确率。但对小孩和亚洲人脸的识别准确率尚待提升。

取自:https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md

对于此库有兴趣的可以观看

2.opencv-python

OpenCV-Python 是一个 Python 绑定库,旨在解决计算机视觉问题。

创立的通用编程语言 Guido van Rossum ,很快就变得非常流行,主要是因为它的简单性和代码可读性。 它使程序员能够在不降低可读性的情况下用更少的代码行来表达想法。

与 C/C++ 等语言相比,Python 速度较慢。 也就是说,Python 可以很容易地用 C/C++ 扩展,这允许我们用 C/C++ 编写计算密集型代码,并创建可以用作 Python 模块的 Python 包装器。 这给了我们两个优势:首先,代码与原始 C/C++ 代码一样快(因为它是在后台工作的实际 C++ 代码),其次,用 Python 编写代码比 C/C++ 更容易。 OpenCV-Python 是原始 OpenCV C++ 实现的 Python 包装器。

OpenCV-Python 使用 Numpy ,这是一个高度优化的库,用于具有 MATLAB 样式语法的数值运算。 所有 OpenCV 数组结构都与 Numpy 数组相互转换。 这也使得与使用 Numpy 的其他库(例如 SciPy 和 Matplotlib)集成变得更加容易。

取自:https://docs.opencv.org/3.4/d0/de3/tutorial_py_intro.html opencv官方网站

内容为网页翻译,如有翻译问题,作者本人也看不明白

二、下载对应的库文件

ps:本人使用的是anaconda

1.下载face_recognition库

打开anaconda prompt
执行命令行操作

pip install face_recognition

windows系统的通常可能会出现个bug
由于dlib库无法安装导致face_recognition无法安装
本人找到的解决方法为

pip install CMake
pip install Boost
pip install dlib

取自:https://blog.csdn.net/weixin_44088439/article/details/87177561
再次执行命令行操作即可完成安装

2.安装opencv-python

打开anaconda prompt
执行命令行操作

pip install opencv-python
pip install opencv-contrib-python

这个方法是作者根据网上的一些教程所得到的,应该下载这两个库
但是在下载opencv的时候可能会出现一些bug,由于问题太多了,就没有记录,如果出现问题,网上搜索应该就可以

三、代码实现

取自:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
这个是GitHub上面一个简单的人脸识别样例,为识别奥巴马和拜登
为了方便,以下为网址内的代码

import face_recognition
import cv2
import numpy as np# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [obama_face_encoding,biden_face_encoding
]
known_face_names = ["Barack Obama","Joe Biden"
]# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# Grab a single frame of videoret, frame = video_capture.read()# Resize frame of video to 1/4 size for faster face recognition processingsmall_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# Find all the faces and face encodings in the current frame of videoface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# See if the face is a match for the known face(s)matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# # If a match was found in known_face_encodings, just use the first one.# if True in matches:#     first_match_index = matches.index(True)#     name = known_face_names[first_match_index]# Or instead, use the known face with the smallest distance to the new faceface_distances = face_recognition.face_distance(known_face_encodings, face_encoding)best_match_index = np.argmin(face_distances)if matches[best_match_index]:name = known_face_names[best_match_index]face_names.append(name)process_this_frame = not process_this_frame# Display the resultsfor (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 sizetop *= 4right *= 4bottom *= 4left *= 4# Draw a box around the facecv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Draw a label with a name below the facecv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting imagecv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

通过分析代码,本人将代码内识别奥巴马以及拜登的部分改为了识别本人
的人脸信息
代码如下:

import face_recognition
import cv2
import numpy as np# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)"""
# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]# Create arrays of known face encodings and their names
known_face_encodings = [obama_face_encoding,biden_face_encoding
]
known_face_names = ["Barack Obama","Joe Biden"
]
"""jzz_image = face_recognition.load_image_file('jzz.jpg')
jzz_face_encoding = face_recognition.face_encodings(jzz_image)[0]known_face_encodings =[jzz_face_encoding
]known_face_names = ["jzz"
]# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# Grab a single frame of videoret, frame = video_capture.read()# Resize frame of video to 1/4 size for faster face recognition processingsmall_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)rgb_small_frame = small_frame[:, :, ::-1]rgb_small_frame = cv2.flip(rgb_small_frame , 1)# Only process every other frame of video to save timeif process_this_frame:# Find all the faces and face encodings in the current frame of videoface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# See if the face is a match for the known face(s)matches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# # If a match was found in known_face_encodings, just use the first one.# if True in matches:#     first_match_index = matches.index(True)#     name = known_face_names[first_match_index]# Or instead, use the known face with the smallest distance to the new faceface_distances = face_recognition.face_distance(known_face_encodings, face_encoding)best_match_index = np.argmin(face_distances)if matches[best_match_index]:name = known_face_names[best_match_index]face_names.append(name)process_this_frame = not process_this_frame#前置摄像头情况下进行反转frame = cv2.flip(frame , 1)# Display the resultsfor (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 sizetop *= 4right *= 4bottom *= 4left *= 4# Draw a box around the facecv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)# Draw a label with a name below the facecv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Display the resulting imagecv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

发现在使用电脑自带摄像头时会出现人物和图像左右翻转的情况,所以添加了一行代码用于反转,如果不需要反转可以删除,代码位于中文注释处
代码如下:

frame = cv2.flip(frame , 1)

总结

以上为本人初学人脸识别时的内容,如有大佬有更好的建议,希望可以指导一下本人

初学python人脸识别相关推荐

  1. python人脸识别、人脸关键点检测、性别检测

    python人脸识别.人脸关键点检测.性别检测 文章目录 根据人脸预测年龄性别和情绪 (python + keras)(三) 一款入门级的人脸.视频.文字检测以及识别的项目. age-gender-e ...

  2. Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38%

    Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38% 仅用 Python 和命令行就可以实现人脸识别的库开源了.该库使用 dlib 顶尖的深度学习人脸识别技术构建 ...

  3. python人脸识别系统下载_简单的Python人脸识别系统

    案例一 导入图片 思路: 1.导入库 2.加载图片 3.创建窗口 4.显示图片 5.暂停窗口 6.关闭窗口 # 1.导入库 import cv2 # 2.加载图片 img = cv2.imread(' ...

  4. python 人脸识别调整人脸大的距离_Python 人脸识别就多简单,看这个就够了!

    原标题:Python 人脸识别就多简单,看这个就够了! 今天给大家介绍一个世界上最简洁的人脸识别库 face_recognition,你可以使用 Python 和命令行工具进行提取.识别.操作人脸. ...

  5. Python | 人脸识别系统 — 用户操作

    本博客为人脸识别系统的摄像头画面展示代码解释 人脸识别系统博客汇总:人脸识别系统-博客索引 项目GitHub地址:Su-Face-Recognition: A face recognition for ...

  6. Python人脸识别黑科技(二):教你使用python+Opencv完成人脸解锁

    继上一篇"Python人脸识别黑科技(一):50行代码运用Python+OpenCV实现人脸追踪+详细教程+快速入门+图像识",那么今天我们来讲关于使用python+opencv+ ...

  7. Python | 人脸识别系统 — 人脸比对 代码部分

    本博客为人脸识别系统的人脸比对代码解释 人脸识别系统博客汇总:人脸识别系统-博客索引 项目GitHub地址:Su-Face-Recognition: A face recognition for us ...

  8. Python人脸识别——从入门到工程实践

    参考书籍:<Python人脸识别从入门到工程实践> 全书共8章: 第 1 章:介绍了人脸识别的基础知识和必备常识: 第 2~4 章:详细讲解了与人脸识别相关的数学.机器学习.计算机视觉.O ...

  9. 【优秀课设】基于OpenCV的Python人脸识别、检测、框选(遍历目录下所有照片依次识别 视频随时标注)

    基于OpenCV的Python人脸识别.检测.框选 (遍历目录下所有照片依次识别 视频随时标注) 移步: https://blog.csdn.net/weixin_53403301/article/d ...

最新文章

  1. Swift 1.1语言快速入门7.2使用无参函数
  2. 使用MVVM绑定AppBar事件
  3. ACM 模板--邻接表 有向图 搜索算法
  4. mysql 之后_MYSQL登陆完之后如何操作???(新手求助)
  5. (转)小波的分解和重构
  6. 数据库建表需要外键约束?
  7. 修改服务器时间需要重启吗,云服务器需要定期重启吗
  8. 《Oracle从入门到精通》读书笔记第四章 SQL语言基础之二
  9. 程序中减少使用if语句的方法集锦
  10. 计算机按键喀秋莎,给自己的小家配置一台巫喀秋莎桌面hifi音响
  11. 拓端tecdat|R语言ISLR工资数据进行多项式回归和样条回归分析
  12. Function.prototype.bind、call与apply方法简介
  13. kuangbin数学训练1
  14. python进行数据抽取_python中的数据抽取
  15. qq附近的人怎么引流?如何利用手机QQ附近功能引流
  16. 品搜妞——百度、谷狗、搜狗的女人三围
  17. 六张思维导图,读懂项目管理
  18. 计算机专业野外考察,野外考察--生存技能
  19. 秋季天凉易感冒 冷水洗脸来预防
  20. android放微信短视频文件,参考微信实现的短视频录像

热门文章

  1. 管道式广谱感应水处理器详细介绍
  2. mongoTemplate日期查询详解
  3. MES如何做好生产过程监控,本文给出了详细解答
  4. centos服务器中木马后的处理和预防
  5. java 生成pdf文件_Java 中HTTP响应数据生成PDF,PDF文件的读取
  6. [教你做小游戏] 用177行代码写个体验超好的五子棋
  7. Python中hasattr的具体用法
  8. rabbitMQ:绑定Exchange发送和接收消息(fanout)
  9. 软件测试面试题:1.怎样看待加班问题?
  10. 资料汇编:关于VR眼镜的参数