face_recognition的5個應用例項

樂亦亦樂發表於2018-12-31

Face Recognition 是一個基於 Python 的人臉識別庫,它還提供了一個命令列工具,讓你通過命令列對任意資料夾中的影象進行人臉識別操作。

該庫使用 dlib 頂尖的深度學習人臉識別技術構建,在戶外臉部檢測資料庫基準(Labeled Faces in the Wild benchmark)上的準確率高達 99.38%。

在網上找到了很多關於face_recognition的有趣程式,這裡進行一下彙總。

安裝:

  • 人臉檢測基於dlib,dlib依賴Boost和cmake
  • 在windows中如果要使用dlib還是比較麻煩的,最好使用anaconda中安裝,這樣可以減少很多麻煩 

執行:pip install face_recognition

這是安裝好的face_recognition,可以看見所依賴的庫!

如果安裝的過程遇到缺少庫的話,缺少哪個就安裝哪個!!!

 

應用1:

檢測給定影象中的所有人臉

# -*- coding: utf-8 -*-


# 檢測人臉
import face_recognition
import cv2

# 讀取圖片並識別人臉
img = face_recognition.load_image_file("1.png")
face_locations = face_recognition.face_locations(img)
print (face_locations)

# 呼叫opencv函式顯示圖片
img = cv2.imread("1.png")
cv2.namedWindow("原圖")
cv2.imshow("原圖", img)

# 遍歷每個人臉,並標註
faceNum = len(face_locations)
for i in range(0, faceNum):
    top =  face_locations[i][0]
    right =  face_locations[i][1]
    bottom = face_locations[i][2]
    left = face_locations[i][3]

    start = (left, top)
    end = (right, bottom)

    color = (55,255,155)
    thickness = 3
    cv2.rectangle(img, start, end, color, thickness)

# 顯示識別結果
cv2.namedWindow("識別")
cv2.imshow("識別", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

用到的圖片1.png

執行結果:

 

應用2:

識別影象中的人臉

資料夾結構:

images資料夾中的檔案 

my_image.jpg

 

程式碼 :faceRecognition.py

# 匯入庫
import os
import face_recognition
# 製作所有可用影象的列表
images = os.listdir('images')
# 載入影象
image_to_be_matched = face_recognition.load_image_file('my_image.jpg')

# 將載入影象編碼為特徵向量

image_to_be_matched_encoded = face_recognition.face_encodings(

   image_to_be_matched)[0]

# 遍歷每張影象
for image in images:
   # 載入影象
   current_image = face_recognition.load_image_file("images/" + image)
   # 將載入影象編碼為特徵向量
   current_image_encoded = face_recognition.face_encodings(current_image)[0]

   # 將你的影象和影象對比,看是否為同一人

   result = face_recognition.compare_faces(

       [image_to_be_matched_encoded], current_image_encoded)

   # 檢查是否一致

   if result[0] == True:

       print ("Matched: " + image)

   else:

       print ("Not matched: " + image)

執行結果:

程式碼中利用face_recognition將要檢視的圖片載入,並將圖片編碼為特徵向量。然後遍歷images檔案中的每一張圖片都載入為特徵向量,並進行比較,輸出結果。

 

應用3:

實時人臉識別

 

程式碼:

# -*- coding: utf-8 -*-
import face_recognition
import cv2

video_capture = cv2.VideoCapture(0)

obama_img = face_recognition.load_image_file("lq.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_img)[0]

face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    ret, frame = video_capture.read()


    small_frame = cv2.resize(frame,(0,0),fx=0.25, fy=0.25)

    if process_this_frame:
        face_locations = face_recognition.face_locations(small_frame)
        face_encodings = face_recognition.face_encodings(small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            match = face_recognition.compare_faces([obama_face_encoding], face_encoding)

            if match[0]:
                name = "lq"
            else:
                name = "unkonwn"

            face_names.append(name)

    process_this_frame = not process_this_frame

    for (top, right, bottom, left), name in zip(face_locations, face_names):
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255),  2)

        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)

    cv2.imshow('Video', frame)
    #按Q退出,結束程式
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

 

執行結果:

 

應用4:

檢測和標記影象中的人臉特徵:

程式碼:

# -*- coding: utf-8 -*-
# 自動識別人臉特徵
from PIL import Image, ImageDraw
import face_recognition

# 將jpg檔案載入到numpy 陣列中
image = face_recognition.load_image_file("my_image.jpg")

#查詢影象中所有面部的所有面部特徵
face_landmarks_list = face_recognition.face_landmarks(image)
#列印發現的臉張數
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))

for face_landmarks in face_landmarks_list:

   #列印此影象中每個面部特徵的位置
    facial_features = [
        'chin',
        'left_eyebrow',
        'right_eyebrow',
        'nose_bridge',
        'nose_tip',
        'left_eye',
        'right_eye',
        'top_lip',
        'bottom_lip'
    ]

    for facial_feature in facial_features:
        print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

   #讓我們在影象中描繪出每個人臉特徵!
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image)

    for facial_feature in facial_features:
        d.line(face_landmarks[facial_feature], width=5)

    pil_image.show()

 

結果:

如果用上文中的1.png,就會發現5張臉,會標記每一張臉的特徵。

 

 

應用5:

識別人臉並美顏

程式碼 :

# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw
import face_recognition

#將jpg檔案載入到numpy陣列中
image = face_recognition.load_image_file("3.jpg")

#查詢影象中所有面部的所有面部特徵
face_landmarks_list = face_recognition.face_landmarks(image)

for face_landmarks in face_landmarks_list:
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image, 'RGBA')

    #讓眉毛變成了一場噩夢
    d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
    d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
    d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
    d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)

    
    #光澤的嘴脣
    d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
    d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
    d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
    d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)

    #閃耀眼睛
    d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
    d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))

    #塗一些眼線
    d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
    d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)

    pil_image.show()

這個就不放執行的截圖了,哈哈,感興趣可以自己找一張圖片執行!!!

 

相關文章