這幾日閒來無事,試用了下face_recognition庫,還真是簡單方便,只消十幾分鍾就可以開始體驗,如果你也感興趣,和我一起試試吧。
背景
Adam Geitgey寫了一系列的“Machine Learning is Fun”。其中第四篇介紹他用DL做的人臉識別。知乎上有官方授權翻譯(機器學習原來這麼有趣!第四章:用深度學習識別人臉 - 知乎專欄),大家可以閱讀,我這裡就不廢話了。
本文主要介紹如何在最短時間開始上手玩玩,弄好效果類似這個
我沒有用到任何GPU資源,在windows上和Linux上均可,下面和我一起來做。
Anaconda的安裝
可以參考下面兩篇文章安裝Anaconda而不是自己安裝Python。
Tensorflow Windows下安裝 - 知乎專欄
Tensorflow Linux下安裝 - 知乎專欄
簡單來說就是進入Anaconda官網:
Download Anaconda Now!
並根據你的機器版本選擇合適的版本,32位的選擇X86,64位的選擇X86_64。正常安裝即可。
安裝face_recognition庫
開啟Anaconda Prompt (在安裝的資料夾中),輸入
pip install face_recognition
如果你在proxy後面,可以宣告proxy
pip install face_recognition ——proxy proxy:port
dlib會在安裝face_recognition時自動安裝。如果你沒裝opencv,輸入
conda install -c https://conda。binstar。org/menpo opencv3
程式
face_recognition是open source的,原始碼在:
ageitgey/face_recognition
其中example有不少例子,其中有個例子我小改了下以便演示多於一個人的識別:
import face_recognition
import cv2
# Get a reference to webcam #0 (the default one)
video_capture = cv2。VideoCapture(0)
# Load a sample picture and learn how to recognize it。
m1_image = face_recognition。load_image_file(“M1_B1。jpg”)
m1_face_encoding = face_recognition。face_encodings(m1_image)[0]
m2_image = face_recognition。load_image_file(“M2。jpg”)
m2_face_encoding = face_recognition。face_encodings(m2_image)[0]
k1_image = face_recognition。load_image_file(“k1。jpg”)
k1_face_encoding = face_recognition。face_encodings(k1_image)[0]
B1_image = face_recognition。load_image_file(“B1。jpg”)
B1_face_encoding = face_recognition。face_encodings(B1_image)[0]
B2_image = face_recognition。load_image_file(“B2。jpg”)
B2_face_encoding = face_recognition。face_encodings(B2_image)[0]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Grab a single frame of video
ret, frame = video_capture。read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2。resize(frame, (0, 0), fx=0。25, fy=0。25)
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition。face_locations(small_frame)
face_encodings = face_recognition。face_encodings(small_frame, face_locations)
face_names = []
faces_to_compare = [
m1_face_encoding,
m2_face_encoding,
k1_face_encoding,
B1_face_encoding,
B2_face_encoding]
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
match = face_recognition。compare_faces(faces_to_compare, face_encoding, tolerance=0。5)
name = “Unknown”
print(match)
if match[0]:
name = “M1”
if match[1]:
name = “M2”
if match[2]:
name = “K1”
if match[3]:
name = “B1”
if match[4]:
name = “B2”
face_names。append(name)
process_this_frame = not process_this_frame
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# 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 - 35), (right, bottom), (0, 0, 255), cv2。FILLED)
font = cv2。FONT_HERSHEY_DUPLEX
cv2。putText(frame, name, (left + 6, bottom - 6), font, 1。0, (255, 255, 255), 1)
# 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 webcam
video_capture。release()
cv2。destroyAllWindows()
宣告:程式碼版權屬於原作者。
圖片
好了萬事俱備,只差圖片了,你可以把你旁邊的親人們大頭照弄過來,改個名字替換掉M1。jpg,M2。jpg等等,放在同一個目錄下即可。圖片沒有什麼特殊要求,正面即可,類似這樣:
執行
現在插上攝像頭,執行下試試吧,是不是挺有意思的?注意這裡我的tolerance=0。5,你可以試試不同的值。太高你帶上眼鏡啥的就認不出來了,顯示unknown,太低容易認錯。