使用Python+OpenCV+Dlib實現(xiàn)人臉檢測與人臉特征關(guān)鍵點識別
到目前為止,我們在檢測人臉方面做得很好,但是我們?nèi)匀恍枰恍┕ぷ鱽硖崛∷刑卣鳎ǖ貥?biāo))。接下來讓我們開始吧。步驟3:識別人臉特征你喜歡魔術(shù)嗎?到目前為止,DLib的工作方式相當(dāng)神奇,只需幾行代碼我們就可以實現(xiàn)很多,而現(xiàn)在我們遇到了一個全新的問題,它還會繼續(xù)這么簡單嗎?回答是肯定的!原來DLib提供了一個名為shape_predictor()的函數(shù),它將為我們提供所有的魔法,但是需要一個預(yù)先訓(xùn)練的模型才能工作。有幾種模型可以與shape_predictor一起工作,我正在使用的模型可以在這里下載,也可以嘗試其他模型。讓我們看看新代碼現(xiàn)在是什么樣子import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector()# Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = 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 imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()像以前一樣,我們總是在同一個代碼上構(gòu)建代碼,現(xiàn)在使用我們的預(yù)測函數(shù)為每個人臉找到特征。但現(xiàn)在我還在做一些奇怪的事情,比如如下代碼的數(shù)值27是用來干嘛的?landmarks = predictor(image=gray, box=face)x = landmarks.part(27).xy = landmarks.part(27).y我們的預(yù)測函數(shù)會返回一個包含68個點的對象,根據(jù)我們之前看到的圖片,如果你注意到的話,會發(fā)現(xiàn)點27正好在眼睛之間,所以如果所有的計算正確,你應(yīng)該看到一個綠點在眼睛之間,如下圖所示:
我們已經(jīng)很接近了,現(xiàn)在讓我們渲染所有的點,而不是只渲染一個:import cv2import numpy as npimport dlib# Load the detectordetector = dlib.get_frontal_face_detector()# Load the predictorpredictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# read the imageimg = cv2.imread("face.jpg")# Convert image into grayscalegray = cv2.cvtColor(src=img, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = 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 imagecv2.imshow(winname="Face", mat=img)# Delay between every framcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()
但是如果你對所有的點都不感興趣呢?實際上,你可以調(diào)整你的范圍間隔來獲得上面術(shù)語表中指定的任何特征,就像我在這里做的那樣:

請輸入評論內(nèi)容...
請輸入評論/評論長度6~500個字
最新活動更多
推薦專題
-
10 大模型的盡頭是開源
- 1 特斯拉工人被故障機器人打成重傷,索賠3.6億
- 2 【行業(yè)深度研究】退居幕后四年后,張一鳴終于把算法公司變成AI公司?
- 3 AI 時代,阿里云想當(dāng)“安卓” ,那誰是“蘋果”?
- 4 華為公布昇騰芯片三年計劃,自研HBM曝光
- 5 硬剛英偉達(dá)!華為發(fā)布全球最強算力超節(jié)點和集群
- 6 機器人9月大事件|3家國產(chǎn)機器人沖刺IPO,行業(yè)交付與融資再創(chuàng)新高!
- 7 谷歌“香蕉”爆火啟示:國產(chǎn)垂類AI的危機還是轉(zhuǎn)機?
- 8 00后華裔女生靠兩部AI電影狂賺7.8億人民幣,AI正式進(jìn)軍好萊塢
- 9 美光:AI Capex瘋投不止,終于要拉起存儲超級周期了?
- 10 華為已殺入!AI領(lǐng)域最熱黃金賽道,大廠的數(shù)字人美女讓我一夜沒睡著覺