python 人脸识别,感觉有点棒棒滴~走,同志们让我们一起出发,咱们的目标是:星辰和大海
目录
1、环境安装必要过的坑
首先我先介绍下我的环境 我是win10系统,Python 使用的是Anconda默认的python3.6.3版本!在这里一定要注意,我们使用的python3.6版本,原先的3.5版本由于各种坑,往死的填也填不完,所以我们就简单略过,用跟家快捷的方法,得到我们想要的结果(在这里我不得不夸下我自己,真是鬼才,嘎嘎嘎~)
不多bb,come on 上安装
准备环境之安装依赖
1. 配置安装好:dlib,这个模式轮子网站好像没有了,可以使用源码安装,或者自行百度亲~
2. 然后利用 pip install face_recongnition
轮子安装网站:点这里
当然在我们python3.6版本,不用考虑安装好自己的cmake环境和boost环境
然后直接利用pip install face_recongnition 就好!
然后直接打开我们的cmd进入python 交互界面输入
import face_recognition
如果没有报错就说明这个第三方软件包正常!
2、模块的讲解
为了调用我们的程序我们需要在我们的 python里面安装opencv-python一遍我们调用我们的摄像头! 方便我们进行人工摄像头识别。
接下来让我们走进python face人脸识别,让我们尽情造作吧,看下下面的代码。(咳咳~如有侵权,请本人联系我,我马上改,嘿嘿~)
导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("abm.png")
#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
for face_landmarks in face_landmarks_list:
#打印此图像中每个面部特征的位置
facial_features = [
'chin',
'left_eyebrow',
'right_eyebrow',
'nose_bridge',
'nose_tip',
'left_eye',
'right_eye',
'top_lip',
'bottom_lip'
]
for facial_feature in facial_features:
print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
#让我们在图像中描绘出每个人脸特征!
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)
for facial_feature in facial_features:
d.line(face_landmarks[facial_feature], width=5)
pil_image.show()
运行结果如下:
运行结果如下:
正常识别,脸部轮廓
我们会识别图片的特征轮廓,也就是通过这个方法来识别面部特征
好的,距离我的目标又近了一步,下面乡亲们请看下方图片的各位美女,嘿嘿~,我想要通过代码去识别宝贝们儿的脸部,并很残忍的切割下来(放大看~)
代码实现:
from PIL import Image
import face_recognition
#这是一个jpg的图片!
image = face_recognition.load_image_file('jh.jpg')
face_locations = face_recognition.face_locations(image)
print('I found {} faces in this photograph'.format(len(face_locations)))
for face_location in face_locations:
top,right,bottom,left = face_location
print(
"A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.save(str(top)+'.jpg')
运行结果如下:
不的不说美女就是赏心悦目,不需要发型衬托五官,很棒~
下面我们通过输入一个图片分析它的特征来更好的识别人脸,对比接下来我注入进去的图片中的人脸看看是不是同一个人。
相当于咱们打卡系统:首先录入指纹(美女图片相当于录入的指纹),然后每天个人打卡对比已经录入的指纹(老马就相当于个人),通过奥巴马的脸部来对比美女们的面部
传递: 人脸图像(想传谁都行)
对比的:
import face_recognition
#加入奥巴马图片
abm_image = face_recognition.load_image_file('abm.png')
#这个是全体图片
unknown_image = face_recognition.load_image_file('jh.jpg')
#对图片的数据进行分析
abm_face_encoding = face_recognition.face_encodings(abm_image)[0]
print('chen_face_encoding:{}'.format(abm_face_encoding))
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
print('unknow_face_encoding:{}'.format(unknown_face_encoding))
known_faces = [
abm_face_encoding
]
result = face_recognition.compare_faces(known_faces,unknown_face_encoding)
print('result is {}'.format(result))
print('这个面孔是奥巴马吗{}'.format(result[0]))
print('这个未知的面孔是我们的新面孔吗{}'.format(not True in result ))
运行结果如下:
实战:利用咱们的摄像头来实现人工人脸检测!
现在我们使用调用摄像头方法,来实现脸部对比,前提你要把先录入几张照片,方便摄像头捕捉的脸部进行对比
# -*- coding: utf-8 -*-
# @Time : 2019/1/3 11:28
# @Author : for
# @File : 03_摄像头识别_test.py
# @Software: PyCharm
# -*- coding: utf-8 -*-
# 摄像头头像识别
import face_recognition
import cv2
video_capture = cv2.VideoCapture(0)
# 本地图像
chenduling_image = face_recognition.load_image_file("abm.png")
chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]
# 本地图像二
sunyizheng_image = face_recognition.load_image_file("pj.jpg")
sunyizheng_face_encoding = face_recognition.face_encodings(sunyizheng_image)[0]
# 本地图片三
zhangzetian_image = face_recognition.load_image_file("tlp.jpg")
zhangzetian_face_encoding = face_recognition.face_encodings(zhangzetian_image)[0]
# Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = [
chenduling_face_encoding,
sunyizheng_face_encoding,
zhangzetian_face_encoding
]
# 人物名称的集合
known_face_names = [
"aobama",
"pujing",
"telangpu"
]
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)
# opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
rgb_small_frame = small_frame[:, :, ::-1]
# Only process every other frame of video to save time
if process_this_frame:
# 根据encoding来判断是不是同一个人,是就输出true,不是为flase
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:
# 默认为unknown
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
print(matches)
name = "Unknown"
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):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# 矩形框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
#加上标签
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display
cv2.imshow('monitor', frame)
# 按Q退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
运行结果如下:
正常识别
哎呦~出来了,大帝的名字,请大家忽略我的破手机~
在这里我们要特别提醒下,这个模块对于亚洲的人脸的识别还是不太给力,不过可以通过训练增加准确度,溜了溜了~
修仙去了,再见来不及握手~