数据集处理:python基于mtcnn的人脸检测并保存

对现有图像进行检测保存:

一、代码实现

import os
import cv2
from mtcnn.mtcnn import MTCNN

outer_path = '/Users/xuqiong/AgeGender/test_img_process/Qbbre'
filelist = os.listdir(outer_path)  # 列举图片

detector = MTCNN()

for item in filelist:
    src = os.path.join(os.path.abspath(outer_path), item)
    input_img = cv2.imread(src)

    detected = detector.detect_faces(input_img)
    if len(detected) > 0:  # 大于0则检测到人脸
        for i, d in enumerate(detected):  # 单独框出每一张人脸
            x1, y1, w, h = d['box']
            x2 = x1 + w
            y2 = y1 + h
            image = input_img[(y1-10):(y2+10), (x1-10):(x2+10)]
            cv2.imwrite(src, image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

如果保存的人脸想要更多留白,可以修改:image = input_img[(y1-10):(y2+10), (x1-10):(x2+10)]中的10。

 

备注:

cv2.IMWRITE_JPEG_QUALITY类型为Long,必须转换成int

从0到9,压缩级别越高,图像尺寸越小
 

二、效果

检测保存前:

检测保存后:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值