Python 人臉識(shí)別有多簡(jiǎn)單,看這個(gè)就夠了!

今天給大家介紹一個(gè)世界上最簡(jiǎn)潔的人臉識(shí)別庫(kù) face_recognition,你可以使用 Python 和命令行工具進(jìn)行提取、識(shí)別、操作人臉。

基于業(yè)內(nèi)領(lǐng)先的 C++ 開(kāi)源庫(kù) dlib 中的深度學(xué)習(xí)模型,用 Labeled Faces in the Wild 人臉數(shù)據(jù)集進(jìn)行測(cè)試,有高達(dá)99.38%的準(zhǔn)確率。

1.特性

  1. 從圖片中找到人臉
  2. 識(shí)別人臉關(guān)鍵位置
  3. 識(shí)別圖片中的人是誰(shuí)
  4. 檢測(cè)視頻中的人臉

2.安裝

最好是使用 Linux 或 Mac 環(huán)境來(lái)安裝,Windows 下安裝會(huì)有很多問(wèn)題。在安裝 face_recognition 之前你需要先安裝以下幾個(gè)庫(kù),注意順序.!

2.1 先安裝 cmake 和 boost

pip  install  cmake
pip install boost

2.3 安裝 dlib

pip install dlib

此處安裝可能要幾分鐘。如安裝出錯(cuò),建議使用 whl 文件來(lái)安裝
下載地址:https://pypi.org/simple/dlib/

2.3 安裝 face_recognition

face_recongnition 一般要配合 opencv 一起使用

pip install face_recognition
pip install opencv-python

3. 人臉識(shí)別

比如這里總共有三張圖片,其中有兩張已知,第三張是需要識(shí)別的圖片

這三張圖片名字分別為: “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 數(shù)組,后三行返回圖像中每個(gè)面部的人臉編碼

然后將未知圖片中的人臉和已知圖片中的人臉進(jìn)行對(duì)比,使用 compare_faces() 函數(shù), 代碼如下:

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

運(yùn)行結(jié)果如下:

不到 二十 行代碼,就能識(shí)別出人臉是誰(shuí),是不是 so easy!

4. 人臉標(biāo)注

僅僅識(shí)別圖片中的人臉總是感覺(jué)差點(diǎn)什么,那么將識(shí)別出來(lái)的人臉進(jìn)行姓名標(biāo)注是不是更加有趣~
已知圖片的識(shí)別和前面代碼是一樣的,未知圖片多了人臉位置的識(shí)別,face_locations() 函數(shù),傳入圖像數(shù)組,返回以上,右,下,左固定順序的臉部位置列表
代碼如下:

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

使用 face_distance() 函數(shù)。將已知臉部位置和未知面部編碼進(jìn)行比較,得到歐式距離~·具體是什么我也不知道,距離就相當(dāng)于相識(shí)度。
face_distance(face_encodings, face_to_compare)
face_encodings:已知的面部編碼
face_to_compare:要比較的面部編碼

本次圖片前面兩張沒(méi)有變化,第三張換成了科比和喬丹的合影,最終運(yùn)行之后結(jié)果如下:

識(shí)別結(jié)果

左邊是原圖,右邊是識(shí)別后自動(dòng)標(biāo)注出來(lái)的圖片。

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. 給人臉美妝

這個(gè)功能需要結(jié)合 PIL 一起使用。用法都差不多,首先就是將圖片文件加載到 numpy 數(shù)組中,然后將人臉中的面部所有特征識(shí)別到一個(gè)列表中

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)

根據(jù)以上代碼做了,我用實(shí)力不行,打球又臟的 "大嘴" 博格特來(lái)做演示!
左邊是原圖,右邊是加了美妝后的效果

你打球的樣子真像 cxk!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容