NVlabs / mask-auto-labeler

Other
160 stars 13 forks source link

No boxes_train2017.json file in COCO dataset #13

Closed eyalbetzalel closed 1 year ago

eyalbetzalel commented 1 year ago

I write a script that creates it from instance_train/val2017.json :


import json
import argparse
from tqdm import tqdm

def convert_coco_to_boxes(coco_annotations_file, output_file):
    # Load COCO annotations file
    with open(coco_annotations_file, 'r') as f:
        coco_annotations = json.load(f)

    # Create empty list to store box annotations
    box_annotations = []

    # Loop through each image in the dataset
    for image in tqdm(coco_annotations['images'], desc='Processing images'):
        # Loop through each annotation for this image
        for annotation in coco_annotations['annotations']:
            # If the annotation is for this image
            if annotation['image_id'] == image['id']:
                # Get the x, y, width, and height of the bounding box
                x, y, w, h = annotation['bbox']
                # Create a box annotation dictionary
                box_annotation = {
                    'image_id': image['id'],
                    'category_id': annotation['category_id'],
                    'bbox': [int(x), int(y), int(w), int(h)]
                }
                # Append the box annotation to the list
                box_annotations.append(box_annotation)

    # Save the box annotations to the output JSON file
    with open(output_file, 'w') as f:
        json.dump(box_annotations, f)

if __name__ == '__main__':
    # Parse command-line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--coco_annotations_file', required=True,
                        help='Path to COCO annotations file')
    parser.add_argument('--output_file', required=True,
                        help='Path to output JSON file')
    args = parser.parse_args()

    # Convert COCO annotations to box annotations
    convert_coco_to_boxes(args.coco_annotations_file, args.output_file)

Run this from the command line with :

nohup python script.py --coco_annotations_file /path/to/coco/annotations.json --output_file /path/to/output/annotations.json &
voidrank commented 1 year ago

Hi @eyalbetzalel

Use instances_train2017.json instead.

liusurufeng commented 1 year ago

@voidrank My annotation files instances_train2017.json/instances_val2017.json contain segmentation information. Now, I want to perform weak segmentation using only the bounding box informations in the annotations. Will using the instances_train2017.json/instances_val2017.json directly have an impact on the results?

voidrank commented 1 year ago

Hi @liusurufeng

Your pipeline looks good to me.