灭火器目标检测数据集 3700张 灭火器 带标注 voc yolo
灭火器目标检测数据集介绍
数据集概览
这是一个针对灭火器目标检测的数据集,包含3700张图片,每张图片都使用VOC或YOLO格式进行了标注。该数据集可用于训练和评估目标检测模型,特别是YOLO系列模型。
数据集特点
- 规模适中: 包含3700张图片,足够训练和评估YOLO模型。
- 单类别标注: 只有一个类别——灭火器。
- VOC/YOLO格式: 标注信息采用VOC或YOLO格式,适合用于目标检测模型。
数据集结构
数据集文件夹结构如下:
fire_extinguisher_dataset/
├── images/
│ ├── train/
│ ├── val/
│ └── test/
├── annotations/
│ ├── train/
│ ├── val/
│ └── test/
└── data.yaml
images/
目录下存放图片文件。annotations/
目录下存放对应的VOC或YOLO格式的标注文件。data.yaml
文件定义了数据集的配置信息。
data.yaml
文件示例(YOLO格式)
# data.yaml
train: ./images/train
val: ./images/val
test: ./images/test
nc: 1 # 类别数量
names: ['Fire Extinguisher']
关键代码示例
以下是一段Python代码示例,展示如何加载和查看数据集中的部分图片及其标注:
import os
import cv2
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
def load_image_and_label(image_path, label_path):
"""
加载图片和其对应的标注信息。
Parameters:
image_path (str): 图片路径。
label_path (str): 标注文件路径。
Returns:
img (numpy.ndarray): 加载的图片。
labels (list of dict): 标注信息列表。
"""
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 转换颜色空间
if label_path.endswith('.xml'): # VOC格式
from xml.etree.ElementTree import ElementTree
tree = ElementTree(file=label_path)
root = tree.getroot()
bboxes = []
for obj in root.findall('object'):
bbox = obj.find('bndbox')
xmin = float(bbox.find('xmin').text)
ymin = float(bbox.find('ymin').text)
xmax = float(bbox.find('xmax').text)
ymax = float(bbox.find('ymax').text)
bboxes.append({
'class_id': 0, # 因为只有一个类别,所以类别ID固定为0
'x_center': (xmin + xmax) / 2 / img.shape[1],
'y_center': (ymin + ymax) / 2 / img.shape[0],
'width': (xmax - xmin) / img.shape[1],
'height': (ymax - ymin) / img.shape[0]
})
labels = [dict(b) for b in bboxes]
elif label_path.endswith('.txt'): # YOLO格式
with open(label_path, 'r') as f:
lines = f.readlines()
labels = []
for line in lines:
class_id, x_center, y_center, width, height = map(float, line.strip().split())
labels.append({
'class_id': int(class_id),
'x_center': x_center,
'y_center': y_center,
'width': width,
'height': height
})
return img, labels
def draw_bounding_boxes(img, labels, class_names):
"""
在图片上绘制边界框。
Parameters:
img (numpy.ndarray): 图片。
labels (list of dict): 标注信息列表。
class_names (list of str): 类别名称列表。
"""
fig, ax = plt.subplots(1, figsize=(12, 12))
ax.imshow(img)
for label in labels:
x_center, y_center, width, height = label['x_center'], label['y_center'], label['width'], label['height']
h, w, _ = img.shape
x_min = int((x_center - width / 2) * w)
y_min = int((y_center - height / 2) * h)
x_max = int((x_center + width / 2) * w)
y_max = int((y_center + height / 2) * h)
rect = Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, linewidth=2, edgecolor='r', facecolor='none')
ax.add_patch(rect)
class_name = class_names[label['class_id']] # YOLO的类别ID从0开始
ax.text(x_min, y_min, class_name, fontsize=12, color='red', backgroundcolor='white')
plt.axis('off')
plt.show()
if __name__ == "__main__":
data_dir = "/path/to/your/dataset"
train_images_dir = os.path.join(data_dir, "images/train")
train_labels_dir = os.path.join(data_dir, "annotations/train")
# 选择任何一张训练集中的图片及其对应的标注信息
image_name = os.listdir(train_images_dir)[0] # 选择第一张图片
label_name = image_name.replace(".jpg", ".txt") # 假设图片是.jpg格式,标签文件名相同但扩展名为.txt
image_path = os.path.join(train_images_dir, image_name)
label_path = os.path.join(train_labels_dir, label_name)
img, labels = load_image_and_label(image_path, label_path)
print(f"Loaded image shape: {img.shape}")
print(f"Number of objects: {len(labels)}")
print(f"First object's label: {labels[0]}")
# 绘制边界框
class_names = ['Fire Extinguisher']
draw_bounding_boxes(img, labels, class_names)
请确保替换"/path/to/your/dataset"
为你本地数据集的实际路径。这段代码会加载训练集中的一张图片及其对应的标注信息,并显示带有边界框和类别的图片。
注意:上述代码仅支持YOLO和VOC两种标注格式之一。如果你的数据集同时存在这两种格式,你需要根据实际情况修改代码来处理不同格式的标注文件。