BDD100k转coco格式(目标检测)

这篇文章看到有转换的python代码,但是直接使用会报错:

10%|████████████████                                                                                                                                                   | 6879/69863 [00:00<00:06, 9376.28it/s]
Traceback (most recent call last):
  File "bdd2coco.py", line 112, in <module>
    bdd2coco_detection(attr_id_dict, train_labels, out_fn)
  File "bdd2coco.py", line 45, in bdd2coco_detection
    for label in i['labels']:
KeyError: 'labels'

根据报错信息,可知这是字典的键错误,可能是键值不匹配,输出处理的文件名,发现到 11ecaf4a-837e3550.jpg 就会引起错误,打开json原文件,定位这个文件名的位置,相比于其他的图片,没有了'label'等部分。

{
    "name": "11ecaf4a-837e3550.jpg",
    "attributes": {
      "weather": "clear",
      "timeofday": "night",
      "scene": "highway"
    },
    "timestamp": 10000
},

解决方案

在下载的代码中添加这样两行代码即可(我的思路是直接跳过处理这张图片,相当于舍弃了):
在这里插入图片描述

if 'labels' not in i.keys():
	continue

补充

1. BDD100k类别

根据官方文档,需要将类别和对应id更新为:

1: pedestrian
2: rider
3: car
4: truck
5: bus
6: train
7: motorcycle
8: bicycle
9: traffic light
10: traffic sign

相应的代码修改为:

attr_dict["categories"] = [
	{"supercategory": "none", "id": 1, "name": "pedestrian"},
    {"supercategory": "none", "id": 2, "name": "rider"},
    {"supercategory": "none", "id": 3, "name": "car"},
    {"supercategory": "none", "id": 4, "name": "truck"},
    {"supercategory": "none", "id": 5, "name": "bus"},
    {"supercategory": "none", "id": 6, "name": "train"},
    {"supercategory": "none", "id": 7, "name": "motorcycle"},
    {"supercategory": "none", "id": 8, "name": "bicycle"},
    {"supercategory": "none", "id": 9, "name": "traffic light"},
    {"supercategory": "none", "id": 10, "name": "traffic sign"}
]
2. bdd100k_labels_images_train.json

这个文件就是官网下载的bdd100k_det_20_labels_trainval.zip解压后的train.json,我下载解压以后是det_train.jsondet_val.json,代码中的文件目录替换为自己本地这两个文件的目录即可。
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值