Tensorflow 口罩识别

Tensorflow 口罩识别

1、数据集的选择

为了避免对图像繁琐的标注,我们选择一个已经人脸区域已经被分割好、类别也已经标注好的数据集。本文选择了Kaggle上的一个口罩数据库。但是这个数据库有坑,请直接拉到文章末尾。

G:.
├─Test
│  ├─WithMask
│  └─WithoutMask
├─Train
│  ├─WithMask
│  └─WithoutMask
└─Validation
    ├─WithMask
    └─WithoutMask

在这里插入图片描述

2、数据集的处理

这里的数据集虽然已经将人脸区域很好的分割,但是其图像的大小不同,因此在训练之前应当选择一个合适的大小,考虑到我这里的计算资源有限,我就把整个数据集中的图片都划分成25*25的图片。

def resize_face_25_25(path):
    processed_images = []
    file_names = os.listdir(path)
    for file in file_names:
        read_path = path  + '\\' + file
        image = cv2.imread(read_path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 转灰度图
        image_25_25 = cv2.resize(image,(25,25))
        image_25_25=image_25_25[:,:,np.newaxis]
        processed_images.append(image_25_25)
    processed_images = np.array(processed_images)
    print(processed_images.shape)
    return processed_images

train_withmask = resize_face_25_25(train_withmask_path)
train_withoutmask = resize_face_25_25(train_withoutmask_path)
valid_withmask = resize_face_25_25(valid_withmask_path)
valid_withoutmask = resize_face_25_25(valid_withoutmask_path)
test_withmask = resize_face_25_25(test_withmask_path)
test_without_mask = resize_face_25_25(test_without_mask_path)

原始数据集中,正例(戴口罩)和反例(不带口罩)是分开的,这里我们把正反例拼接到一起。

# 拼接
X_train = np.concatenate([train_withmask, train_withoutmask])
y_train = np.concatenate([np.ones(5000), np.zeros(5000)]) # 戴口罩是1,不戴口罩是0

X_valid = np.concatenate([valid_withmask, valid_withoutmask])
y_valid = np.concatenate([np.ones(400), np.zeros(400)]) 
  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值