你所需要的,不仅仅是一个好用的代理。
人脸识别,乍一听还是高大上的东西.
今天我们就分享下怎么玩?
不需要一大坨代码,只要你会装……包.
当然,我们的重点不是装包,归根结底,还是怎么玩.
Ok,那么,我们开始
1.OpenCV 一个图像处理的强大的包.官网 http://opencv.org/,最新的版本3.3.0
2.Python2.7或者Python3.x
3.依赖包NumPY. 可以pip install numpy
4.依赖包face recognition.主角就是他了.pip install face recognition
注意事项参照地址 https://github.com/ageitgey/face_recognition
1.我们首先给定一张被照片
2.从摄像头捕捉的图像里获取检测出头像,跟上面的照片的图片进行对比.
先看下结果:
从截下来的图片来看. 我自己因为没有放图片上去进行识别,所以是unknown的. 手机上的图片被识别到了写上chu(名字) 标签.
代码:
import face_recognition
import cv2
import time
#获取摄像头
video_capture = cv2.VideoCapture(0)
# 加载图片
obama_image = face_recognition.load_image_file("IMG_20170723_213850R.jpg")
# 图片先识别一遍人脸,后面用来比较
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# 初始化变量
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# 从摄像头获取图像
ret, frame = video_capture.read()
# 缩放1/4大小,方便快速处理
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# 保证只处理一遍
if process_this_frame:
#找出所有的人脸
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# 判断是否匹配给定的人脸
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
name = "Unknown"
if match[0]:
name = "CHU<del>"
face_names.append(name)
process_this_frame = not process_this_frame
# 把识别到和为识别到的人脸进行标记
for (top, right, bottom, left), name in zip(face_locations, face_names):
# 原来是1/4的大小,现在放大4倍
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), 2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.5, (255, 255, 255), 3)
#识别到时候进行抓屏
for name in face_names:
if name=="CHU</del>":
cv2.imwrite('img-%d.png'%int(time.time()), frame)
# 显示加了头像框视频
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头句柄
video_capture.release()
cv2.destroyAllWindows()