使用Python+OpenCV+Dlib實(shí)現(xiàn)人臉檢測(cè)與人臉特征關(guān)鍵點(diǎn)識(shí)別
到目前為止,我們?cè)跈z測(cè)人臉?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 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è)代碼上構(gòu)建代碼,現(xiàn)在使用我們的預(yù)測(cè)函數(shù)為每個(gè)人臉找到特征。但現(xiàn)在我還在做一些奇怪的事情,比如如下代碼的數(shù)值27是用來(lái)干嘛的?landmarks = predictor(image=gray, box=face)x = landmarks.part(27).xy = landmarks.part(27).y我們的預(yù)測(cè)函數(shù)會(huì)返回一個(gè)包含68個(gè)點(diǎn)的對(duì)象,根據(jù)我們之前看到的圖片,如果你注意到的話,會(huì)發(fā)現(xiàn)點(diǎn)27正好在眼睛之間,所以如果所有的計(jì)算正確,你應(yīng)該看到一個(gè)綠點(diǎn)在眼睛之間,如下圖所示:
我們已經(jīng)很接近了,現(xiàn)在讓我們渲染所有的點(diǎn),而不是只渲染一個(gè):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()
但是如果你對(duì)所有的點(diǎn)都不感興趣呢?實(shí)際上,你可以調(diào)整你的范圍間隔來(lái)獲得上面術(shù)語(yǔ)表中指定的任何特征,就像我在這里做的那樣:

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
7月22-29日立即報(bào)名>> 【線下論壇】第三屆安富利汽車生態(tài)圈峰會(huì)
-
7.30-8.1火熱報(bào)名中>> 全數(shù)會(huì)2025(第六屆)機(jī)器人及智能工廠展
-
7月31日免費(fèi)預(yù)約>> OFweek 2025具身智能機(jī)器人產(chǎn)業(yè)技術(shù)創(chuàng)新應(yīng)用論壇
-
免費(fèi)參會(huì)立即報(bào)名>> 7月30日- 8月1日 2025全數(shù)會(huì)工業(yè)芯片與傳感儀表展
-
即日-2025.8.1立即下載>> 《2024智能制造產(chǎn)業(yè)高端化、智能化、綠色化發(fā)展藍(lán)皮書(shū)》
-
8月5日立即報(bào)名>> 【在線會(huì)議】CAE優(yōu)化設(shè)計(jì):醫(yī)療器械設(shè)計(jì)的應(yīng)用案例與方案解析
推薦專題
- 1 AI產(chǎn)業(yè)的新高度!英偉達(dá)成為全球首家市值破4萬(wàn)億美元的公司
- 2 一文讀懂:到底什么是 “具身智能” ?
- 3 豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
- 4 “支付+”時(shí)代,支付即生態(tài) | 2025中國(guó)跨境支付十大趨勢(shì)
- 5 中國(guó)最具實(shí)力AI公司TOP10
- 6 深圳跑出40億超級(jí)隱形冠軍:賣機(jī)器人年入6.1億,港股上市
- 7 特斯拉Robotaxi上路,馬斯克端上畫(huà)了十年的餅
- 8 “AI六小虎”到了下一個(gè)賽點(diǎn)
- 9 北京,跑出150億超級(jí)獨(dú)角獸:物流機(jī)器人全球第一,揭示3大機(jī)會(huì)
- 10 張勇等人退出阿里合伙人