http://blog.csdn.net/sunmc1204953974/article/details/49976045
人臉檢測
#coding=utf-8 # -*- coding: utf-8 -*- import sys import dlib from skimage import io #使用dlib自帶的frontal_face_detector作為我們的特征提取器 detector = dlib.get_frontal_face_detector() #使用dlib提供的圖片窗口 win = dlib.image_window() #sys.argv[]是用來獲取命令行參數的,sys.argv[0]表示代碼本身文件路徑,所以參數從1開始向后依次獲取圖片路徑 for f in sys.argv[1:]: #輸出目前處理的圖片地址 print("Processing file: {}".format(f)) #使用skimage的io讀取圖片 img = io.imread(f) #使用detector進行人臉檢測 dets為返回的結果 dets = detector(img, 1) #dets的元素個數即為臉的個數 print("Number of faces detected: {}".format(len(dets))) #使用enumerate 函數遍歷序列中的元素以及它們的下標 #下標i即為人臉序號 #left:人臉左邊距離圖片左邊界的距離 ;right:人臉右邊距離圖片左邊界的距離 #top:人臉上邊距離圖片上邊界的距離 ;bottom:人臉下邊距離圖片上邊界的距離 for i, d in enumerate(dets): print("dets{}".format(d)) print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}" .format( i, d.left(), d.top(), d.right(), d.bottom())) #也可以獲取比較全面的信息,如獲取人臉與detector的匹配程度 dets, scores, idx = detector.run(img, 1) for i, d in enumerate(dets): print("Detection {}, dets{},score: {}, face_type:{}".format( i, d, scores[i], idx[i])) #繪制圖片(dlib的ui庫可以直接繪制dets) win.set_image(img) win.add_overlay(dets) #等待點擊 dlib.hit_enter_to_continue()
python dlibface.py itlay.jpg
人臉關鍵點標記
# -*- coding: utf-8 -*- import dlib import numpy from skimage import io #源程序是用sys.argv從命令行參數去獲取訓練模型,精簡版我直接把路徑寫在程序中了 predictor_path = "./shape_predictor_68_face_landmarks.dat" #源程序是用sys.argv從命令行參數去獲取文件夾路徑,再處理文件夾里的所有圖片 #這里我直接把圖片路徑寫在程序里了,每運行一次就只提取一張圖片的關鍵點 faces_path = "./itlay.jpg" #與人臉檢測相同,使用dlib自帶的frontal_face_detector作為人臉檢測器 detector = dlib.get_frontal_face_detector() #使用官方提供的模型構建特征提取器 predictor = dlib.shape_predictor(predictor_path) #使用dlib提供的圖片窗口 win = dlib.image_window() #使用skimage的io讀取圖片 img = io.imread(faces_path) #繪制圖片 win.clear_overlay() win.set_image(img) #與人臉檢測程序相同,使用detector進行人臉檢測 dets為返回的結果 dets = detector(img, 1) #dets的元素個數即為臉的個數 print("Number of faces detected: {}".format(len(dets))) #使用enumerate 函數遍歷序列中的元素以及它們的下標 #下標k即為人臉序號 #left:人臉左邊距離圖片左邊界的距離 ;right:人臉右邊距離圖片左邊界的距離 #top:人臉上邊距離圖片上邊界的距離 ;bottom:人臉下邊距離圖片上邊界的距離 for k, d in enumerate(dets): print("dets{}".format(d)) print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) #使用predictor進行人臉關鍵點識別 shape為返回的結果 shape = predictor(img, d) #獲取第一個和第二個點的坐標(相對於圖片而不是框出來的人臉) print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1))) #繪制特征點 win.add_overlay(shape) #繪制人臉框 win.add_overlay(dets) #也可以這樣來獲取(以一張臉的情況為例) #get_landmarks()函數會將一個圖像轉化成numpy數組,並返回一個68 x2元素矩陣,輸入圖像的每個特征點對應每行的一個x,y坐標。 def get_landmarks(im): rects = detector(im, 1) return numpy.matrix([[p.x, p.y] for p in predictor(im, rects[0]).parts()]) #多張臉使用的一個例子 def get_landmarks_m(im): dets = detector(im, 1) #臉的個數 print("Number of faces detected: {}".format(len(dets))) for i in range(len(dets)): facepoint = np.array([[p.x, p.y] for p in predictor(im, dets[i]).parts()]) for i in range(68): #標記點 im[facepoint[i][1]][facepoint[i][0]] = [232,28,8] return im #打印關鍵點矩陣 print("face_landmark:") print(get_landmarks(img)) #等待點擊 dlib.hit_enter_to_continue()