voxel51 / fiftyone

Refine high-quality datasets and visual AI models
https://fiftyone.ai
Apache License 2.0
8.83k stars 557 forks source link

[BUG]why can't I open my own datasets in yolov8 format? #4691

Open Edward-YS opened 2 months ago

Edward-YS commented 2 months ago

Describe the problem

HELLO! I'm just a new guy to use the fiftyone which is a good tool. I want to open my own datasets in yolov8 format and see the marked rectangle box. It can add sample to my dataset but turn to be wrong when it opens the fiftyone website and report an error "No dataset selected"

Code to reproduce issue

import glob import os import fiftyone as fo from fiftyone import Sample, Detection, Dataset

图片和YOLO标注文件的路径

images_dir = r"D:/auto_Detection/鸟类检测/数据集/fiftyone下载10000/train/images" annotations_dir = r"D:/auto_Detection/鸟类检测/数据集/fiftyone下载10000/train/labels"

支持的类别

classes = ["bird"] # 根据你的数据集替换类别列表

获取所有图片路径

images_patt = os.path.join(images_dir, "*.jpg") # 假设图片是.jpg格式 image_paths = glob.glob(images_patt)

创建数据集

dataset_name = "bird_dataset" dataset = Dataset(name=dataset_name)

遍历所有图片

for image_path in image_paths:

生成YOLO标注文件的完整路径

annotation_path = os.path.join(annotations_dir, os.path.splitext(os.path.basename(image_path))[0] + ".txt")

# 初始化一个空的样本
sample = Sample(filepath=image_path)

# 读取YOLO标注文件
if os.path.isfile(annotation_path):
    with open(annotation_path, "r") as f:
        lines = f.readlines()
        detections = []
        for line in lines:
            # YOLO格式通常为 <class_index> <x> <y> <width> <height>
            parts = line.strip().split()
            if len(parts) < 5:
                continue
            class_index = int(parts[0])
            x, y, width, height = map(float, parts[1:5])
            # 转换为(x_min, y_min, x_max, y_max)格式
            x_min = x - width / 2
            y_min = y - height / 2
            x_max = x + width / 2
            y_max = y + height / 2
            label = classes[class_index]

            # 创建检测标注并添加到样本中
            detection = Detection(
                label=label,
                bounding_box=[x_min, y_min, x_max - x_min, y_max - y_min],
                confidence=1.0  # 假设分数为1.0
            )
            detections.append(detection)

# 将检测标注添加到样本
sample["ground_truth"] = fo.Detections(detections=detections)

# 将样本添加到数据集
dataset.add_sample(sample)

启动FiftyOne App并可视化标注的矩形框

session= fo.launch_app(dataset) session.wait()

![LFZH0)P897HT5F4KW}P(X$U](https://github.com/user-attachments/assets/8c55c177-ee98-4037-bc56-34a030b36376)

System information

Other info/logs

Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Please do not use screenshots for sharing text. Code snippets should be used instead when providing tracebacks, logs, etc.

Willingness to contribute

The FiftyOne Community encourages bug fix contributions. Would you or another member of your organization be willing to contribute a fix for this bug to the FiftyOne codebase?

benjaminpkane commented 2 months ago

Hi @Edward-YS 👋 This FAQ might help