溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python中怎么檢測(cè)人臉特征

發(fā)布時(shí)間:2021-07-10 11:29:33 來(lái)源:億速云 閱讀:142 作者:Leah 欄目:大數(shù)據(jù)

Python中怎么檢測(cè)人臉特征,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

安裝要求

與往常一樣,本文將用代碼演示示例,并將逐步指導(dǎo)你實(shí)現(xiàn)一個(gè)完整的人臉特征識(shí)別示例。但是在開(kāi)始之前,你需要啟動(dòng)一個(gè)新的Python項(xiàng)目并安裝3個(gè)不同的庫(kù):

  • opencv python

  • dlib

如果像我一樣使用pipenv,可以使用以下命令安裝所有這些文件:

pipenv install opencv-python, dlib

如果你使用的是Mac和某些版本的Linux,則在安裝dlib時(shí)可能會(huì)遇到一些問(wèn)題,如果在安裝過(guò)程中遇到編譯錯(cuò)誤,請(qǐng)確保檢查使用的CMake庫(kù)版本。在Mac中,確保你有可用的CMake,并且可以使用正確的版本運(yùn)行:

brew install cmake

對(duì)于其他操作系統(tǒng),請(qǐng)?jiān)诰€檢查以獲得特定支持。

步驟1:載入并顯示圖片

我們將從小處著手并以代碼為基礎(chǔ),直到有一個(gè)可以正常工作的示例為止。

通常,我喜歡使用繪圖來(lái)渲染圖像,但是由于我們?cè)谏院蟮奈恼轮袦?zhǔn)備了一些很酷的東西,因此我們將做一些不同的事情,并且將創(chuàng)建一個(gè)窗口來(lái)展示我們的工作結(jié)果。

讓我們一起看看代碼吧!

import cv2
# read the image
img = cv2.imread("face.jpg")
# show the image
cv2.imshow(winname="Face", mat=img)
# Wait for a key press to exit
cv2.waitKey(delay=0)
# Close all windows
cv2.destroyAllWindows()

很簡(jiǎn)單,對(duì)吧?我們只是用imread加載圖像,然后告訴OpenCV在winname中顯示圖像,這將打開(kāi)窗口并給它一個(gè)標(biāo)題。

之后,我們需要暫停執(zhí)行,因?yàn)楫?dāng)腳本停止時(shí),窗口會(huì)被破壞,所以我們使用cv2.waitKey來(lái)保持窗口,直到按下某個(gè)鍵,然后銷毀窗口并退出腳本。

如果使用代碼并在代碼目錄中添加了一個(gè)名為face.jpg的圖像,你應(yīng)該得到如下內(nèi)容:

原始圖像:

Python中怎么檢測(cè)人臉特征

步驟2:人臉識(shí)別

到目前為止,我們還沒(méi)有對(duì)圖像做任何處理,只是把它呈現(xiàn)在一個(gè)窗口中,非常無(wú)聊,但是現(xiàn)在我們將開(kāi)始編碼好的內(nèi)容,我們將從識(shí)別圖像中哪里有一張臉開(kāi)始。

為此,我們將使用名為get_frontial_face_detector()的Dlib函數(shù),非常直觀。但是有一個(gè)警告,這個(gè)函數(shù)只適用于灰度圖像,所以我們必須首先使用OpenCV。

get_frontial_face_detector()將返回一個(gè)檢測(cè)器,該檢測(cè)器是一個(gè)我們可以用來(lái)檢索人臉信息的函數(shù)。每個(gè)面都是一個(gè)對(duì)象,其中包含可以找到圖像的點(diǎn)。

但我們最好在代碼上看看:

import cv2
import dlib
# Load the detector
detector = dlib.get_frontal_face_detector()
# read the image
img = cv2.imread("face.jpg")
# Convert image into grayscale
gray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
# Use detector to find landmarks
faces = detector(gray)
for face in faces:
    x1 = face.left() # left point
    y1 = face.top() # top point
    x2 = face.right() # right point
    y2 = face.bottom() # bottom point
    # Draw a rectangle
    cv2.rectangle(img=img, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0), thickness=4)
# show the image
cv2.imshow(winname="Face", mat=img)
# Wait for a key press to exit
cv2.waitKey(delay=0)
# Close all windows
cv2.destroyAllWindows()

上面的代碼將從圖像中檢索所有面部,并在每個(gè)面部上渲染一個(gè)矩形,從而產(chǎn)生如下圖像:

Python中怎么檢測(cè)人臉特征

到目前為止,我們?cè)诎l(fā)現(xiàn)人臉?lè)矫孀龅煤芎茫俏覀內(nèi)匀恍枰恍┕ぷ鱽?lái)提取所有特征(地標(biāo))。接下來(lái)讓我們開(kāi)始吧。

步驟3:識(shí)別人臉特征

你喜歡魔術(shù)嗎?到目前為止,DLib的工作方式相當(dāng)神奇,只需幾行代碼我們就可以實(shí)現(xiàn)很多,而現(xiàn)在我們遇到了一個(gè)全新的問(wèn)題,它還會(huì)繼續(xù)這么簡(jiǎn)單嗎?

回答是肯定的!原來(lái)DLib提供了一個(gè)名為shape_predictor()的函數(shù),它將為我們提供所有的魔法,但是需要一個(gè)預(yù)先訓(xùn)練的模型才能工作。

