使用Python+OpenCV+Dlib實(shí)現(xiàn)人臉檢測(cè)與人臉特征關(guān)鍵點(diǎn)識(shí)別
點(diǎn)擊上方“小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂”
重磅干貨,第一時(shí)間送達(dá)

相關(guān)閱讀:https://towardsdatascience.com/essential-opencv-functions-to-get-you-started-into-computer-vision-743df932e60
Dlib:http://dlib.net/
它是如何工作的?

顎點(diǎn)= 0–16 右眉點(diǎn)= 17–21 左眉點(diǎn)= 22–26 鼻點(diǎn)= 27–35 右眼點(diǎn)= 36–41 左眼點(diǎn)= 42–47 口角= 48–60 嘴唇分?jǐn)?shù)= 61–67
安裝要求
opencv python dlib
pipenv,可以使用以下命令安裝所有這些文件:pipenv install opencv-python, dlib
brew install cmake
步驟1:載入并顯示圖片
import cv2# read the imageimg = cv2.imread("face.jpg")# show the imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()

步驟2:人臉識(shí)別
import cv2import dlib# Load the detectordetector = dlib.get_frontal_face_detector()# 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 pointy1 = face.top() # top pointx2 = face.right() # right pointy2 = face.bottom() # bottom point# Draw a rectanglecv2.rectangle(img=img, pt1=(x1, y1), pt2=(x2, y2), color=(0, 255, 0), thickness=4)# show the imagecv2.imshow(winname="Face", mat=img)# Wait for a key press to exitcv2.waitKey(delay=0)# Close all windowscv2.destroyAllWindows()

步驟3:識(shí)別人臉特征
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 pointy1 = face.top() # top pointx2 = face.right() # right pointy2 = face.bottom() # bottom point# Look for the landmarkslandmarks = predictor(image=gray, box=face)x = landmarks.part(27).xy = landmarks.part(27).y# Draw a circlecv2.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()
landmarks = predictor(image=gray, box=face)x = landmarks.part(27).xy = landmarks.part(27).y

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 pointy1 = face.top() # top pointx2 = face.right() # right pointy2 = face.bottom() # bottom point# Create landmark objectlandmarks = predictor(image=gray, box=face)# Loop through all the pointsfor n in range(0, 68):x = landmarks.part(n).xy = landmarks.part(n).y# Draw a circlecv2.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()


步驟4:實(shí)時(shí)檢測(cè)
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 imagecap = cv2.VideoCapture(0)while True:_, frame = cap.read()# Convert image into grayscalegray = cv2.cvtColor(src=frame, code=cv2.COLOR_BGR2GRAY)# Use detector to find landmarksfaces = detector(gray)for face in faces:x1 = face.left() # left pointy1 = face.top() # top pointx2 = face.right() # right pointy2 = face.bottom() # bottom point# Create landmark objectlandmarks = predictor(image=gray, box=face)# Loop through all the pointsfor n in range(0, 68):x = landmarks.part(n).xy = landmarks.part(n).y# Draw a circlecv2.circle(img=frame, center=(x, y), radius=3, color=(0, 255, 0), thickness=-1)# show the imagecv2.imshow(winname="Face", mat=frame)# Exit when escape is pressedif cv2.waitKey(delay=1) == 27:break# When everything done, release the video capture and video write objectscap.release()# Close all windowscv2.destroyAllWindows()

結(jié)論
評(píng)論
圖片
表情
