基于YOLOv5的人体行为检测
项目概述
本项目旨在使用YOLOv5进行人体行为检测,包括五种行为:跌倒、站立、蹲下、坐下、跑。项目包含数据集、报告和运行视频,并提供了标注文件(XML和TXT格式),可以直接用于模型训练。
数据集
总图像数:3000张
训练集:2400张
验证集:600张
类别:
跌倒 (Fall)
站立 (Stand)
蹲下 (Squat)
坐下 (Sit)
跑 (Run)
每个类别的图像数量和标注框数量如下:
Fall:600张图像,600个标注框
Stand:600张图像,600个标注框
Squat:600张图像,600个标注框
Sit:600张图像,600个标注框
Run:600张图像,600个标注框
总计:3000张图像,3000个标注框。
数据集结构
假设数据集的目录结构如下:
深色版本
human_behavior_detection_dataset/
├── images/
│ ├── train/
│ │ ├── 000001.jpg
│ │ ├── 000002.jpg
│ │ └── …
│ ├── val/
│ │ ├── 000001.jpg
│ │ ├── 000002.jpg
│ │ └── …
├── labels/
│ ├── train/
│ │ ├── 000001.txt
│ │ ├── 000002.txt
│ │ └── …
│ ├── val/
│ │ ├── 000001.txt
│ │ ├── 000002.txt
│ │ └── …
├── labels_voc/
│ ├── train/
│ │ ├── 000001.xml
│ │ ├── 000002.xml
│ │ └── …
│ ├── val/
│ │ ├── 000001.xml
│ │ ├── 000002.xml
│ │ └── …
环境准备
安装依赖:
bash
深色版本
pip install -r requirements.txt
其中 requirements.txt 包含以下内容:
txt
深色版本
numpy
opencv-python
pyyaml
torch
torchvision
albumentations
matplotlib
pandas
下载YOLOv5:
bash
深色版本
git clone https://github.com/ultralytics/yolov5
cd yolov5
pip install -r requirements.txt
配置数据集
创建一个配置文件 human_behavior_detection.yaml,内容如下:
yaml
深色版本
path: human_behavior_detection_dataset # 数据集路径
train: images/train # 训练集图像路径
val: images/val # 验证集图像路径
nc: 5 # 类别数
names: [‘Fall’, ‘Stand’, ‘Squat’, ‘Sit’, ‘Run’] # 类别名称
训练模型
训练模型:
bash
深色版本
python train.py --data human_behavior_detection.yaml --img 640 --batch 16 --epochs 100 --weights yolov5s.pt
代码示例:
python
深色版本
from yolov5.models.experimental import attempt_load
from yolov5.utils.datasets import LoadImages
from yolov5.utils.general import non_max_suppression, scale_coords, xyxy2xywh
import torch
import cv2
加载预训练模型
weights = ‘yolov5s.pt’
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
model = attempt_load(weights, map_location=device)
加载图像
dataset = LoadImages(‘human_behavior_detection_dataset/images/val/000001.jpg’)
进行预测
for path, img, im0s, vid_cap in dataset:
img = torch.from_numpy(img).to(device)
img = img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
pred = model(img)[0] pred = non_max_suppression(pred, 0.25, 0.45) for i, det in enumerate(pred): # detections per image if len(det): det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round() for *xyxy, conf, cls in reversed(det): label = f'{model.module.names[int(cls)]} {conf:.2f}' plot_one_box(xyxy, im0s, label=label, color=(0, 255, 0), line_thickness=3)显示图像
cv2.imshow(‘Prediction’, im0s)
cv2.waitKey(0)
cv2.destroyAllWindows()
模型评估
评估模型:
bash
深色版本
python val.py --data human_behavior_detection.yaml --img 640 --weights runs/train/exp/weights/best.pt
代码示例:
python
深色版本
from yolov5.utils.general import check_img_size
from yolov5.utils.metrics import ap_per_class, box_iou
from yolov5.utils.torch_utils import select_device, time_synchronized
import torch
import numpy as np
加载预训练模型
weights = ‘runs/train/exp/weights/best.pt’
device = select_device(‘’)
model = attempt_load(weights, map_location=device)
设置图像尺寸
imgsz = check_img_size(640, s=model.stride.max())
加载验证集
dataset = LoadImages(‘human_behavior_detection_dataset/images/val’, img_size=imgsz)
评估
iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95
niou = iouv.numel()
seen = 0
names = model.module.names if hasattr(model, ‘module’) else model.names
nc = len(names) # number of classes
stats, ap, ap_class = [], [], []
for path, img, im0s, vid_cap in dataset:
img = torch.from_numpy(img).to(device)
img = img.float() # uint8 to fp16/32
img /= 255.0 # 0 - 255 to 0.0 - 1.0
if img.ndimension() == 3:
img = img.unsqueeze(0)
# Inference t1 = time_synchronized() pred = model(img)[0] t2 = time_synchronized() # Apply NMS pred = non_max_suppression(pred, 0.25, 0.45) # Process detections for i, det in enumerate(pred): # detections per image seen += 1 if len(det): det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round() # Convert to xywh format det[:, :4] = xyxy2xywh(det[:, :4]) # Load ground truth with open(f'human_behavior_detection_dataset/labels/val/{Path(path).stem}.txt', 'r') as f: labels = [line.split() for line in f.readlines()] labels = np.array(labels, dtype=np.float32) labels[:, 1:] = xywh2xyxy(labels[:, 1:]) # Compute IoU iou = box_iou(labels[:, 1:], det[:, :4]) correct = torch.zeros((det.shape[0], niou), dtype=torch.bool, device=device) for di, d in enumerate(det): i = iou[di].max(0) # best iou and index if i[0] > iouv[0]: correct[di] = i[0] > iouv # Append statistics stats.append((correct.cpu(), det[:, 4].cpu(), det[:, 5].cpu(), labels[:, 0].cpu()))Compute statistics
stats = [np.concatenate(x, 0) for x in zip(*stats)]
if len(stats) and stats[0].any():
tp, fp, p, r, f1, ap, ap_class = ap_per_class(*stats, plot=False, save_dir=‘.’, names=names)
ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
mp, mr, mf1, map50, map = p.mean(), r.mean(), f1.mean(), ap50.mean(), ap.mean()
print(f’mP: {mp:.3f}, mR: {mr:.3f}, mF1: {mf1:.3f}, mAP@0.5: {map50:.3f}, mAP@0.5:0.95: {map:.3f}')
可视化结果
你可以使用以下代码来可视化模型的预测结果:
python
深色版本
import cv2
import torch
from yolov5.models.experimental import attempt_load
from yolov5.utils.general import non_max_suppression, scale_coords, xyxy2xywh
from yolov5.utils.plots import plot_one_box
加载训练好的模型
weights = ‘runs/train/exp/weights/best.pt’
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
model = attempt_load(weights, map_location=device)
读取测试图像
image_path = ‘human_behavior_detection_dataset/images/val/000001.jpg’
image = cv2.imread(image_path)
进行预测
results = model(image)
可视化预测结果
for result in results:
boxes = result.boxes.xyxy # 获取边界框
confidences = result.boxes.conf # 获取置信度
class_ids = result.boxes.cls # 获取类别ID
for box, confidence, class_id in zip(boxes, confidences, class_ids): x1, y1, x2, y2 = map(int, box) label = f'{model.names[int(class_id)]} {confidence:.2f}' color = (0, 255, 0) # 绿色 plot_one_box([x1, y1, x2, y2], image, label=label, color=color, line_thickness=3)显示图像
cv2.imshow(‘Prediction’, image)
cv2.waitKey(0)
cv2.destroyAllWindows()
报告
项目报告
项目背景:
人体行为检测在智能监控、健康护理等领域具有广泛的应用前景。本项目旨在使用YOLOv5实现对人体五种行为(跌倒、站立、蹲下、坐下、跑)的检测。
数据集:
数据集包含3000张图像,标注文件为YOLO格式和VOC格式。数据集已按8:2的比例划分为训练集和验证集。
模型训练:
使用YOLOv5进行训练,训练过程包括数据增强、模型选择、损失函数优化等。
模型评估:
评估指标包括mAP@0.5、mAP@0.5:0.95、精确率、召回率等。
实验结果:
模型在验证集上的表现良好,mAP@0.5达到XX%,mAP@0.5:0.95达到XX%。
改进策略:
未来可以尝试更多的数据增强方法、优化模型架构、引入注意力机制等,以进一步提升模型性能。
运行视频
运行视频:包含模型在验证集上的预测结果,展示了模型对五种行为的检测效果。
总结
以上步骤涵盖了从数据集准备到模型训练、评估和可视化的完整流程