基于深度学习课堂行为检测 6种行为 举手识别 阅读检测 书写检测 手机使用数据集 基于深度学习/yolov5的课堂行为检测6种行为可识别hand-raising/reading/writing/using phone/bowing the head/leaning over the table/共6种基于YOLOv5的课堂行为检测项目的详细介绍包括数据集准备、模型训练、推理和可视化。该项目可以识别以下6种行为举手hand-raising、阅读reading、书写writing、使用手机using phone、低头bowing the head、趴在桌子上leaning over the table。项目结构深色版本ClassroomBehaviorDetection/ ├── data/ │ ├── images/ │ │ ├── train/ │ │ ├── val/ │ │ └── test/ │ └── labels/ │ ├── train/ │ ├── val/ │ └── test/ ├── models/ │ └── yolov5/ ├── utils/ │ ├── data_loader.py │ ├── metrics.py │ └── plot.py ├── main.py ├── train.py ├── infer.py └── README.md数据集准备数据集格式假设你的数据集已经标注好并且格式为YOLOv5所需的格式。每个图像对应一个.txt文件包含目标框的坐标和类别标签。数据集目录结构深色版本data/ ├── images/ │ ├── train/ │ ├── val/ │ └── test/ └── labels/ ├── train/ ├── val/ └── test/数据集划分确保你的数据集已经划分为训练集、验证集和测试集。例如train目录下的图像和标签用于训练val目录下的用于验证test目录下的用于测试。安装YOLOv5首先克隆YOLOv5仓库并安装必要的依赖项bash深色版本cd models/git clone https://github.com/ultralytics/yolov5.gitcd yolov5pip install -r requirements.txt3. 数据配置文件创建一个数据配置文件data/classroom_behavior.yaml指定数据集路径和类别信息yaml深色版本train: …/data/images/train/val: …/data/images/val/test: …/data/images/test/nc: 6 # 类别数量names:hand-raisingreadingwritingusing phonebowing the headleaning over the table训练模型编写一个训练脚本train.pypython深色版本import osimport torchfrom yolov5 import traindef main():# 设置训练参数data_yaml ‘…/data/classroom_behavior.yaml’model_yaml ‘yolov5s.yaml’epochs 100batch_size 16img_size 640device ‘cuda’ if torch.cuda.is_available() else ‘cpu’# 开始训练 train.run(datadata_yaml, modelmodel_yaml, epochsepochs, batchbatch_size, imgszimg_size, devicedevice)ifname ‘main’:main()运行训练脚本bash深色版本python train.py5. 推理和可视化编写一个推理脚本infer.py用于加载训练好的模型并对新图像进行预测python深色版本import osimport torchimport cv2import numpy as npfrom yolov5.models.experimental import attempt_loadfrom yolov5.utils.general import non_max_suppression, scale_coordsfrom yolov5.utils.plots import Annotatordef load_model(weights_path, device):model attempt_load(weights_path, map_locationdevice)model.eval()return modeldef infer_image(model, img_path, img_size, device):img cv2.imread(img_path)img cv2.cvtColor(img, cv2.COLOR_BGR2RGB)img cv2.resize(img, (img_size, img_size))img img.transpose(2, 0, 1)img img.astype(np.float32) / 255.0img torch.from_numpy(img).unsqueeze(0).to(device)with torch.no_grad(): pred model(img)[0] pred non_max_suppression(pred, conf_thres0.25, iou_thres0.45) return pred, imgdef draw_boxes(img, pred, names):annotator Annotator(img, line_width2, examplestr(names))for i, det in enumerate(pred):if len(det):det[:, :4] scale_coords(img.shape[2:], det[:, :4], img.shape[2:]).round()for *xyxy, conf, cls in reversed(det):c int(cls)label f’{names[c]} {conf:.2f}’annotator.box_label(xyxy, label, colorcolors(c, True))return annotator.result()ifname ‘main’:weights_path ‘runs/train/exp/weights/best.pt’img_path ‘…/data/images/test/000001.jpg’img_size 640device ‘cuda’ if torch.cuda.is_available() else ‘cpu’model load_model(weights_path, device) pred, img infer_image(model, img_path, img_size, device) # 读取原始图像 original_img cv2.imread(img_path) # 绘制预测框 annotated_img draw_boxes(original_img, pred, names[hand-raising, reading, writing, using phone, bowing the head, leaning over the table]) # 显示结果 cv2.imshow(Result, annotated_img) cv2.waitKey(0) cv2.destroyAllWindows()评价指标编写一个脚本evaluate.py来计算模型的评价指标如IoU、mAP等python深色版本import osimport torchfrom yolov5 import valfrom yolov5.utils.general import check_img_sizefrom yolov5.utils.datasets import create_dataloaderdef main():data_yaml ‘…/data/classroom_behavior.yaml’weights_path ‘runs/train/exp/weights/best.pt’img_size 640batch_size 16device ‘cuda’ if torch.cuda.is_available() else ‘cpu’# 加载数据 dataloader create_dataloader(../data/images/val/, img_size, batch_size, 32, single_clsFalse, pad0.5, rectTrue)[0] # 加载模型 model attempt_load(weights_path, map_locationdevice) model.eval() # 验证模型 val.run(datadata_yaml, weightsweights_path, batch_sizebatch_size, imgszimg_size, devicedevice, dataloaderdataloader)ifname ‘main’:main()运行评价脚本bash深色版本python evaluate.py7. 可视化标注文件编写一个脚本visualize_annotations.py来可视化标注文件python深色版本import osimport cv2import numpy as npdef read_labels(label_path):with open(label_path, ‘r’) as f:lines f.readlines()labels []for line in lines:parts line.strip().split()class_id int(parts[0])x_center float(parts[1])y_center float(parts[2])width float(parts[3])height float(parts[4])labels.append((class_id, x_center, y_center, width, height))return labelsdef draw_boxes_on_image(img_path, label_path, names):img cv2.imread(img_path)labels read_labels(label_path)for class_id, x_center, y_center, width, height in labels: x int((x_center - width / 2) * img.shape[1]) y int((y_center - height / 2) * img.shape[0]) w int(width * img.shape[1]) h int(height * img.shape[0]) color (0, 255, 0) cv2.rectangle(img, (x, y), (x w, y h), color, 2) label names[class_id] cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2) return imgifname ‘main’:img_path ‘…/data/images/val/000001.jpg’label_path ‘…/data/labels/val/000001.txt’names [‘hand-raising’, ‘reading’, ‘writing’, ‘using phone’, ‘bowing the head’, ‘leaning over the table’]annotated_img draw_boxes_on_image(img_path, label_path, names) cv2.imshow(Annotated Image, annotated_img) cv2.waitKey(0) cv2.destroyAllWindows()运行项目确保数据集和标签文件已经准备好并放在相应的目录中。运行训练脚本bash深色版本python train.py运行推理脚本bash深色版本python infer.py运行评价脚本bash深色版本python evaluate.py运行可视化标注文件脚本bash深色版本python visualize_annotations.py代码说明数据集准备确保数据集已经标注好并且格式为YOLOv5所需的格式。训练模型使用train.py脚本训练模型指定数据配置文件和训练参数。推理和可视化使用infer.py脚本加载训练好的模型并对新图像进行预测使用visualize_annotations.py脚本可视化标注文件。评价指标使用evaluate.py脚本计算模型的评价指标。希望这些代码和说明能帮助你完成基于YOLOv5的课堂行为检测项目。