EduCoder平臺:人臉識別系統——Dlib人臉特征提取
第1關:檢測人臉特征點
編程要求:
請在右側編輯器中的BEGIN-END之間撰寫代碼,使用Dlib檢測人臉特征點并列印:
-
- 匯入OpenCV和Dlib庫;
-
- 讀取指定image_path影像;
-
- 將圖片轉化為灰度圖;
-
- 使用正向人臉檢測器檢測并獲取人臉;
-
- 使用訓練好的能檢測68個人臉特征點的模型,檢測特征點;
-
- 列印出對應的特征點(列印函式已經默認寫好,無需修改),
代碼如下:
import cv2
import dlib
# 讀取圖片
img_path = "step1/image/face.jpg"
img=cv2.imread(img_path)
#轉換為灰階圖片
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# 正向人臉檢測器
detector = dlib.get_frontal_face_detector()
# 使用訓練完成的68個特征點模型
predictor_path = "data/shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)
# 使用檢測器來檢測影像中的人臉
faces =detector(gray,1)
print("人臉數: ", len(faces))
for i, face in enumerate(faces):
print("第", i+1, "個人臉的矩形框坐標:\n","left:", face.left(), "right:", face.right(), "top:", face.top(), "bottom:", face.bottom())
# 獲取人臉特征點
shape = predictor(img,face)
print("第", i+1, '個人臉的特征點:')
print(shape.parts())
第2關:繪制人臉特征點
編程要求:
請在右側編輯器中的BEGIN-END之間撰寫代碼,使用OpenCV繪制人臉特征點,并保存圖片到指定路徑:
-
- 圓心半徑為 1;
-
- 顏色設定為綠色(0,255,0);
-
- 粗細為 2,
代碼如下:
import cv2
import dlib
# 讀取圖片
img_path = "step2/image/face.jpg"
img = cv2.imread(img_path)
# 轉換為灰階圖片
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 正向人臉檢測器
detector = dlib.get_frontal_face_detector()
# 使用訓練完成的68個特征點模型
predictor_path = "data/shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(predictor_path)
# 使用檢測器來檢測影像中的人臉
faces = detector(gray, 1)
for i, face in enumerate(faces):
# 獲取人臉特征點
shape = predictor(img, face)
'''****************BEGIN****************'''
for pt in shape.parts():
pt_pos=(pt.x,pt.y)
cv2.circle(img,pt_pos,1,(0,255,0),2)
'''**************** END ****************'''
cv2.imwrite('step2/out/face.jpg', img)
第3關:訓練人臉特征點模型
編程要求:
恭喜你學習完如何訓練自己的人臉特征點檢測器,請在右側編輯器中的BEGIN-END之間撰寫代碼,完成一個人臉檢測器,并應用到新的圖片上:
-
定義模型訓練需要的引數, 其中:oversampling_amount 引數設定為100,正則化nu設定為0.05,數的深度 tree_depth 設定為1,be_verbose屬性設定為假(False);
-
呼叫訓練模型函式;
-
呼叫測驗模型函式;
-
使用剛才訓練完成的人臉特征點檢測器進行檢查;
-
獲取每一個人臉區域內的特征點,使用OpenCV繪制特征點, 點的顏色設定為藍色(0, 0, 255),粗細為3;
-
保存圖片到指定路徑(代碼中已經寫好路徑),
代碼如下:
import os
import sys
import cv2
import dlib
import glob
# 資料集路徑
faces_folder = 'step3/dataSet'
'''****************BEGIN****************'''
# 1. 定義模型訓練需要的引數
options=dlib.shape_predictor_training_options()
'''**************** END ****************'''
'''****************BEGIN****************'''
# 2.引數設定
# 通過對訓練樣本進行隨機變形擴大樣本數目
options.oversampling_amount=100
# 通過增加正則化(將nu調小)和使用更小深度的樹來降低模型的容量
options.nu=0.05
options.tree_depth=1
options.be_verbose = False
'''**************** END ****************'''
# 3. 呼叫測驗模型函式
# 訓練集路徑
training_xml_path = os.path.join(
faces_folder, "training_with_face_landmarks.xml")
'''****************BEGIN****************'''
dlib.train_shape_predictor(training_xml_path,"predictor.dat",options)
'''**************** END ****************'''
print('模型訓練完成')
# 4. 使用人臉特征點檢測器模型
# 人臉區域檢測器
detector = dlib.get_frontal_face_detector()
# 從本地匯入人臉特征點檢測器
'''****************BEGIN****************'''
predictor=dlib.shape_predictor("predictor.dat")
'''**************** END ****************'''
# 檢測人臉以及人臉特征點
faces_folder = 'step3/image/'
for f in glob.glob(os.path.join(faces_folder, "*.jpg")):
print("處理檔案: {}".format(f))
# 加載圖片
img = dlib.load_rgb_image(f)
# 檢測圖片
dets = detector(img, 1)
print("檢測到的人臉個數為: {}".format(len(dets)))
# 遍歷圖片中識別出的每一個人臉
for k, d in enumerate(dets):
# 列印人臉區域位置
print("人臉區域 {}: Left: {} Top: {} Right: {} Bottom: {}".format(k, d.left(), d.top(), d.right(), d.bottom()))
# 獲取每一個人臉區域內的特征點
shape = predictor(img, d)
# 遍歷所有點,繪制特征點
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
'''****************BEGIN****************'''
cv2.circle(img,pt_pos,2,(0,0,255),3)
'''**************** END ****************'''
# 圖片保存路徑
save_path = 'step3/out/face-landmark.jpg'
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
'''****************BEGIN****************'''
# 保存圖片
cv2.imwrite(save_path,img)
'''**************** END ****************'''
if os.path.exists(save_path):
print('保存檢測后圖片成功')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/392284.html
標籤:其他
