行人持刀检测数据集 voc yolo

行人持刀检测数据集 9000张 持刀检测 带标注 voc yolo

行人持刀检测数据集

数据集描述

该数据集旨在用于行人持刀行为的检测任务,涵盖了多种场景下的行人图像,特别是那些携带刀具的行人。数据集包含大量的图像及其对应的标注信息,可用于训练计算机视觉模型,以识别和定位行人手中的刀具。

数据规模

数据集共有9000张图像,这些图像都带有详细的标注信息。

类别及数量

数据集中的主要类别为:

  1. 行人持刀 (person_with_knife):标注出图像中携带刀具的行人。
标注格式

数据集中的标注信息采用了VOC(Visual Object Classes)格式,每个图像都有一个对应的XML文件,记录了每个对象的位置信息(边界框坐标)和类别标签。此外,也可以选择YOLO格式的标注文件(TXT文件),方便使用YOLO系列模型进行训练。

数据集结构

典型的数据集目录结构如下:

 

深色版本

1pedestrian_knife_detection_dataset/
2├── Annotations/
3│   ├── img_0001.xml
4│   ├── img_0002.xml
5│   └── ...
6├── ImageSets/
7│   ├── Main/
8│   │   ├── train.txt
9│   │   ├── val.txt
10│   │   └── test.txt
11├── JPEGImages/
12│   ├── img_0001.jpg
13│   ├── img_0002.jpg
14│   └── ...
15└── labels/
16    ├── train/
17    │   ├── img_0001.txt
18    │   ├── img_0002.txt
19    └── val/
20        ├── img_0001.txt
21        ├── img_0002.txt
应用场景

该数据集可以用于以下应用场景:

  • 公共安全:实时监测公共场所中的行人是否携带刀具,提高公共安全水平。
  • 安防监控:辅助安防系统的视频监控,及时发现潜在威胁。
  • 事件响应:在发生紧急事件时,快速识别携带刀具的人员,以便采取相应措施。
  • 科研分析:用于研究行人持刀行为的识别技术和算法。

示例代码

以下是一个使用Python和相关库(如OpenCV、PyTorch等)来加载和展示数据集的简单示例代码:

 

python

深色版本

1import os
2import cv2
3import xml.etree.ElementTree as ET
4from PIL import Image
5import numpy as np
6
7# 数据集路径
8dataset_path = 'path/to/pedestrian_knife_detection_dataset/'
9
10# 加载图像和标签
11def load_image_and_label(image_path, annotation_path):
12    # 读取图像
13    image = Image.open(image_path).convert('RGB')
14    # 解析XML文件
15    tree = ET.parse(annotation_path)
16    root = tree.getroot()
17    objects = []
18    for obj in root.findall('object'):
19        name = obj.find('name').text
20        bbox = obj.find('bndbox')
21        xmin = int(bbox.find('xmin').text)
22        ymin = int(bbox.find('ymin').text)
23        xmax = int(bbox.find('xmax').text)
24        ymax = int(bbox.find('ymax').text)
25        objects.append([xmin, ymin, xmax, ymax, name])
26    return image, objects
27
28# 展示图像
29def show_image_with_boxes(image, boxes):
30    img = np.array(image)
31    for box in boxes:
32        xmin, ymin, xmax, ymax, name = box
33        cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2)
34        cv2.putText(img, name, (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
35    cv2.imshow('Image with Boxes', img)
36    cv2.waitKey(0)
37    cv2.destroyAllWindows()
38
39# 主函数
40if __name__ == "__main__":
41    images_dir = os.path.join(dataset_path, 'JPEGImages')
42    annotations_dir = os.path.join(dataset_path, 'Annotations')
43    
44    # 获取图像列表
45    image_files = [f for f in os.listdir(images_dir) if f.endswith('.jpg')]
46    
47    # 随机选择一张图像
48    selected_image = np.random.choice(image_files)
49    image_path = os.path.join(images_dir, selected_image)
50    annotation_path = os.path.join(annotations_dir, selected_image.replace('.jpg', '.xml'))
51    
52    # 加载图像和标签
53    image, boxes = load_image_and_label(image_path, annotation_path)
54    
55    # 展示带有标注框的图像
56    show_image_with_boxes(image, boxes)

这段代码展示了如何加载图像和其对应的VOC XML标注文件,并在图像上绘制边界框和类别标签。您可以根据实际需求进一步扩展和修改这段代码,以适应您的具体应用场景。

示例代码:从VOC格式转换为YOLO格式

以下是将VOC格式的标注文件转换为YOLO格式的标签文件的示例代码:

 

python

深色版本

1import os
2import xml.etree.ElementTree as ET
3import shutil
4
5# 数据集路径
6dataset_path = 'path/to/pedestrian_knife_detection_dataset/'
7
8# VOC标注文件目录
9voc_annotations_dir = os.path.join(dataset_path, 'Annotations')
10# 输出YOLO标签文件目录
11yolo_labels_dir = os.path.join(dataset_path, 'labels')
12
13# 创建YOLO标签目录
14os.makedirs(yolo_labels_dir, exist_ok=True)
15
16# 复制VOC图像集划分文件到YOLO目录
17for split in ['train', 'val']:
18    shutil.copy(os.path.join(dataset_path, 'ImageSets/Main/{}.txt'.format(split)), os.path.join(yolo_labels_dir, '{}.txt'.format(split)))
19
20# 从VOC格式转换为YOLO格式
21def convert_voc_to_yolo(voc_path, yolo_path, width, height):
22    with open(voc_path, 'r') as infile:
23        tree = ET.parse(infile)
24        root = tree.getroot()
25        objects = []
26        for obj in root.findall('object'):
27            name = obj.find('name').text
28            bbox = obj.find('bndbox')
29            xmin = int(bbox.find('xmin').text)
30            ymin = int(bbox.find('ymin').text)
31            xmax = int(bbox.find('xmax').text)
32            ymax = int(bbox.find('ymax').text)
33            x_center = (xmin + xmax) / 2.0
34            y_center = (ymin + ymax) / 2.0
35            w = xmax - xmin
36            h = ymax - ymin
37            x_center /= width
38            y_center /= height
39            w /= width
40            h /= height
41            objects.append([name, x_center, y_center, w, h])
42
43    with open(yolo_path, 'w') as outfile:
44        for obj in objects:
45            class_index = {'person_with_knife': 0}[obj[0]]  # 假设只有一个类别
46            line = f"{class_index} {obj[1]} {obj[2]} {obj[3]} {obj[4]}\n"
47            outfile.write(line)
48
49# 主函数
50if __name__ == "__main__":
51    # 获取VOC标注文件列表
52    voc_files = [f for f in os.listdir(voc_annotations_dir) if f.endswith('.xml')]
53    
54    # 遍历VOC文件并转换为YOLO格式
55    for voc_file in voc_files:
56        # 获取图像尺寸
57        image_file = os.path.join(dataset_path, 'JPEGImages', voc_file.replace('.xml', '.jpg'))
58        image = Image.open(image_file)
59        width, height = image.size
60        
61        # 转换并保存YOLO标签文件
62        yolo_file = os.path.join(yolo_labels_dir, voc_file.replace('.xml', '.txt'))
63        convert_voc_to_yolo(os.path.join(voc_annotations_dir, voc_file), yolo_file, width, height)

这段代码展示了如何将VOC格式的标注文件转换为YOLO格式的标签文件,方便使用YOLO系列模型进行训练。您可以根据实际需求进一步扩展和修改这段代码,以适应您的具体应用场景。如果您的数据集中有多个类别,请调整 class_index 字典中的映射关系。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值