Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Convert Cityscapes to COCO format: How to convert to other classes (ex: traffic light) #1033

Open
MarceloContreras opened this issue Oct 25, 2022 · 1 comment

Comments

@MarceloContreras
Copy link

Has somebody successfully converted cityscapes dataset filtering classes such as traffic lights, poles, etc? Indeed, I did the change of desired classes in category_instancesonly and also commented on the invalid contour warning. However, even if I did that, I still get .json output files with 0 annotations after loading all the images.

@Aryan-Mishra24
Copy link

To successfully convert the Cityscapes dataset while filtering specific classes such as traffic lights and poles, you can follow the steps below:

First, clone the Cityscapes scripts repository:
bash

git clone https://github.com/mcordts/cityscapesScripts.git
cd cityscapesScripts/cityscapesscripts/preparation
Modify the createTrainIdInstanceImgs.py script to filter out specific classes. In the createTrainIdInstanceImgs.py file, find the following lines:
python

Load the annotation

objects = csHelpers.loadAnnotaton(labelImgPath)
Add a filtering function after the lines mentioned above:
python

def filter_objects(objects, classes_to_keep):
filtered_objects = []
for obj in objects:
if obj.label.name in classes_to_keep:
filtered_objects.append(obj)
return filtered_objects

classes_to_keep = ['traffic light', 'pole']
objects = filter_objects(objects, classes_to_keep)
Replace the classes_to_keep list with the classes you want to keep in the final annotations.

In the json2instanceImg.py script, find the following line:
python

if not anno['regions']:
Replace the above line with the following:
python

if len(anno['regions']) == 0:
Now you can run the createTrainIdInstanceImgs.py script to generate the filtered dataset:
bash

python createTrainIdInstanceImgs.py --cityscapesPath path/to/cityscapes --outputPath path/to/output
Finally, run the json2instanceImg.py script to convert the modified annotations to JSON files:
bash
python json2instanceImg.py --cityscapesPath path/to/cityscapes --outputPath path/to/output
After following these steps, the output JSON files should contain only the filtered classes (e.g., traffic lights and poles). Make sure to replace path/to/cityscapes with the path to your Cityscapes dataset and path/to/output with your desired output path.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants