人脸识别demo
我们知道当今最火的莫过于人工智能了,人工智能指在计算机科学的基础上,综合信息论、心理学、生理学、语言学、逻辑学和数学等知识,制造能模拟人类智能行为的计算机系统的边缘学科。在人工智能的范畴内有两个方向:计算机视觉、自然语音处理(NLP,国内外也有人称NPL)。简介:这里介绍一个demo,同时这个项目是基于计算机视觉的基础上完成的,旨在简单的科普人工智能需要的第三方库import fa...
·
我们知道当今最火的莫过于人工智能了,人工智能指在计算机科学的基础上,综合信息论、心理学、生理学、语言学、逻辑学和数学等知识,制造能模拟人类智能行为的计算机系统的边缘学科。在人工智能的范畴内有两个方向:计算机视觉、自然语音处理(NLP,国内外也有人称NPL)。
- 简介:这里介绍一个demo,同时这个项目是基于计算机视觉的基础上完成的,旨在简单的科普人工智能
- 需要的第三方库
import face_recognition
import cv2
import datetime
import glob2 as gb
相关库介绍
- face_recogniton是世界上最简单的人脸识别库了。你可以通过Python引用或者命令行的形式使用它,来管理和识别人脸,该软件包使用dlib中最先进的人脸识别深度学习算法,使得识别准确率在《Labled Faces in the world》测试基准下达到了99.38%,它同时提供了一个叫face_recognition的命令行工具,以便你可以用命令行对一个文件夹中的图片进行识别操作。
- cv2是Opencv(Open Source Computer Vision Library)的一个扩展库,里面含有各种用于图像处理的函数及进程。可以运作在Linux、Windows和Mac OS操作系统上。
- datetime 是Python处理日期和时间的标准库;可以获取当前日期和时间,也可以获取指定日期和时间等等
- glob2 是python自己带的一个文件操作相关模块,用它可以查找符合自己目的的文件,类似于Windows下的文件搜索,支持通配符操作。
- 代码部分
video_capture = cv2.VideoCapture(0) # 使用cv2打开摄像头获取当前图像
img_path = gb.glob(r'D:\pycharmproject\F_recognition\photo\\*.jpg') # 获取路径
known_face_names = [] # 使用数组获取文件夹下的图片信息
known_face_encodings = []
for i in img_path: # 遍历,通过同文件夹下的图片比对
picture_name = i.replace('D:\pycharmproject\F_recognition\photo\\*.jpg', '')
picture_newname = picture_name.replace('.jpg', '')
someone_img = face_recognition.load_image_file(i)
someone_face_encoding = face_recognition.face_encodings(someone_img)[0]
known_face_names.append(picture_newname)
known_face_encodings.append(someone_face_encoding)
someone_img = []
someone_face_encoding = []
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 i in face_encodings:
match = face_recognition.compare_faces(known_face_encodings, i, tolerance=0.39)
if True in match:
match_index = match.index(True)
name = "match"
# To print name and time
cute_clock = datetime.datetime.now()
print(known_face_names[match_index] + ':' + str(cute_clock))
else:
name = "unknown"
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), (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.0, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
- 效果
- 识别成功
- 识别失败
- 完整代码
# -*- coding: utf-8 -*-
# @Time : 2019/1/4 19:59
# @Author : 陶陶name
# @FileName: Recognition.py
# @Software: PyCharm
# @Email :1017190168@qq.com
import face_recognition
import cv2
import datetime
import glob2 as gb
video_capture = cv2.VideoCapture(0)
img_path = gb.glob(r'D:\pycharmproject\F_recognition\photo\\*.jpg')
known_face_names = []
known_face_encodings = []
for i in img_path:
picture_name = i.replace('D:\pycharmproject\F_recognition\photo\\*.jpg', '')
picture_newname = picture_name.replace('.jpg', '')
someone_img = face_recognition.load_image_file(i)
someone_face_encoding = face_recognition.face_encodings(someone_img)[0]
known_face_names.append(picture_newname)
known_face_encodings.append(someone_face_encoding)
someone_img = []
someone_face_encoding = []
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 i in face_encodings:
match = face_recognition.compare_faces(known_face_encodings, i, tolerance=0.39)
if True in match:
match_index = match.index(True)
name = "match"
# To print name and time
cute_clock = datetime.datetime.now()
print(known_face_names[match_index] + ':' + str(cute_clock))
else:
name = "unknown"
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), (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.0, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
- 总结
- 这是好久之前写的demo了,一直没时间整理,乘着暑期期间,将一些有用项目、demo全整理一遍,记录下来,方便自己回忆和分享。如有问题欢迎指正。
更多推荐
已为社区贡献3条内容
所有评论(0)