目录
官方合作微信:gldz_super
本专栏《AI计算机视觉进阶项目》主要以计算机视觉实战项目为主,第一个项目为口罩检测:该项目将分为几个模块进行展示结束:
1项目概述
要求如下:检测出视频中的人是否带口罩,如果带了进行检测,该项目主要分为正常佩戴、未佩戴口罩以及不规范(漏鼻子)三个类别。训练的数据集有正常佩戴1915张、未佩戴口罩1918张以及不规范(漏鼻子)1919张数据。
2.项目流程
本节主要完成项目的数据处理模块,由于随着数据量过大训练的参数越多,基于此为了加速模型,对图像进行裁剪主要的人脸区域以及转为Blob格式进行处理(减去均值)具体如下所示:
- 读取图像
- 裁剪出人脸
- 转为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.下一章节内容
- 处理所有图像
- 存储为numpy格式