本文將側重於人臉識別的實踐應用,對算法如何運做只做註釋。如果你想了解更多,能夠瀏覽下面這篇文章: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
針對本文,如下列出了我使用的和推薦使用的系統配置以下:算法
爲了確保搭建模型前的每件事都安排穩當,接下來咱們將詳細探討如何配置。網絡
步驟一:硬件安裝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()
這段代碼將輸出以下相似的結果