exdark数据集转yolo格式(仅供参考)

# author: wujiahao
# last modified: 2021/11/5
#       exdark has some .JPEG or .JPG or .jpg or .png image ,but the responding ann txt is sometimes not correct in name
#       so we should convert the image name first to unified .png, and also convert the txt name
#       and select low light image w.r.t. imagecalsslist.txt 
#       and convert annotation format

import os
import cv2
import shutil
import glob

data_root = '/data1/wjh/exdark' # anndir  imgdir
dst_path = '/data1/wjh/exdark/exdark_vocformat2'
annfold = 'anndir'
imgfold = 'imgdir'
txtfile = 'imageclasslist.txt'
class_folders = ['Bicycle', 'Boat', 'Bottle', 'Bus', 'Car', 'Cat', 'Chair', 'Cup', 'Dog', 'Motorbike', 'People', 'Table']
class_idx_dict = {
    'Bicycle':1,
    'Boat':3,
    'Bottle':4,
    'Bus':5,
    'Car':6,
    'Cat':7,
    'Chair':8,
    'Table':10,
    'Dog':11,
    'Motorbike':13,
    'People':14,
}
ignore_class = 'Cup'

with open(txtfile, 'r') as ftxt:
    lines = ftxt.readlines()[1:]
for line in lines:  # each img 
    words = line.split()
    img = words[0]
    class_folder = class_folders[int(words[1])-1]
    file_prefix = img.split('.')[0]
    # copy img and change suffix
    img_file_path = glob.glob(os.path.join(data_root, imgfold, class_folder, file_prefix+'*'))[0]
    file_suffix = img_file_path.split('.')[-1]
    h_img, w_img, c_img = cv2.imread(img_file_path).shape
    if file_suffix in ['jpg', 'JPG', 'JPEG']:
        shutil.copy(img_file_path, os.path.join(dst_path, file_prefix+'.jpg'))
    else:
        shutil.copy(img_file_path, os.path.join(dst_path, file_prefix+'.png'))
    # convert ann
    ann_file_path = glob.glob(os.path.join(data_root, annfold, class_folder, file_prefix+'*'))[0]
    with open(ann_file_path, 'r') as f_in:
        ann_lines = f_in.readlines()[1:]
    dst_file_path = os.path.join(dst_path, file_prefix + '.txt')
    with open(dst_file_path, 'w') as f_out:
        for ann_line in ann_lines:  # each obj in the img
            ann_words = ann_line.split()
            if ann_words[0] == ignore_class:
                continue
            class_idx = class_idx_dict[ann_words[0]]
            bbox = ann_words[1:5]
            l,t,w,h = [float(bbox[i]) for i in range(4)]
            x_center, y_center, w_bbox, h_bbox = (l+w/2)/w_img, (t+h/2)/h_img, w/w_img, h/h_img
            outline = f'{class_idx} {x_center} {y_center} {w_bbox} {h_bbox}\n'
            f_out.write(outline)
    print(img_file_path)
        

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 22
    评论
将COCO数据集换为YOLO格式需要进行以下步骤: 1. 下载COCO数据集:首先,你需要下载COCO数据集,包括图像和标注文件。你可以从COCO官方网站上下载。 2. 解析标注文件:COCO数据集的标注文件是JSON格式的。你可以使用Python中的json库来解析它们。标注文件中包含了每个图像的标注信息,包括类别、边界框的坐标等。 3. 换为YOLO格式:将COCO数据集的标注信息换为YOLO格式需要进行一些计算和换。首先,YOLO格式要求边界框坐标是相对于图像宽度和高度的比例值,而不是像素坐标。因此,你需要将COCO数据集中的像素坐标换为相对值。另外,YOLO格式要求每个标注行包含类别索引和边界框坐标,以空格分隔。 4. 保存为txt文件:最后,将换后的YOLO格式标注信息保存为txt文件。每个图像对应一个txt文件,文件名与图像文件名相同,只是扩展名不同。 下面是一个示例代码,演示了如何将COCO数据集换为YOLO格式: ```python import json # 读取COCO标注文件 with open('annotations.json', 'r') as f: annotations = json.load(f) # 换为YOLO格式 yolo_annotations = [] for annotation in annotations: image_id = annotation['image_id'] category_id = annotation['category_id'] bbox = annotation['bbox'] # 计算边界框相对于图像的比例值 x, y, width, height = bbox img_width, img_height = get_image_size(image_id) # 获取图像宽度和高度 x_rel = x / img_width y_rel = y / img_height width_rel = width / img_width height_rel = height / img_height # 将标注信息换为YOLO格式 yolo_annotation = f"{category_id} {x_rel} {y_rel} {width_rel} {height_rel}" yolo_annotations.append(yolo_annotation) # 保存为txt文件 with open('annotations.txt', 'w') as f: for annotation in yolo_annotations: f.write(annotation + '\n') ``` 请注意,上述代码仅是一个示例,你需要根据你的实际情况进行适当修改和调整。另外,你可能还需要编写一些辅助函数来获取图像的宽度和高度等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值