python dlib学习(七):人脸特征点对齐
生活随笔
收集整理的這篇文章主要介紹了
python dlib学习(七):人脸特征点对齐
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
前面的博客介紹過人臉特征點標定:python dlib學習(二):人臉特征點標定。這次試著使用這些人臉特征點來對人臉進行對齊。
完整工程鏈接附在文章最后。
程序
上代碼,程序中使用了python-opencv,事先要配置好環境。
我們在程序中會導入識別人臉特征點的模型,官方例程給出的模型的鏈接:
http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2(5個特征點)
我使用的跟前面博客(python dlib學習(二):人臉特征點標定)中的一樣,是檢測人臉68個特征點的模型。
下載鏈接:http://pan.baidu.com/s/1i46vPu1。
程序中已有注釋,不做贅述。
# coding: utf-8 import cv2 import dlib import sys import numpy as np import os# 獲取當前路徑 current_path = os.getcwd() # 指定你存放的模型的路徑,我使用的是檢測68個特征點的那個模型, # predicter_path = current_path + '/model/shape_predictor_5_face_landmarks.dat'# 檢測人臉特征點的模型放在當前文件夾中 predicter_path = current_path + '/model/shape_predictor_68_face_landmarks.dat' face_file_path = current_path + '/faces/inesta.jpg'# 要使用的圖片,圖片放在當前文件夾中 print predicter_path print face_file_path# 導入人臉檢測模型 detector = dlib.get_frontal_face_detector() # 導入檢測人臉特征點的模型 sp = dlib.shape_predictor(predicter_path)# 讀入圖片 bgr_img = cv2.imread(face_file_path) if bgr_img is None:print("Sorry, we could not load '{}' as an image".format(face_file_path))exit()# opencv的顏色空間是BGR,需要轉為RGB才能用在dlib中 rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) # 檢測圖片中的人臉 dets = detector(rgb_img, 1) # 檢測到的人臉數量 num_faces = len(dets) if num_faces == 0:print("Sorry, there were no faces found in '{}'".format(face_file_path))exit()# 識別人臉特征點,并保存下來 faces = dlib.full_object_detections() for det in dets:faces.append(sp(rgb_img, det))# 人臉對齊 images = dlib.get_face_chips(rgb_img, faces, size=320) # 顯示計數,按照這個計數創建窗口 image_cnt = 0 # 顯示對齊結果 for image in images:image_cnt += 1cv_rgb_image = np.array(image).astype(np.uint8)# 先轉換為numpy數組cv_bgr_image = cv2.cvtColor(cv_rgb_image, cv2.COLOR_RGB2BGR)# opencv下顏色空間為bgr,所以從rgb轉換為bgrcv2.imshow('%s'%(image_cnt), cv_bgr_image)cv2.waitKey(0) cv2.destroyAllWindows()運行結果
原圖:
完整工程下載鏈接:
http://download.csdn.net/download/hongbin_xu/10115082
總結
以上是生活随笔為你收集整理的python dlib学习(七):人脸特征点对齐的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 机器学习入门学习笔记:(4.1)SVM算
- 下一篇: python dlib学习(八):训练人