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


  • 人臉識別中有前景的應用
  • 系統准備—— 硬件/軟件要求
  • 硬件安裝
  • 軟件安裝
  • 探究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()

 

這段代碼將輸出如下類似的結果

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM