MTCNN的用法

print('get frame faces....')
    detector = MTCNN()
    save_path = [ './processed/validate/Northwind/']
    paths = ['./img/validate/Northwind/']
    for index, path in enumerate(paths):
        dirs = get_dirs(path)
        loader = tqdm(dirs)
        for d in loader:
            os.makedirs(save_path[index] + d.split('/')[-1], exist_ok=True)
            files = get_files(d)
            for file in files:
                img_path = d + '/' + file
                s_path = save_path[index] + d.split('/')[-1] + '/' + file
                img = cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
                info = detector.detect_faces(img)       #这里的 info是什么意思
                if (len(info) > 0):
                    x, y, width, height = info[0]['box']
                    confidence = info[0]['confidence']
                    b, g, r = cv2.split(img)
                    img = cv2.merge([r, g, b])
                    img = img[y:y + height, x:x + width, :]
                    cv2.imwrite(s_path, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
                    loader.set_description('confidence:{:4f} img:{}'.format(confidence, img_path))

info = detector.detect_faces(img)
这里的 info是什么意思?
如果检测到了人脸:
info 是一个包含多个字典的列表,每个字典对应一张检测到的人脸。
每个字典的常见字段包括:
‘box’:人脸的边界框,格式为 [x, y, width, height]。
‘confidence’:检测到人脸的置信度(0 到 1 的值)。
‘keypoints’:关键点的位置,包括眼睛、鼻子和嘴巴的坐标,格式为:

{
    'left_eye': (x1, y1),
    'right_eye': (x2, y2),
    'nose': (x3, y3),
    'mouth_left': (x4, y4),
    'mouth_right': (x5, y5)
}

如果 info 的内容如下:


info = [
    {
        'box': [100, 50, 200, 200],  # 人脸的边界框,左上角坐标和宽高
        'confidence': 0.98,         # 检测的置信度
        'keypoints': {
            'left_eye': (150, 100),
            'right_eye': (250, 100),
            'nose': (200, 150),
            'mouth_left': (150, 220),
            'mouth_right': (250, 220)
        }
    }
]

info[0][‘box’]: 表示人脸的边界框,值为 [100, 50, 200, 200],对应 (x=100, y=50, width=200, height=200)。
info[0][‘confidence’]: 表示检测到人脸的置信度,为 0.98。
info[0][‘keypoints’]: 包含五个关键点的位置,分别是左右眼、鼻子和嘴角。
4. 在代码中的使用
代码片段中的操作:

info = detector.detect_faces(img)
if len(info) > 0:
    x, y, width, height = info[0]['box']
    confidence = info[0]['confidence']

含义:
检测人脸后,如果有检测结果(len(info) > 0),则取出第一个人脸的信息(info[0])。
提取边界框(box)的坐标和尺寸。
提取检测置信度(confidence)。
作用:
将检测到的人脸区域裁剪出来(img[y:y + height, x:x + width, :])。
保存为裁剪后的图片。

img = img[y:y + height, x:x + width, :]
cv2.imwrite(s_path, img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LinlyZhai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值