有幾種模型可以與shape_predictor一起工作,我正在使用的模型可以在這里下載,也可以嘗試其他模型。

讓我們看看新代碼現(xiàn)在是什么樣子

import cv2
import dlib
# Load the detector
detector = dlib.get_frontal_face_detector()
# Load the predictor
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# read the image
img = cv2.imread("face.jpg")
# Convert image into grayscale
gray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
# Use detector to find landmarks
faces = detector(gray)
for face in faces:
    x1 = face.left() # left point
    y1 = face.top() # top point
    x2 = face.right() # right point
    y2 = face.bottom() # bottom point
    # Look for the landmarks
    landmarks = predictor(image=gray, box=face)
    x = landmarks.part(27).x
    y = landmarks.part(27).y
    # Draw a circle
    cv2.circle(img=img, center=(x, y), radius=5, color=(0, 255, 0), thickness=-1)
# show the image
cv2.imshow(winname="Face", mat=img)
# Wait for a key press to exit
cv2.waitKey(delay=0)
# Close all windows
cv2.destroyAllWindows()

像以前一樣,我們總是在同一個(gè)代碼上構(gòu)建代碼,現(xiàn)在使用我們的預(yù)測(cè)函數(shù)為每個(gè)人臉找到地標(biāo)。現(xiàn)在我還在做一些奇怪的事情,比如27號(hào)在那里做什么?

landmarks = predictor(image=gray, box=face)
x = landmarks.part(27).x
y = landmarks.part(27).y

我們的預(yù)測(cè)函數(shù)將返回一個(gè)包含所有68個(gè)點(diǎn)的對(duì)象,根據(jù)我們之前看到的圖片,如果你注意到的話,會(huì)發(fā)現(xiàn)點(diǎn)27正好在眼睛之間,所以如果所有的計(jì)算正確,你應(yīng)該看到一個(gè)綠點(diǎn)在眼睛之間,如下圖所示:

Python中怎么檢測(cè)人臉特征

我們已經(jīng)很接近了,現(xiàn)在讓我們渲染所有的點(diǎn),而不是只渲染一個(gè):

import cv2
import numpy as np
import dlib
# Load the detector
detector = dlib.get_frontal_face_detector()
# Load the predictor
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# read the image
img = cv2.imread("face.jpg")
# Convert image into grayscale
gray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)
# Use detector to find landmarks
faces = detector(gray)
for face in faces:
    x1 = face.left() # left point
    y1 = face.top() # top point
    x2 = face.right() # right point
    y2 = face.bottom() # bottom point
    # Create landmark object
    landmarks = predictor(image=gray, box=face)
    # Loop through all the points
    for n in range(0, 68):
        x = landmarks.part(n).x
        y = landmarks.part(n).y
        # Draw a circle
        cv2.circle(img=img, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)
# show the image
cv2.imshow(winname="Face", mat=img)
# Delay between every fram
cv2.waitKey(delay=0)
# Close all windows
cv2.destroyAllWindows()

Python中怎么檢測(cè)人臉特征

但是如果你對(duì)所有的點(diǎn)都不感興趣呢?實(shí)際上,你可以調(diào)整你的范圍間隔來(lái)獲得上面術(shù)語(yǔ)表中指定的任何特征,就像我在這里做的那樣:

Python中怎么檢測(cè)人臉特征

太棒了,但我們能做點(diǎn)更酷的事嗎?

步驟4:實(shí)時(shí)檢測(cè)

是的,你沒(méi)看錯(cuò)!這可能就是你想要的效果!下一步是連接我們的網(wǎng)絡(luò)攝像頭,從你的視頻流中進(jìn)行實(shí)時(shí)地標(biāo)識(shí)別。

你可以通過(guò)使用相機(jī)遍歷視頻幀或使用視頻文件來(lái)對(duì)面部進(jìn)行實(shí)時(shí)面部地標(biāo)檢測(cè)。

如果要使用自己的攝像機(jī),請(qǐng)參考以下代碼,如果使用的是視頻文件,請(qǐng)確保將數(shù)字0更改為視頻路徑。

如果要結(jié)束窗口,請(qǐng)按鍵盤上的ESC鍵:

import cv2
import dlib

# Load the detector
detector = dlib.get_frontal_face_detector()

# Load the predictor
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# read the image
cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    # Convert image into grayscale
    gray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)

    # Use detector to find landmarks
    faces = detector(gray)

    for face in faces:
        x1 = face.left()  # left point
        y1 = face.top()  # top point
        x2 = face.right()  # right point
        y2 = face.bottom()  # bottom point

        # Create landmark object
        landmarks = predictor(image=gray, box=face)

        # Loop through all the points
        for n in range(0, 68):
            x = landmarks.part(n).x
            y = landmarks.part(n).y

            # Draw a circle
            cv2.circle(img=frame, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)

    # show the image
    cv2.imshow(winname="Face", mat=frame)

    # Exit when escape is pressed
    if cv2.waitKey(delay=1) == 27:
        break

# When everything done, release the video capture and video write objects
cap.release()

# Close all windows
cv2.destroyAllWindows()

看完上述內(nèi)容,你們掌握Python中怎么檢測(cè)人臉特征的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI