AI计算机视觉进阶项目(一)——带口罩识别检测(1)

本博客介绍了一个AI计算机视觉项目,专注于口罩检测,包括正常佩戴、未佩戴和不规范佩戴三种情况。项目首先从读取图像开始,接着裁剪出人脸,通过模型加载进行人脸检测,最后将裁剪的图像转换为Blob格式以加速模型训练。
摘要由CSDN通过智能技术生成

目录

1项目概述

2.项目流程

3.项目实现

3.1 读取图像

3.2裁剪出人脸

3.2.1模型加载

 3.3将裁剪后的图像转为Blob图像

 4.完整程序

 5.下一章节内容

官方合作微信:gldz_super

本专栏《AI计算机视觉进阶项目》主要以计算机视觉实战项目为主,第一个项目为口罩检测:该项目将分为几个模块进行展示结束:

1项目概述

        要求如下:检测出视频中的人是否带口罩,如果带了进行检测,该项目主要分为正常佩戴、未佩戴口罩以及不规范(漏鼻子)三个类别。训练的数据集有正常佩戴1915张、未佩戴口罩1918张以及不规范(漏鼻子)1919张数据。

2.项目流程

        本节主要完成项目的数据处理模块,由于随着数据量过大训练的参数越多,基于此为了加速模型,对图像进行裁剪主要的人脸区域以及转为Blob格式进行处理(减去均值)具体如下所示:

  1. 读取图像
  2. 裁剪出人脸
  3. 转为Blob格式

3.项目实现

3.1 读取图像

 # 读取图像
    img = cv2.imread('./mask_people.jpg')
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.show()

3.2裁剪出人脸

3.2.1模型加载

# 剪切人脸,为了图像的计算效率对人的图像进行剪切
    # 加载SSD模型:将人脸剪切出来
    face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt',
                                             'weights/res10_300x300_ssd_iter_140000.caffemodel')

    # 裁剪后的人脸
    face_crop = face_detect(img)
    plt.imshow(cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB))
    plt.show()

face_detect函数如下所示:

def face_detect(img):
    # 转为blob:进行均值相减,加快运算
    img_blob = cv2.dnn.blobFromImage(img, 1, (300, 300), (104, 177, 123), swapRB=True)
    # 输入
    face_detector.setInput(img_blob)
    # 推理
    detections = face_detector.forward()
    print(detections.shape)

    # 遍历结果
    # 获取原图大小
    img_h, img_w = img.shape[:2]

    # 人数
    person_count = detections.shape[2]

    for face_index in range(person_count):
        # 置信度
        confidence = detections[0, 0, face_index, 2]
        # print(confidence) # 发现大部分置信度都是比较小,我们这挑选比较大的置信度
        if confidence >0.5: # 表示检测到人脸
            locations = detections[0, 0, face_index, 3:7]*np.array([img_w, img_h, img_w, img_h]) # 由于之前归一化了,所以拿到位置要乘以宽高返回到原图大小
            # 用矩形框画出人脸:由于画矩形框不能有小数,所以需要取整
            l, t, r, b = locations.astype('int')
            # cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 5)
            # plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
            # plt.show()
            return img[t:b, l:r]
    return None

裁剪结果如下所示:

 3.3将裁剪后的图像转为Blob图像

# 转为Blob图像
def imgBlob(img):
    img_blob = cv2.dnn.blobFromImage(img, 1, (100, 100), (104, 177, 123), swapRB=True)
    # 转变后图像由三维会变为四维,所以需要进行维度压缩:变为:(1, 3, 300, 300)
    img_squeeze = np.squeeze(img_blob)  # 变为(3, 300, 300)所以需要转置将3放在后面
    img_squeeze = img_squeeze.T # 输出人倒立横着的,所以需要旋转90度
    # 旋转
    img_rotate = cv2.rotate(img_squeeze, cv2.ROTATE_90_CLOCKWISE)
    # 再进行镜像翻转图像就和原图位置一样
    img_flip = cv2.flip(img_rotate, 1)
    # 去除负数:将小于0设置为0,大于0不变
    img_flip = np.maximum(img_flip, 0)
    # 归一化
    img_blob = img_flip / img_flip.max()

    return img_blob

 4.完整程序

import cv2
import numpy as np
import matplotlib.pyplot as plt
import os, glob
import tqdm

def face_detect(img):
    # 转为blob:进行均值相减,加快运算
    img_blob = cv2.dnn.blobFromImage(img, 1, (300, 300), (104, 177, 123), swapRB=True)
    # 输入
    face_detector.setInput(img_blob)
    # 推理
    detections = face_detector.forward()
    print(detections.shape)

    # 遍历结果
    # 获取原图大小
    img_h, img_w = img.shape[:2]

    # 人数
    person_count = detections.shape[2]

    for face_index in range(person_count):
        # 置信度
        confidence = detections[0, 0, face_index, 2]
        # print(confidence) # 发现大部分置信度都是比较小,我们这挑选比较大的置信度
        if confidence >0.5: # 表示检测到人脸
            locations = detections[0, 0, face_index, 3:7]*np.array([img_w, img_h, img_w, img_h]) # 由于之前归一化了,所以拿到位置要乘以宽高返回到原图大小
            # 用矩形框画出人脸:由于画矩形框不能有小数,所以需要取整
            l, t, r, b = locations.astype('int')
            # cv2.rectangle(img, (l, t), (r, b), (0, 255, 0), 5)
            # plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
            # plt.show()
            return img[t:b, l:r]
    return None

# 转为Blob图像
def imgBlob(img):
    img_blob = cv2.dnn.blobFromImage(img, 1, (100, 100), (104, 177, 123), swapRB=True)
    # 转变后图像由三维会变为四维,所以需要进行维度压缩:变为:(1, 3, 300, 300)
    img_squeeze = np.squeeze(img_blob)  # 变为(3, 300, 300)所以需要转置将3放在后面
    img_squeeze = img_squeeze.T # 输出人倒立横着的,所以需要旋转90度
    # 旋转
    img_rotate = cv2.rotate(img_squeeze, cv2.ROTATE_90_CLOCKWISE)
    # 再进行镜像翻转图像就和原图位置一样
    img_flip = cv2.flip(img_rotate, 1)
    # 去除负数:将小于0设置为0,大于0不变
    img_flip = np.maximum(img_flip, 0)
    # 归一化
    img_blob = img_flip / img_flip.max()

    return img_blob

if __name__ =="__main__":
    # 读取图像
    img = cv2.imread('./mask_people.jpg')
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.show()

    # 剪切人脸,为了图像的计算效率对人的图像进行剪切
    # 加载SSD模型:将人脸剪切出来
    face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt',
                                             'weights/res10_300x300_ssd_iter_140000.caffemodel')

    # 裁剪后的人脸
    face_crop = face_detect(img)
    plt.imshow(cv2.cvtColor(face_crop, cv2.COLOR_BGR2RGB))
    plt.show()

    img_blob = imgBlob(face_crop)
    plt.imshow(cv2.cvtColor(img_blob, cv2.COLOR_BGR2RGB))
    plt.show()

 5.下一章节内容

  1. 处理所有图像
  2. 存储为numpy格式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI炮灰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值