使用FiftyOne把coco导出为yolo格式
| |
---|
AI学习交流qq群 | 873673497 |
官网 | turingevo.com |
邮箱 | wmx@turingevo.com |
github | https://github.com/turingevo |
huggingface | https://huggingface.co/turingevo |
import fiftyone as fo
import fiftyone.zoo as foz
import yaml
import os
image_path = '/media/wmx/ws3/AI/data/coco2017/train2017'
annotations_path = '/media/wmx/ws3/AI/data/coco2017/annotations_trainval2017/annotations/instances_train2017.json'
dataset = fo.Dataset.from_dir(
dataset_type=fo.types.COCODetectionDataset,
data_path=image_path,
labels_path=annotations_path,
max_samples=200,
name="coco2017"
)
filter_names = [
"airplane", "apple", "backpack", "banana", "baseball bat", "baseball glove",
"bear", "bed", "bench", "bicycle", "bird", "boat", "book", "bottle", "bowl",
"broccoli", "bus", "cake", "car", "carrot", "cat", "cell phone", "chair",
"clock", "couch", "cow", "cup", "dining table", "dog", "donut", "elephant",
"fire hydrant", "fork", "frisbee", "giraffe", "hair drier", "handbag", "horse",
"hot dog", "keyboard", "kite", "knife", "laptop", "microwave", "motorcycle",
"mouse", "orange", "oven", "parking meter", "person", "pizza", "potted plant",
"refrigerator", "remote", "sandwich", "scissors", "sheep", "sink", "skateboard",
"skis", "snowboard", "spoon", "sports ball", "stop sign", "suitcase", "surfboard",
"teddy bear", "tennis racket", "tie", "toaster", "toilet", "toothbrush",
"traffic light", "train", "truck", "tv", "umbrella", "vase", "wine glass", "zebra"
]
filtered_dataset = dataset.filter_labels("detections", fo.ViewField("label").is_in(filter_names))
random_subset = filtered_dataset.take(100)
export_dir = '/media/wmx/ws3/AI/data/51/coco-yolo2017'
try:
random_subset.export(
export_dir="/media/wmx/ws3/AI/data/51/coco-yolo2017",
dataset_type=fo.types.YOLOv5Dataset,
label_field="ground_truth"
)
except ValueError as e:
print(f"Export failed: {e}. Please check the 'names' field in your dataset configuration.")
print(f"数据已成功导出到 {export_dir}")
yaml_file_path = os.path.join(export_dir, 'dataset.yaml')
yaml_content = {
'names': filter_names,
'path': export_dir,
'train': './images/train/'
}
with open(yaml_file_path, 'w') as yaml_file:
yaml.dump(yaml_content, yaml_file)
print(f"yaml 文件已成功创建/更新到 {yaml_file_path}")
session = fo.launch_app(random_subset, port=5151)
session.wait()