人脸识别demo
2019-07-24 21:59
92 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/public669/article/details/97171124
我们知道当今最火的莫过于人工智能了,人工智能指在计算机科学的基础上,综合信息论、心理学、生理学、语言学、逻辑学和数学等知识,制造能模拟人类智能行为的计算机系统的边缘学科。在人工智能的范畴内有两个方向:计算机视觉、自然语音处理(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 : 胡子旋 # @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) 7ff7 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全整理一遍,记录下来,方便自己回忆和分享。如有问题欢迎指正。
相关文章推荐
- C# 离线人脸识别Demo 使用ArcFace 2.0开发完成
- Android静态图片人脸识别的完整demo(附完整源码)
- OpenCV-2.4.6-android-sdk 人脸识别demo搭建
- C# 离线人脸识别Demo 使用ArcFace 2.0开发完成
- Android人脸识别Demo竖屏YUV方向调整和图片保存
- Android人脸识别Demo竖屏YUV方向调整和图片保存
- 人脸识别demo使用教程
- OpenCV-2.4.6-android-sdk 人脸识别demo搭建
- 人脸识别 ArcFaceDemo 第二版 [Android]
- OpenCV-2.4.6-android-sdk 人脸识别demo搭建
- C# ArcFace 免费人脸识别 2.0 demo
- C# 离线人脸识别 ArcSoft V2.0 Demo
- 虹软2.0 离线人脸识别python3 demo
- 安卓开发学习记录--打开已存在的项目(以Android虹软人脸识别sdk的demo为例)
- Android人脸识别Demo竖屏YUV方向调整和图片保存
- 虹软离线人脸识别 ArcFace 2.0 Demo [C++]
- 腾讯云技术专家卢萌凯手把手教你Demo一个人脸识别程序!
- 人脸识别&ORC的Demo
- java版 ArcFace2.0利用人脸识别猜年龄 demo
- 人脸识别ArcFace C#DEMO 开发应用全过程