人臉檢測實戰(zhàn)終極之OpenCV+Python實現(xiàn)人臉對齊
前言
這篇博文的目的是演示如何使用 OpenCV、Python 和面部標志對齊人臉。
給定一組面部標志(輸入坐標),我們的目標是將圖像扭曲并轉(zhuǎn)換為輸出坐標空間。
在這個輸出坐標空間中,整個數(shù)據(jù)集中的所有面都應該:
- 在圖像中居中。
- 旋轉(zhuǎn),使眼睛位于水平線上(即,旋轉(zhuǎn)面部,使眼睛位于相同的 y 坐標上)。
- 進行縮放,使面的大小大致相同。
為此,我們將首先調(diào)用一個實現(xiàn)的 Python 類FaceAligner ,以使用仿射變換對齊人臉。
注意:仿射變換用于旋轉(zhuǎn)、縮放、平移等。我們可以將上述所有三個要求打包到一個 cv2.warpAffine 調(diào)用中; 訣竅是創(chuàng)建旋轉(zhuǎn)矩陣 M 。
然后,我們將創(chuàng)建一個示例驅(qū)動程序 Python 腳本來接受輸入圖像、檢測人臉并對齊它們。
最后,我們將回顧我們使用 OpenCV 過程進行人臉對齊的結(jié)果。
實現(xiàn)面部矯正器
面部對齊算法本身基于 Mastering OpenCV with Practical Computer Vision Projects (Baggio, 2012) 的第 8 章,如果您有 C++ 背景或興趣,我強烈推薦它。 本書在 GitHub 上提供了開放訪問的代碼示例。
新建facealigner.py實現(xiàn)FaceAligner類。
# import the necessary packages from imutils.face_utils.helpers import FACIAL_LANDMARKS_68_IDXS from imutils.face_utils.helpers import FACIAL_LANDMARKS_5_IDXS from imutils.face_utils.helpers import shape_to_np import numpy as np import cv2 class FaceAligner: def __init__(self, predictor, desiredLeftEye=(0.35, 0.35), desiredFaceWidth=256, desiredFaceHeight=None): # store the facial landmark predictor, desired output left # eye position, and desired output face width + height self.predictor = predictor self.desiredLeftEye = desiredLeftEye self.desiredFaceWidth = desiredFaceWidth self.desiredFaceHeight = desiredFaceHeight # if the desired face height is None, set it to be the # desired face width (normal behavior) if self.desiredFaceHeight is None: self.desiredFaceHeight = self.desiredFaceWidth
導入必要的包
定義的構(gòu)造函數(shù)開始我們的 FaceAligner 類。
我們的構(gòu)造函數(shù)有 4 個參數(shù):
- predictor :面部標志性預測器模型。
- requiredLeftEye :一個可選的 (x, y) 元組,顯示默認值,指定所需的輸出左眼位置。對于此變量,通常會看到 20-40% 范圍內(nèi)的百分比。這些百分比控制對齊后人臉的可見程度。使用的確切百分比將因應用程序而異。使用 20% 時,您基本上會獲得“放大”的臉部視圖,而使用較大的值時,臉部會顯得更“縮小”。
- requiredFaceWidth :另一個可選參數(shù),以像素為單位定義我們想要的人臉。我們將此值默認為 256 像素。
- requiredFaceHeight :最后一個可選參數(shù),以像素為單位指定我們所需的人臉高度值。
接下來,讓我們決定是想要一張方形的人臉圖像還是矩形的圖像。檢查 requiredFaceHeight 是否為 None ,如果是,我們將其設(shè)置為 desiredFaceWidth ,這意味著面部是方形的。方形圖像是典型情況?;蛘撸覀兛梢詾?desiredFaceWidth 和 desiredFaceHeight 指定不同的值以獲得感興趣的矩形區(qū)域。
現(xiàn)在我們已經(jīng)構(gòu)建了我們的 FaceAligner 對象,接下來我們將定義一個對齊人臉的函數(shù)。
這個函數(shù)有點長,所以我把它分成了 5 個代碼塊,讓它更容易理解:
def align(self, image, gray, rect): # convert the landmark (x, y)-coordinates to a NumPy array shape = self.predictor(gray, rect) shape = shape_to_np(shape) # extract the left and right eye (x, y)-coordinates (lStart, lEnd) = FACIAL_LANDMARKS_IDXS["left_eye"] (rStart, rEnd) = FACIAL_LANDMARKS_IDXS["right_eye"] leftEyePts = shape[lStart:lEnd] rightEyePts = shape[rStart:rEnd]
定義了 align 函數(shù),它接受三個參數(shù):
image : RGB 輸入圖像。
gray :灰度輸入圖像。
rect :由 dlib 的 HOG 人臉檢測器生成的邊界框矩形。
應用 dlib 的面部標志預測器并將標志轉(zhuǎn)換為 NumPy 格式的 (x, y) 坐標。
接下來,從 helpers.py 腳本中找到的 FACIAL_LANDMARK_IDXS 字典中讀取 left_eye 和 right_eye 區(qū)域。 這些 2 元組值存儲在左/右眼開始和結(jié)束索引中。
提取leftEyePts 和 rightEyePts 。
接下來,計算每只眼睛的中心以及眼睛質(zhì)心之間的角度。
這個角度是對齊我們的圖像的關(guān)鍵組成部分。
眼睛之間的綠線夾角,如下圖所示,是我們比較關(guān)心的。
接下來是角度計算:
# compute the center of mass for each eye leftEyeCenter = leftEyePts.mean(axis=0).astype("int") rightEyeCenter = rightEyePts.mean(axis=0).astype("int") # compute the angle between the eye centroids dY = rightEyeCenter[1] - leftEyeCenter[1] dX = rightEyeCenter[0] - leftEyeCenter[0] angle = np.degrees(np.arctan2(dY, dX)) - 180
分別通過平均每只眼睛的所有 (x, y) 點來計算每只眼睛的質(zhì)心,也稱為質(zhì)心。
給定眼睛中心,我們可以計算 (x, y) 坐標的差異并取反正切以獲得眼睛之間的旋轉(zhuǎn)角度。
這個角度將允許我們校正旋轉(zhuǎn)。
為了確定角度,我們首先計算 y 方向的增量 dY 。 這是通過在第 38 行找到 rightEyeCenter 和 leftEyeCenter 之間的差異來完成的。
類似地,我們計算 dX ,即第 39 行 x 方向的增量。
接下來,我們計算面部旋轉(zhuǎn)的角度。 我們使用帶有參數(shù) dY 和 dX 的 NumPy 的 arctan2 函數(shù),然后轉(zhuǎn)換為度數(shù),同時減去 180 以獲得角度。
在以下代碼塊中,我們計算所需的右眼坐標(作為左眼位置的函數(shù))并計算新結(jié)果圖像的比例。
# compute the desired right eye x-coordinate based on the # desired x-coordinate of the left eye desiredRightEyeX = 1.0 - self.desiredLeftEye[0] # determine the scale of the new resulting image by taking # the ratio of the distance between eyes in the *current* # image to the ratio of distance between eyes in the # *desired* image dist = np.sqrt((dX ** 2) + (dY ** 2)) desiredDist = (desiredRightEyeX - self.desiredLeftEye[0]) desiredDist *= self.desiredFaceWidth scale = desiredDist / dist
根據(jù)所需的左眼 x 坐標計算所需的右眼。從 1.0 中減去 self.desiredLeftEye[0] 因為所需的RightEyeX 值應該與圖像的右邊緣等距,因為相應的左眼 x 坐標與其左邊緣的距離相同。
然后可以通過獲取當前圖像中眼睛之間的距離與所需圖像中眼睛之間的距離的比率來確定人臉的比例
首先,計算歐幾里得距離比 dist 。
接下來,使用左右眼 x 值之間的差異,計算所需的距離,desiredDist。
通過在第 52 行乘以所需的面寬來更新所需的距離。這實質(zhì)上是根據(jù)所需的寬度縮放的眼睛距離。
最后,比例是通過將 desiredDist 除以我們之前計算的 dist 來計算的。
現(xiàn)在有了旋轉(zhuǎn)角度和比例,需要在計算仿射變換之前采取一些步驟。這包括找到眼睛之間的中點以及計算旋轉(zhuǎn)矩陣并更新其平移分量:
# compute center (x, y)-coordinates (i.e., the median point) # between the two eyes in the input image eyesCenter = (int((leftEyeCenter[0] + rightEyeCenter[0]) // 2), int((leftEyeCenter[1] + rightEyeCenter[1]) // 2)) # grab the rotation matrix for rotating and scaling the face M = cv2.getRotationMatrix2D(eyesCenter, angle, scale) # update the translation component of the matrix tX = self.desiredFaceWidth * 0.5 tY = self.desiredFaceHeight * self.desiredLeftEye[1] M[0, 2] += (tX - eyesCenter[0]) M[1, 2] += (tY - eyesCenter[1])
計算 eyeCenter ,即左右眼之間的中點。 這將用于我們的旋轉(zhuǎn)矩陣計算。 本質(zhì)上,這個中點位于鼻子的頂部,是我們將面部旋轉(zhuǎn)的點:
為了計算旋轉(zhuǎn)矩陣 M ,我們使用 cv2.getRotationMatrix2D 指定 eyeCenter 、角度和比例。這三個值中的每一個都先前已計算過,因此請根據(jù)需要返回。
cv2.getRotationMatrix2D 的參數(shù)說明如下:
- eyeCenter :眼睛之間的中點是我們將圍繞面部旋轉(zhuǎn)的點。
- angle:我們將面部旋轉(zhuǎn)到的角度,以確保眼睛位于同一水平線上。
- scale :我們將放大或縮小圖像的百分比,確保圖像縮放到所需的大小。
現(xiàn)在必須更新矩陣的平移分量,使人臉在仿射變換后仍然在圖像中。
取所需面寬的一半并將值存儲為 tX,即 x 方向的平移。
為了計算 tY ,y 方向的平移,將所需的面部高度乘以所需的左眼 y 值,desiredLeftEye[1]。
使用 tX 和 tY ,通過從它們對應的眼睛中點值(第 66 行和第 67 行)中減去每個值來更新矩陣的平移分量。
然后應用仿射變換來對齊人臉:
# apply the affine transformation (w, h) = (self.desiredFaceWidth, self.desiredFaceHeight) output = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC) # return the aligned face return output
為方便起見,將 desiredFaceWidth 和 desiredFaceHeight 分別存儲到 w 和 h 中(第 70 行)。
然后調(diào)用 cv2.warpAffine 執(zhí)行最后一步。 此函數(shù)調(diào)用需要 3 個參數(shù)和 1 個可選參數(shù):
- image : 人臉圖像。
- M :平移、旋轉(zhuǎn)和縮放矩陣。
- (w, h) :輸出面所需的寬度和高度。
- flags :用于扭曲的插值算法,在本例中為 INTER_CUBIC 。 要了解其他可能的標志和圖像轉(zhuǎn)換,請參閱 OpenCV 文檔。
最后,對齊的臉面。
對齊人臉
開始編寫人臉對齊腳本,將其命名為Now let's put this alignment class to work with a simple driver script. Open up a new file, name it align_faces.py:
# import the necessary packages from imutils.face_utils import FaceAligner from imutils.face_utils import rect_to_bb import argparse import imutils import dlib import cv2 # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-p", "--shape-predictor", required=True, help="path to facial landmark predictor") ap.add_argument("-i", "--image", required=True, help="path to input image") args = vars(ap.parse_args())
如果您的系統(tǒng)上沒有安裝 imutils 和/或 dlib,請確保通過 pip 安裝/升級它們:
pip install --upgrade imutils pip install --upgrade dlib
win10安裝dlib參考:如何安裝dlib gpu版本
# initialize dlib's face detector (HOG-based) and then create # the facial landmark predictor and the face aligner detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor(args["shape_predictor"]) fa = FaceAligner(predictor, desiredFaceWidth=256)
使用 dlib 的 get_frontal_face_detector 初始化我們的檢測器對象。
使用 --shape-predictor 來實例化我們的面部標志預測器,這是 dlib 的預訓練預測器的路徑。
通過在第 21 行初始化一個對象 fa 來利用剛剛在上一節(jié)中構(gòu)建的 FaceAligner 類。我們指定了 256 像素的人臉寬度。
接下來,加載圖像并為人臉檢測做準備:
# load the input image, resize it, and convert it to grayscale image = cv2.imread(args["image"]) image = imutils.resize(image, width=800) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # show the original input image and detect faces in the grayscale # image cv2.imshow("Input", image) rects = detector(gray, 2)
加載由命令行參數(shù) --image 指定的圖像。 調(diào)整圖像的大小,保持第 25 行的縱橫比,使其寬度為 800 像素。 然后將圖像轉(zhuǎn)換為灰度。
處理輸入圖像中的人臉檢測,我們在其中應用了 dlib 的人臉檢測器。 此函數(shù)返回 rects ,這是我們檢測器發(fā)現(xiàn)的人臉周圍的邊界框列表。
在下一個塊中,我們遍歷 rects ,對齊每個人臉,并顯示原始和對齊的圖像。
# loop over the face detections for rect in rects: # extract the ROI of the *original* face, then align the face # using facial landmarks (x, y, w, h) = rect_to_bb(rect) faceOrig = imutils.resize(image[y:y + h, x:x + w], width=256) faceAligned = fa.align(image, gray, rect) # display the output images cv2.imshow("Original", faceOrig) cv2.imshow("Aligned", faceAligned) cv2.waitKey(0)
開始循環(huán)。
對于 dlib 預測的每個邊界框 rect,我們將其轉(zhuǎn)換為格式 (x, y, w, h)。
隨后,將框的大小調(diào)整為 256 像素的寬度,保持縱橫比。將這個原始但調(diào)整大小的圖像存儲為 faceOrig 。
對齊圖像,指定圖像、灰度圖像和矩形。
最后,將原始和對應的對齊人臉圖像顯示到各自窗口的屏幕上。
在顯示下一個原始/對齊圖像對之前,等待用戶在任一窗口處于焦點時按下一個鍵。
對所有檢測到的人臉重復上面的過程,然后腳本退出。
展示結(jié)果
輸入命令:
python align_faces.py --shape-predictor shape_predictor_68_face_landmarks.dat --image 11.jpg
到此這篇關(guān)于人臉檢測實戰(zhàn)終極之OpenCV+Python實現(xiàn)人臉對齊的文章就介紹到這了,更多相關(guān)Python OpenCV人臉對齊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python基于pygame實現(xiàn)的font游戲字體(附源碼)
這篇文章主要介紹了Python基于pygame實現(xiàn)的font游戲字體,涉及Python響應鍵盤按鍵動態(tài)操作圖片元素的相關(guān)技巧,需要的朋友可以參考下2015-11-11Python Scrapy框架:通用爬蟲之CrawlSpider用法簡單示例
這篇文章主要介紹了Python Scrapy框架:通用爬蟲之CrawlSpider用法,結(jié)合實例形式分析了Scrapy框架中CrawlSpider的基本使用方法,需要的朋友可以參考下2020-04-04詳解一種用django_cache實現(xiàn)分布式鎖的方式
這篇文章主要介紹了詳解一種用django_cache實現(xiàn)分布式鎖的方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09python+requests接口壓力測試500次,查看響應時間的實例
這篇文章主要介紹了python+requests接口壓力測試500次,查看響應時間的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python實現(xiàn)的遠程登錄windows系統(tǒng)功能示例
這篇文章主要介紹了Python實現(xiàn)的遠程登錄windows系統(tǒng)功能,結(jié)合實例形式分析了Python基于wmi模塊的遠程連接與進程操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2018-06-06pycharm中下載的包但是import還是無法使用/報紅的解決方法
用pycharm開發(fā)時,在導入自己寫的python文件時出現(xiàn)模塊名爆紅的情況,下面這篇文章主要給大家介紹了關(guān)于pycharm中下載包但是import還是無法使用/報紅的解決方法,需要的朋友可以參考下2023-02-02python爬蟲 基于requests模塊發(fā)起ajax的get請求實現(xiàn)解析
這篇文章主要介紹了python爬蟲 基于requests模塊發(fā)起ajax的get請求實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08