menpo作為一個集成程度比較高的開發庫,里面集合了opencv numpy 還有很多其他的庫,理論上資料之間可以相互轉換,如果大家用的不熟練的話可以將該庫的輸出轉化為自己熟悉的庫比如numpy,用于做人臉識別是比較方便的(我不是要做這個的所以人臉識別具體怎么搞我也不太清楚,我是想用該庫做醫學影像分割),在該領域一個比較核心的點是landmark localization,所以下面將會演示如何使用menpo的庫進行人臉特征點識別,今天只是分享個實體,每個函式的具體意思和引數要求會在下一篇博文中講解,今天只是展示一下如何使用(我不會告訴你我還沒看明白他的底層庫的),演算法具體原理就不細講了(我也不懂,貌似該庫是對幾十篇頂刊的演算法實作),包括AAM SDM APS 等等諸多演算法,從某種角度講這能讓我們使用高級演算法的同時還不用自己去實作,節省很大一部分時間,
首先完成上面對庫的安裝和環境的搭建,為了使效果更直觀我使用了jupyter 來進行程式運行,jupyter如何在linux下安裝可以參考其他博客
資料源來自于http://ibug.doc.ic.ac.uk/resources/facial-point-annotations/ 的LFPW資料源,該資料源中包括png型別圖片 還有pts點集,具體情況如下:
資料資訊如下:

將資料資源下載完成后就可以加載圖片和資料點集了:
%matplotlib inline
from menpowidgets import visualize_images
import menpo.io as mio
from menpo.visualize import print_progress
from menpo.landmark import labeller, face_ibug_68_to_face_ibug_68_trimesh
path_to_images = '/home/ke/testset/'
training_images = []
for img in print_progress(mio.import_images(path_to_images, verbose=True)):
# convert to greyscale
if img.n_channels == 3:
img = img.as_greyscale()
# crop to landmarks bounding box with an extra 20% padding
img = img.crop_to_landmarks_proportion(0.2)
# rescale image if its diagonal is bigger than 400 pixels
d = img.diagonal()
if d > 400:
img = img.rescale(400.0 / d)
# define a TriMesh which will be useful for Piecewise Affine Warp of HolisticAAM
labeller(img, 'PTS', face_ibug_68_to_face_ibug_68_trimesh)
# append to list
training_images.append(img)
visualize_images(training_images)
需要修改path_to_image 為你圖片存的路徑,其他的無需修改,下面是效果展示:
下面是訓練模型(包括了特征點對齊,生成shape_model 和appearance_model),訓練完成后會告訴你在不同scale下的模型已經訓練完畢
from menpofit.aam import PatchAAM
from menpo.feature import fast_dsift
patch_aam = PatchAAM(training_images, group='PTS', patch_shape=[(15, 15), (23, 23)],
diagonal=150, scales=(0.5, 1.0), holistic_features=fast_dsift,
max_shape_components=20, max_appearance_components=150,
verbose=True)
隨后就是基于光流法生成一個fillter用于最終的fitting,
from menpofit.aam import LucasKanadeAAMFitter, WibergInverseCompositional
fitter = LucasKanadeAAMFitter(patch_aam, lk_algorithm_cls=WibergInverseCompositional,
n_shape=[5, 20], n_appearance=[30, 150])
print(fitter)
生成完filter后就可以進行測驗了,首先匯入路徑和圖片,該圖片即為測驗圖片
from pathlib import Path
import menpo.io as mio
path_to_lfpw = Path('/home/ke/testset/')
image = mio.import_image(path_to_lfpw / 'image_0021.png')
image = image.as_greyscale()
一般需要定位模型的初始位置,否則大概率出現收斂不成功的問題,一個好的初始位置在該問題中很重要,在該庫中可以使用 load_dlib_frontal_face_detector()函式生成人臉的初始位置,并將該位置作為filter的輸入位置
from menpodetect import load_dlib_frontal_face_detector
# Load detector
detect = load_dlib_frontal_face_detector()
# Detect
bboxes = detect(image)
print("{} detected faces.".format(len(bboxes)))
# View
if len(bboxes) > 0:
image.view_landmarks(group='dlib_0', line_colour='red',
render_markers=False, line_width=4);

如上圖所示可以較為準確的識別人臉的位置,大家如果想用在其他領域也可以使用這個menpodetect庫,里面集成了opencv dlib 還有其他很多的人臉檢測的資源,有興趣的可以玩一下,
隨后就可以使用filter進行最后的擬合了:initial_bbox是初始的位置
# initial bbox
initial_bbox = bboxes[0]
# fit image
result = fitter.fit_from_bb(image, initial_bbox, max_iters=[15, 5],
gt_shape=image.landmarks['PTS'].lms)
# print result
print(result)
下面是最終的人臉識別效果:

他可以顯示迭代程序,效果很不錯,今天就分享到這,我要繼續去看庫檔案了,
該庫可用于醫療影像分割、人臉識別等等很多領域,鑒于大家看英文感覺麻煩,我就看一點給大家分享一點,學會了該庫感徑訓是很有用的,如果后面時間的多的話會寫一下原理和具體實作
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/298749.html
標籤:其他
