資源簡介
python調用dlib庫實現簡單的人臉識別, 附python源碼和dlib庫。
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
#?By?309867165@qq.com??
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()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-01-14?09:54??img\
?????文件???????19273??2019-01-14?09:53??img\1.jpg
?????文件???????10886??2019-01-14?09:53??img\2.jpg
?????文件????????9570??2019-01-14?09:54??img\3.jpg
?????文件????????1794??2019-01-14?11:05??pyface.py
?????文件??????164487??2019-01-14?11:48??python調用dlib庫實現簡單的人臉識別.docx
?????文件????16106007??2019-01-14?09:11??dlib-19.16.zip
評論
共有 條評論