日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中dlib库_python 基于dlib库的人脸检测的实现

發布時間:2025/3/8 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中dlib库_python 基于dlib库的人脸检测的实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本周暫時比較清閑,可以保持每日一更的速度。

國外身份證項目新增需求,檢測出身份證正面的人臉。最開始考慮mobilenet-ssd,經同事提醒,有現成的人臉庫dlib,那就用傳統方法嘗試一下。

dlib安裝

dlib的安裝小費一波周折,我的python版本是3.6,直接pip install dlib安裝失敗。https://pypi.org/project/dlib/19.6.0/找到python3.6對應的whl文件下載安裝或者直接pip install dlib==19.6.0 提示Successfully installed dlib-19.6.0安裝成功。事情沒那么簡單,import dlib時報錯: ImportError: DLL load failed: 找不到指定的模塊。

下載dlib-19.8.1-cp36-cp36m-win_amd64.whl然后cd到相應的目錄下?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("人臉數 / 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, "個人臉的矩形框坐標:",

"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)

隨便網上找了張圖測試,效果如下

以上就是本文的全部內容,希望對大家的學習有所幫助

您可能感興趣的文章:

總結

以上是生活随笔為你收集整理的python中dlib库_python 基于dlib库的人脸检测的实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。