python检测人脸用的什么库_python 基于dlib库的人脸检测

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

代码

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值