深度學習構建視頻人臉識別模型

  • 人臉識別中有前景的應用
  • 系統準備—— 硬件/軟件要求
  • 硬件安裝
  • 軟件安裝
  • 探究Python的實現
  • 簡單演練
  • 人臉識別案例

本文將側重於人臉識別的實踐應用,對算法如何運做只做註釋。如果你想了解更多,能夠瀏覽下面這篇文章:python

Understanding and Building an Object Detection Model from Scratch in Pythongit

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

 

系統準備 —— 硬件/軟件要求

如今你知道了人臉識別技術能夠實現的應用,讓咱們看看如何可以經過可用的開源工具來實現它。這就能體現領域的優點了 —— 分享開源代碼的意願和行動都是其餘領域沒法比擬的。web

針對本文,如下列出了我使用的和推薦使用的系統配置以下:算法

  • 一個網絡攝像頭(羅技 Logitech C920)用來搭建一個實時人臉識別器,並將其安裝在聯想E470 ThinkPad系列筆記本(英特爾酷睿5第7代內核Core i5 7th Gen)。你也能夠使用筆記本電腦自帶的攝像頭,或電視監控系統攝像頭。在任意系統上作實時的視頻分析均可以,並不必定是我正在使用的配置。
  • 使用圖形處理器(GPU)來加速視頻處理會更好。
  • 在軟件方面,咱們使用Ubuntu 18.04操做系統安裝全部必要的軟件。

爲了確保搭建模型前的每件事都安排穩當,接下來咱們將詳細探討如何配置。網絡

步驟一:硬件安裝app

首要的事就是檢查網絡攝像頭是否正確安裝。在Ubuntu上有一個簡單小技巧 —— 看一下相應的設備在操做系統中是否已經被註冊。你能夠照着如下的步驟來作:ide

  步驟1.1:在鏈接網絡攝像頭到筆記本電腦以前,經過在命令提示符窗口輸入命令ls /dev/video* 來檢查全部鏈接上的視頻設備。這將列出那些已經鏈接入系統的視頻設備。工具

  步驟1.2:鏈接網絡攝像頭並再次執行這一命令,若是網絡攝像頭被成功鏈接,將顯示一個新的設備。ui

  步驟1.3:另外一件你能夠作的事是使用任一網絡攝像頭軟件來檢查攝像頭可否正常工做,Ubuntu中你能夠用「Cheese」來檢查。spa

步驟二:軟件安裝

步驟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()

 

這段代碼將輸出以下相似的結果

相關文章
相關標籤/搜索