震驚!只用20行代碼就寫出了一我的臉識別的程序

本文首發於公衆號【Python編程與實戰】

今天給你們介紹一個世界上最簡潔的人臉識別庫 face_recognition,你可使用 Python 和命令行工具進行提取、識別、操做人臉。python

基於業內領先的 C++ 開源庫 dlib 中的深度學習模型,用 Labeled Faces in the Wild 人臉數據集進行測試,有高達99.38%的準確率。編程

1.特性

  1. 從圖片中找到人臉
  2. 識別人臉關鍵位置
  3. 識別圖片中的人是誰
  4. 檢測視頻中的人臉

2.安裝

最好是使用 Linux 或 Mac 環境來安裝,Windows 下安裝會有不少問題。在安裝 face_recognition 以前你須要先安裝如下幾個庫,注意順序.!數組

2.1 先安裝 cmake 和 boost

pip  install  cmake
pip install boost
複製代碼

2.3 安裝 dlib

pip install dlib
複製代碼

此處安裝可能要幾分鐘。如安裝出錯,建議使用 whl 文件來安裝 下載地址:pypi.org/simple/dlib…bash

####2.3 安裝 face_recognition face_recongnition 通常要配合 opencv 一塊兒使用函數

pip install face_recognition
pip install opencv-python
複製代碼

3. 人臉識別

好比這裏總共有三張圖片,其中有兩張已知,第三張是須要識別的圖片工具

這三張圖片名字分別爲: 「kobe,jpg」, "jordan.jpeg", "unkown.jpeg" 首先獲取人臉中的信息學習

kobe_image = face_recognition.load_image_file("kobe.jpg")  # 已知科比照片
jordan_image = face_recognition.load_image_file("jordan.jpeg")  # 已知喬丹照片
unknown_image = face_recognition.load_image_file("unkown.jpeg")  # 未知照片

kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]
jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
複製代碼

代碼中前三行分別是加載三張圖片文件並返回圖像的 numpy 數組,後三行返回圖像中每一個面部的人臉編碼測試

而後將未知圖片中的人臉和已知圖片中的人臉進行對比,使用 compare_faces() 函數, 代碼以下:ui

known_faces = [
    kobe_face_encoding,
    jordan_face_encoding
]
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)  # 識別結果列表
print("這張未知照片是科比嗎? {}".format(results[0]))
print("這張未知照片是喬丹嗎? {}".format(results[1]))
複製代碼

運行結果以下:編碼

不到 二十 行代碼,就能識別出人臉是誰,是否是 so easy!

4. 人臉標註

僅僅識別圖片中的人臉老是感受差點什麼,那麼將識別出來的人臉進行姓名標註是否是更加有趣~ 已知圖片的識別和前面代碼是同樣的,未知圖片多了人臉位置的識別,face_locations() 函數,傳入圖像數組,返回以上,右,下,左固定順序的臉部位置列表 代碼以下:

face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)
複製代碼

使用 face_distance() 函數。將已知臉部位置和未知面部編碼進行比較,獲得歐式距離~·具體是什麼我也不知道,距離就至關於相識度。 face_distance(face_encodings, face_to_compare) face_encodings:已知的面部編碼 face_to_compare:要比較的面部編碼

本次圖片前面兩張沒有變化,第三張換成了科比和喬丹的合影,最終運行以後結果以下:

識別結果

左邊是原圖,右邊是識別後自動標註出來的圖片。

import face_recognition
from PIL import Image, ImageDraw
import numpy as np


def draws():
    kobe_image = face_recognition.load_image_file("kobe.jpg")
    kobe_face_encoding = face_recognition.face_encodings(kobe_image)[0]

    jordan_image = face_recognition.load_image_file("jordan.jpeg")
    jordan_face_encoding = face_recognition.face_encodings(jordan_image)[0]

    known_face_encodings = [
        kobe_face_encoding,
        jordan_face_encoding
    ]
    known_face_names = [
        "Kobe",
        "Jordan"
    ]

    unknown_image = face_recognition.load_image_file("two_people.jpeg")

    face_locations = face_recognition.face_locations(unknown_image)
    face_encodings = face_recognition.face_encodings(unknown_image, face_locations)

    pil_image = Image.fromarray(unknown_image)
    draw = ImageDraw.Draw(pil_image)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

        name = "Unknown"

        face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
        best_match_index = np.argmin(face_distances)
        if matches[best_match_index]:
            name = known_face_names[best_match_index]

        draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))

        text_width, text_height = draw.textsize(name)
        draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))
        draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))

    del draw
    pil_image.show()
    pil_image.save("image_with_boxes.jpg")
複製代碼

5. 給人臉美妝

這個功能須要結合 PIL 一塊兒使用。用法都差很少,首先就是將圖片文件加載到 numpy 數組中,而後將人臉中的面部全部特徵識別到一個列表中

image = face_recognition.load_image_file("bogute.jpeg")
face_landmarks_list = face_recognition.face_landmarks(image)
複製代碼

遍歷列表中的元素,修改眉毛

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), wid=6)
複製代碼

根據以上代碼作了,我用實力不行,打球又髒的 "大嘴" 博格特來作演示! 左邊是原圖,右邊是加了美妝後的效果

你打球的樣子真像 cxk!

相關文章
相關標籤/搜索