python 基于dlib庫的人臉檢測的實(shí)現(xiàn)
本周暫時(shí)比較清閑,可以保持每日一更的速度。
國外身份證項(xiàng)目新增需求,檢測出身份證正面的人臉。最開始考慮mobilenet-ssd,經(jīng)同事提醒,有現(xiàn)成的人臉庫dlib,那就用傳統(tǒng)方法嘗試一下。
dlib安裝
dlib的安裝小費(fèi)一波周折,我的python版本是3.6,直接pip install dlib安裝失敗。https://pypi.org/project/dlib/19.6.0/找到python3.6對應(yīng)的whl文件下載安裝或者直接pip install dlib==19.6.0 提示Successfully installed dlib-19.6.0安裝成功。事情沒那么簡單,import dlib時(shí)報(bào)錯(cuò): ImportError: DLL load failed: 找不到指定的模塊。
還是版本的問題,查找最新版本的whl :https://pypi.org/simple/dlib/
下載 dlib-19.8.1-cp36-cp36m-win_amd64.whl 然后cd到相應(yīng)的目錄下 pip install dlib-19.8.1-cp36-cp36m-win_amd64.whl
代碼
import dlib
import cv2
import os
def resize(img, width=None, height=None, inter=cv2.INTER_AREA):
"""
initialize the dimensions of the input image and obtain
the image size
"""
dim = None
(h, w) = img.shape[:2]
if width is None and height is None:
return img
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(img, dim, interpolation=inter)
# return the resized image
return resized
# 使用 Dlib 的正面人臉檢測器 frontal_face_detector
detector = dlib.get_frontal_face_detector()
# 圖片所在路徑
imgs_path = 'test/'
filelist = os.listdir(imgs_path)
# 使用 detector 檢測器來檢測圖像中的人臉
for img_path in filelist:
img = cv2.imread(imgs_path + img_path)
img = resize(img, width=512)
faces = detector(img, 1)
print("人臉數(shù) / Faces in all: ", len(faces))
for i, d in enumerate(faces):
w = d.right() - d.left()
h = d.bottom() - d.top()
d_left = int(d.left() - w * 0.25)
d_right = int(d.right() + w * 0.25)
d_top = int(d.top() - w * 0.70)
d_bottom = int(d.bottom() + w * 0.2)
print("第", i + 1, "個(gè)人臉的矩形框坐標(biāo):",
"left:", d_left, "right:", d_right, "top:", d_top, "bottom:", d_bottom)
cv2.rectangle(img, tuple([d_left, d_top]), tuple([d_right, d_bottom]), (0, 255, 255), 2)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.imwrite('./result.jpg',img)
隨便網(wǎng)上找了張圖測試,效果如下

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python結(jié)合opencv實(shí)現(xiàn)人臉檢測與跟蹤
- Python+OpenCV人臉檢測原理及示例詳解
- python中使用OpenCV進(jìn)行人臉檢測的例子
- Python OpenCV利用筆記本攝像頭實(shí)現(xiàn)人臉檢測
- Python OpenCV調(diào)用攝像頭檢測人臉并截圖
- Python基于OpenCV實(shí)現(xiàn)視頻的人臉檢測
- python opencv人臉檢測提取及保存方法
- 50行Python代碼實(shí)現(xiàn)人臉檢測功能
- python利用OpenCV2實(shí)現(xiàn)人臉檢測
- python實(shí)現(xiàn)人臉檢測的簡單實(shí)例
相關(guān)文章
Python實(shí)現(xiàn)的遞歸神經(jīng)網(wǎng)絡(luò)簡單示例
這篇文章主要介紹了Python實(shí)現(xiàn)的遞歸神經(jīng)網(wǎng)絡(luò),是一篇摘錄自github代碼片段的文章,涉及Python遞歸與數(shù)學(xué)運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Python3 實(shí)現(xiàn)文件批量重命名示例代碼
在Python中os模塊里,os.renames() 方法用于遞歸重命名目錄或文件。這篇文章主要介紹了Python3 文件批量重命名操作示例,需要的朋友可以參考下2019-06-06
Python機(jī)器學(xué)習(xí)之K-Means聚類實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)之K-Means聚類的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文
游戲界面中文字也是非常常見的元素之一,pygame專門提供了Font模塊來支持文字的顯示,下面這篇文章主要給大家介紹了關(guān)于pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python實(shí)現(xiàn)圖像和辦公文檔處理的方法和技巧
本文介紹了Python實(shí)現(xiàn)圖像和辦公文檔處理的方法和技巧,包括使用Pillow庫處理圖像、使用OpenCV庫進(jìn)行圖像識別和處理、使用PyPDF2庫處理PDF文檔、使用docx和xlwt庫處理Word和Excel文檔等,幫助讀者更好地掌握Python在圖像和辦公文檔處理方面的應(yīng)用2023-05-05
python GUI庫圖形界面開發(fā)之PyQt5布局控件QVBoxLayout詳細(xì)使用方法與實(shí)例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QVBoxLayout詳細(xì)使用方法與實(shí)例,需要的朋友可以參考下2020-03-03

