BDD 100K数据集的json源文件批量转txt格式(YOLO格式/含代码)

想看懂代码的uu可以先了解一下yolo需要的txt数据格式是什么样的,还有BDD的json文件中的数据结构是什么样的。
想直接一键应用的看注释即可

import json
import os
import shutil
from pathlib import Path


Jsonpath = r'C:/Users/Xiaohu/Desktop/BDD100K/labels/det_val.json'
out_labels_path = r'C:/Users/Xiaohu/Desktop/BDD100K/labels/labels/val'

#labels normalized
#首先载入json文件
with open (Jsonpath,'r') as j:
    Metadata = json.load(j) 
    #这里读取每张图片对应的列表(BDD的图片以列表形式存储)
    for i,dict_pic in enumerate(Metadata): 
        category_value_list = []
        n = dict_pic['name']
        pic_name = Path(n).stem + '.txt'
        if 'labels' in dict_pic:
            list_labels = dict_pic['labels'] 
            for dict_label in list_labels:
                category_key = dict_label['category']
                category_value = ClassDict[category_key]
                category_value_list.append(category_value)
               #这里是把json里的数据正则化或者说归一化
                with open(os.path.join(out_labels_path, pic_name), 'a') as l:
                    w = 1280
                    h = 720
                    out_det = []
                    
                    dict_dimension = dict_label['box2d']
                    x1 = dict_dimension['x1']
                    y1 = dict_dimension['y1']
                    x2 = dict_dimension['x2']
                    y2 = dict_dimension['y2']
                    
                    xc = (x1 + x2) / (2* w)
                    yc = (y1 + y2) / (2 * h)
                    bw = (x2 - x1 )/ w
                    bh = (y2 - y1) / h
                    
                    out_det.append('{:.6f}'.format(xc))
                    out_det.append('{:.6f}'.format(yc))
                    out_det.append('{:.6f}'.format(bw))
                    out_det.append('{:.6f}'.format(bh))
                    
                   #逐行写入
                    line = str(category_value)
                    for coord in out_det:
                        line += ' ' + str(coord)
                    l.write(line)
                    l.write('\n')  
                    
             
  
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BDD100K是一个包车辆行驶场景的大型数据集,其中包括图像、视频和语义分割标注。如果你想将BDD100K数据集换为YOLO格式,可以按照以下步骤进行: 1. 下载BDD100K数据集并解压缩。 2. 安装Python3和OpenCV。 3. 创建一个新的文件夹,用于存储YOLO格式的标注文件和图像。 4. 在该文件夹中创建一个名为“classes.txt”的文件,并在其中列出数据集中所有可能的类别。 5. 打开终端或命令提示符窗口,导航到BDD100K数据集文件夹。 6. 运行以下Python脚本,将BDD100K标注换为YOLO格式: ```python import os import cv2 # Define the classes classes = ["car", "truck", "bus", "person", "bike", "motor", "traffic light", "traffic sign"] # Open classes file with open("classes.txt", "w") as f: for i, c in enumerate(classes): f.write(f"{c}\n") # Traverse the image directory and convert the annotations for dirpath, dirnames, filenames in os.walk("bdd100k/images/100k/train"): for filename in filenames: if filename.endswith(".jpg"): # Load the image img = cv2.imread(os.path.join(dirpath, filename)) # Load the annotation file anno_file = os.path.join("bdd100k/labels", dirpath.split("/")[-1], filename.replace(".jpg", ".txt")) with open(anno_file, "r") as f: annotations = f.readlines() # Create a new annotation file with open(os.path.join("yolo_labels", filename.replace(".jpg", ".txt")), "w") as f: for annotation in annotations: # Parse the annotation values = annotation.split(" ") x, y, w, h = [float(v) for v in values[2:6]] x_center = x + w / 2 y_center = y + h / 2 class_id = classes.index(values[0]) # Convert to YOLO format img_h, img_w, _ = img.shape x_center /= img_w y_center /= img_h w /= img_w h /= img_h f.write(f"{class_id} {x_center} {y_center} {w} {h}\n") ``` 7. 运行脚本后,YOLO格式的标注文件将存储在名为“yolo_labels”的文件夹中,并且每个图像都有一个相应的标注文件。 注意:这个示例只是一个基本的换脚本,可能需要根据实际情况进行更改。例如,如果您的数据集其他类别或具有不同的分辨率,则需要相应地更新代码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值