參考自https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/
在原有基礎上有一部分的修改(image改為可選參數,若不填則為拍照后選取),如果有想深入學習的,可以去關注這位‘吳克’先生的文章。
本文不涉及關於人臉檢測的訓練部分(雖然之后隨着學習深入我會再發相關的隨筆),只是簡單的用輪子。
今天我們來使用dlib和opencv進行人臉的檢測標注
首先安裝opencv和dlib的方法
pip install dlib
pip install opencv-python
本程序中還使用了imutils用於resize圖片,安裝方法如下
pip install imutils
dlib中為我們提供了關於人臉檢測標注訓練好的文件 可在http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2下載(如果
在參考網頁下載到的例程中也包含了這個文件了)
訓練好的文件可識別人臉的68個關鍵點並標注(關鍵點越少肯定越容易導致識別錯誤)
本程序運行方法:若.py和shape_predictor_68_face_landmarks.dat以及需要檢測的圖片在同一目錄下,在當前目錄console中輸入
python my_facial_landmarks.py -p shape_predictor_68_face_landmarks.dat -i guanhai.jpg
或采用拍照識別的方式,輸入
python my_facial_landmarks.py -p shape_predictor_68_face_landmarks.dat
在圖片框中按q完成拍照
之后會顯示標注后的照片
例如輸入如下圖片運行截圖
拍照然后識別就不舉例了吧,大家可以自行嘗試
代碼如下my_facial_landmarks.py
from imutils import face_utils
import argparse
import imutils
import dlib
import cv2
def takephoto():
cap = cv2.VideoCapture(0)
while (1):
# get a frame
ret, frame = cap.read()
# show a frame
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):#按q鍵完成照相
# cv2.imwrite("./test0.jpg", frame) 保存照片,但在這里我們並不需要
return frame#返回圖片
cap.release()
cv2.destroyAllWindows()
def main():
# construct the argument parser and parse the arguments 使用argparse設置輸入所需的實參
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--shape-predictor", required=True, #訓練好的關於檢測的文件
help="path to facial landmark predictor")
ap.add_argument("-i", "--image", required=False,default='0', #圖片
help="path to input image")
args = vars(ap.parse_args())
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
#初始化dlib人臉檢測(基於HOG),然后創建面部標志預測器
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])
# load the input image, resize it, and convert it to grayscale
if args['image'] != '0':
image = cv2.imread(args['image'])#輸入圖片實參則讀入圖片
else:
image = takephoto()#若未輸入則進行照相操作
image = imutils.resize(image, width=500) # 調整圖片寬度為500
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#圖片調整為灰色
# detect faces in the grayscale image 檢測灰度圖像中的面部
rects = detector(gray, 1)
# loop over the face detections 循環進行人臉的檢測
for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the facial landmark (x, y)-coordinates to a NumPy
# array
# 確定面部區域的面部標志,然后將面部標志(x,y)坐標轉換成NumPy陣列
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)
# convert dlib's rectangle to a OpenCV-style bounding box
# [i.e., (x, y, w, h)], then draw the face bounding box
#將dlib矩形轉換為OpenCV樣式的邊界框[即(x,y,w,h)],然后繪制邊界框
(x, y, w, h) = face_utils.rect_to_bb(rect)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the face number 人臉序號的標記(可識別多張)
cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image
#循環找到面部地關鍵點的(x,y)坐標並在圖像上繪制它們
for (x, y) in shape:
cv2.circle(image, (x, y), 1, (0, 0, 255), -1)
# show the output image with the face detections + facial landmarks
#用臉部檢測+面部標志顯示輸出圖像
cv2.imshow("Output", image)
cv2.waitKey(0)
if __name__ == '__main__':
main()