基于python的面部识别(需导入opencv、face_recognition)
基于python的面部识别:
完成此项目需要导入两个库 opencv、face_recognition
<1>
opevcv 比较好安装,首先导入opencv库
命令符输入pip --version看是否安装有pip(没有的话去官网下载一个pip并解压,然后cmd打开命令输入
python setup.py install (即可安装pip)
如果确认安装了,直接输入:
pip install opencv-python
安装opencv包,等待导入完成即可。
<2>
导入face_recognition,这个可能比较复杂,关于这个库github上有介绍,你可以去看看 :https://github.com/ageitgey/face_recognition#face-recognition
一、如果你本机没有安装vistual studio,就先下载安装,我下载的是目前最新版本:
下载安装好后,提示重新启动电脑。重启后接下来下面步骤。
二、接下来安装boost
通过此链接:https://www.boost.org/users/download/
进入如下页面:
解压boost压缩文件,在cmd界面中,进入boost解压目录中
输入:bootstrap.bat 回车
出现如下界面:
继续输入:.\b2 回车 这个过程比较长,大概四十分钟左右,请耐心等待。
出现下列界面情况,则说明安装成功。
三、接下来安装cmake
通过此链接:https://cmake.org/download/
进入如下页面:
选择加入系统环境( Add CMake to the system PATH for all users)
安装成功后
四、接下来安装dlib
通过此链接:http://dlib.net/
进入如下页面:
下载解压后,打开cmd界面内,进入dlib的解压目录,输入:python setup.py install
最后,重新打开cmd界面内,输入:pip install face_recognition 导入完成即可。
pip list 查看安装库列表 看是否成功导入以上库。
如果确认都完成了。接下来打开python 输入代码即可:
代码如下:
import face_recognition
import cv2
video_capture = cv2.VideoCapture(0)
miaojianxi_image = face_recognition.load_image_file("D:\python工作\PyCharm 工作空间i/08A15DD447960583165D6464CA74CC3C.png")#自己选择图片作为识别依据图片
aobama_image2 = face_recognition.load_image_file("D:\python工作\PyCharm 工作空间i/70JP-fyrwsqk0881071.png")#自己选择图片作为识别依据图片
miaojianxi_face_encoding =face_recognition.face_encodings(miaojianxi_image)[0]
aobama_face_encoding2 =face_recognition.face_encodings(aobama_image2)[0]
known_face_encodings = [
miaojianxi_face_encoding,aobama_face_encoding2
]
known_face_names = [
"Miao miao","aobama" #名字(对应前面识别图片顺序)
]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_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:
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "UNKONWN"
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (255, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (255, 0, 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('Face monitor', frame)
if cv2.waitKey(1) & 0xFF == ord(' '): #设置以空格结束程序
break
video_capture.release()
cv2.destroyAllWindows()
运行效果如图:
只能到这了,希望大家成功完成!
----- ------------ 转载请说明出处谢谢!https://mp.****.net----