zoukankan      html  css  js  c++  java
  • 深度学习构建视频人脸识别模型

    • 人脸识别中有前景的应用
    • 系统准备—— 硬件/软件要求
    • 硬件安装
    • 软件安装
    • 探究Python的实现
    • 简单演练
    • 人脸识别案例

    本文将侧重于人脸识别的实践应用,对算法如何运作只作注释。如果你想了解更多,可以浏览下面这篇文章:

    Understanding and Building an Object Detection Model from Scratch in Python

    https://www.analyticsvidhya.com/blog/2018/06/understanding-building-object-detection-model-python/

    系统准备 —— 硬件/软件要求

    现在你知道了人脸识别技术可以实现的应用,让我们看看如何能够通过可用的开源工具来实现它。这就能体现领域的优势了 —— 分享开源代码的意愿和行动都是其他领域无法比拟的。

    针对本文,以下列出了我使用的和推荐使用的系统配置如下:

    • 一个网络摄像头(罗技 Logitech C920)用来搭建一个实时人脸识别器,并将其安装在联想E470 ThinkPad系列笔记本(英特尔酷睿5第7代内核Core i5 7th Gen)。你也可以使用笔记本电脑自带的摄像头,或电视监控系统摄像头。在任意系统上做实时的视频分析都可以,并不一定是我正在使用的配置。
    • 使用图形处理器(GPU)来加速视频处理会更好。
    • 在软件方面,我们使用Ubuntu 18.04操作系统安装所有必要的软件。

    为了确保搭建模型前的每件事都安排妥当,接下来我们将详细探讨如何配置。

    步骤一:硬件安装

    首要的事就是检查网络摄像头是否正确安装。在Ubuntu上有一个简单小技巧 —— 看一下相应的设备在操作系统中是否已经被注册。你可以照着以下的步骤来做:

      步骤1.1:在连接网络摄像头到笔记本电脑之前,通过在命令提示符窗口输入命令ls /dev/video* 来检查所有连接上的视频设备。这将列出那些已经连接入系统的视频设备。

      步骤1.2:连接网络摄像头并再次执行这一命令,如果网络摄像头被成功连接,将显示一个新的设备。

      步骤1.3:另一件你可以做的事是使用任一网络摄像头软件来检查摄像头能否正常工作,Ubuntu中你可以用“Cheese”来检查。

    步骤二:软件安装

    步骤2.1:安装Python

    本文中的代码是用Python 3.5编写。尽管有多种方法来安装Python,我还是建议使用Anaconda —— 数据科学中最受欢迎的Python发行版。

    步骤2.2:安装OpenCV

    OpenCV(Open Source Computer Vision) 是一个旨在创建计算机视觉应用的软件库。它包含大量预先写好的图像处理功能。要安装OpenCV,需要一个该软件库的pip安装:

    pip3 install opencv-python

    步骤2.3:安装face_recognition应用程序接口

    最后,我们将会使用face_recognition,它号称是世界上最简单的人脸识别应用程序Python接口。安装如下:

    pip install dlib
    pip install face_recognition

    简单流程

    首先,创建一个face_detector.py文件同时复制下面的代码:

     # import librariesimport cv2import face_recognition# Get a reference to webcam video_capture = cv2.VideoCapture("/dev/video1")# Initialize variablesface_locations = []while True:
     # Grab a single frame of video
     ret, frame = video_capture.read()
     # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
     rgb_frame = frame[:, :, ::-1]
     # Find all the faces in the current frame of video
     face_locations = face_recognition.face_locations(rgb_frame)
     # Display the results
     for top, right, bottom, left in face_locations:
     # Draw a box around the face
     cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
     # Display the resulting image
     cv2.imshow('Video', frame)
     # Hit 'q' on the keyboard to quit!
     if cv2.waitKey(1) & 0xFF == ord('q'):
     break# Release handle to the webcamvideo_capture.release()cv2.destroyAllWindows()

    然后,用如下命令执行这个Python文件:

    python face_detector.py

    如果一切运行正确,会弹出一个新窗口,以运行实时的人脸识别。

     

    总结一下,以上的代码做了这些:

    • 首先,我们配置了运行影像分析的硬件。
    • 接下来我们逐帧地捕捉实时影像。
    • 然后我们处理每一帧并且提取图像中所有人脸的位置。
    • 最后,我们以视频形式描绘出这些帧,同时标注人脸的位置。

     

     


     

    人脸识别案例

    例如,你想搭建一个自动的摄像头定位系统,来实时跟踪演讲者的位置。根据他的位置,系统会转动摄像头使得演讲者总是处于视频的中间。

    我们怎么做到这一点?第一步就是要搭建一个可辨识视频中人(们)的系统,并且重点放在演讲者的位置。

    首先,我们引入必要的代码库:

    import cv2
    import face_recognition

    然后,读入影片并得到影片长度:

    input_movie = cv2.VideoCapture("sample_video.mp4")
    length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))

    随后我们新建一个输出文件,使其拥有和输入文件相似的分辨率和帧频。

    载入演讲者的一个影像样本,来识别视频中的主角:

    input_movie = cv2.VideoCapture("sample_video.mp4")
    length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))

    做完这些,现在我们可以执行一个循环来完成如下步骤:

    • 从影片中提取一帧影像
    • 找到所有的人脸并完成识别
    • 新建一个影片,来将源帧图像和标注演讲者脸部的位置合并起来

    让我们看看这部分代码:

     # Initialize variablesface_locations = []face_encodings = []face_names = []frame_number = 0while True:
     # Grab a single frame of video
     ret, frame = input_movie.read()
     frame_number += 1
     # Quit when the input video file ends
     if not ret:
     break
     # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
     rgb_frame = frame[:, :, ::-1]
     # Find all the faces and face encodings in the current frame of video
     face_locations = face_recognition.face_locations(rgb_frame, model="cnn")
     face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
     face_names = []
     for face_encoding in face_encodings:
     # See if the face is a match for the known face(s)
     match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50)
     name = None
     if match[0]:
     name = "Phani Srikant"
     face_names.append(name)
     # Label the results
     for (top, right, bottom, left), name in zip(face_locations, face_names):
     if not name:
     continue
     # Draw a box around the face
     cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
     # Draw a label with a name below the face
     cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
     font = cv2.FONT_HERSHEY_DUPLEX
     cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
     # Write the resulting image to the output video file
     print("Writing frame {} / {}".format(frame_number, length))
     output_movie.write(frame)# All done!input_movie.release()cv2.destroyAllWindows()

    这段代码将输出如下类似的结果

     

     

     

     

     

     

     

     

  • 相关阅读:
    网站页面性能优化的 34条黄金守则 (雅虎团队经验)
    进程调度算法小结
    玩转TCP连接
    数据包在网络中的流转
    浅入理解JVM虚拟机
    Leecode no.47 全排列 II
    Leecode no.143 重排链表
    关于我用设计模式对公司代码重构的这件事
    进程间通信方式小结
    Leecode no.82 删除排序链表中的重复元素 II
  • 原文地址:https://www.cnblogs.com/klsfct/p/10716613.html
Copyright © 2011-2022 走看看