1. 程式人生 > >人臉檢測之face_recognition演算法除錯

人臉檢測之face_recognition演算法除錯

參考:https://github.com/ageitgey/face_recognition

公司專案需求,要出一個人臉檢測與識別的demo,檢視網上比較成熟的是face_recognition方案,因此在電腦上按照推薦步驟進行除錯。face_recognition使用dlib最先進的面部識別功能構建而成,並且該模型在LFW資料集中有99.38%的準確率。

LFW人臉資料庫是由美國馬薩諸塞州立大學阿默斯特分校計算機視覺實驗室整理完成的資料庫,主要用來研究非受限情況下的人臉識別問題。LFW資料庫主要是從網際網路上搜集影象,一共含有13000多張人臉影象,每張影象都被標識出對應的人的名字,其中有1680人對應不只一張照片。

1 安裝dlib

首先複製程式碼

git clone https://github.com/davisking/dlib.git

然後build

cd dlib
mkdir build
cd build
cmake ..
cmake --build .

編譯安裝

cd ..
python3 setup.py install

安裝完之後執行python3,然後倒入 import dlib,如果沒有報錯則安裝成功

2 下載face_recognition模組

pip3 install face_recognition

安裝完之後,就可以匯入該模組使用

3 視訊中檢測人臉

在本地放一個測試視訊,放入兩張帶名字標籤的圖片,下邊程式碼即可實現檢測

#!/usr/bin/python  
 
import face_recognition
import cv2

# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture("test.mp4")

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "zongxp",
    "Joe Biden"
]

# 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)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # 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(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]

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

執行上述指令碼既可以實現在視訊中實時檢測人臉

4 輸出檢測結果

如果在伺服器上執行相關程式碼,沒有視覺化的介面時,可以將檢測結果輸出,主要用到兩個命令:

  • face_recognition - Recognize faces in a photograph or folder full for photographs.
  • face_detection - Find faces in a photograph or folder full for photographs.

4.1 face_recognition

首先建立一個資料夾,如known_people,用來儲存所有知道名字的人的圖片,並且用名字來命名圖片,如obama.jpg、jobs.jpg等;然後再建立另一個資料夾,如unknown_people,用來儲存待識別的圖片。如圖所示,我在known_people資料夾中放入一張zongxp.jpg,在unknown_people中放入unknown1.jpg和unknown2.jpg

執行以下指令

face_recognition known_people/ unknown_people/

輸出如下

unknown_people/unknown2.jpg,unknown_person
unknown_people/unknown1.jpg,zongxp

4.2 face_detection

執行該指令,傳入圖片名稱或圖片路徑,即可輸出每張圖片中人臉的位置,如

face_detection  known_people

輸出如下

known_people/zongxp.jpg,370,474,680,164

4.3 調整容忍度

如果一張圖片中由於相似性,多個人得到了相同的結果輸出,即將多個人判斷為同一個人,則需要用較低的容忍度來使區分更加嚴格。只需要在指令中新增--tolerance引數即可,預設值為0.6,值越低表示越嚴格,比如

face_recognition --tolerance 0.5 known_people/ unknown_people/

4.4 顯示距離

使用--show-distance true引數可實現計算過程中的距離值輸出,比如

face_recognition --show-distance true known_people/ unknown_people/

輸出如下

unknown_people/unknown2.jpg,unknown_person,None
unknown_people/unknown1.jpg,zongxp,0.3190091574234111

4.5 選擇性輸出

執行指令後,會輸出檔案的詳細資訊,有時候我們只關心一部分,比如只關心輸出的名字,則可以使用以下指令進行輸出

face_recognition known_people unknown_people | cut -d ',' -f2

輸出如下

unknown_person
zongxp

4.6 選擇CPU核數

可以使用--cpus引數來選擇計算過程中使用的核數,如

face_recognition --cpus 4 known_people/ unknown_people/

如果想要使用全部的CPU,可直接使用--cpus -1引數