Skip to content

Convert annotations from VIA to COCO.

License

Notifications You must be signed in to change notification settings

woctezuma/VIA2COCO

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 

Repository files navigation

VIA2COCO

This repository contains Python code to convert annotations from Oxford's VGG Image Annotator (VIA) to Microsoft's Common Objects in Context (COCO) format.

Installation

Usage

The simplest usage would be to specify a list of categories.

Caveat:

  • categories should have distinct names,
  • the order matters, as it will be used to number categories.
import convert as via2coco

input_dir = '/content/data/balloon/train/'
input_json = input_dir + 'via_region_data.json'
categories = ['balloon']

coco_dict = via2coco.convert(
    imgdir=input_dir,
    annpath=input_json,
    categories=categories,
)

By default, the first category is indexed as 1. This can be changed to 0 (or any non-negative integer) as follows:

import convert as via2coco

input_dir = '/content/data/balloon/train/'
input_json = input_dir + 'via_region_data.json'
categories = ['balloon']
first_class_index = 0

coco_dict = via2coco.convert(
    imgdir=input_dir,
    annpath=input_json,
    categories=categories,
    first_class_index=first_class_index,
)

To save the output as a JSON file:

import convert as via2coco

input_dir = '/content/data/balloon/train/'
input_json = input_dir + 'via_region_data.json'
categories = ['balloon']

output_json = input_dir + 'coco_train.json'

coco_dict = via2coco.convert(
    imgdir=input_dir,
    annpath=input_json,
    categories=categories,
    output_file_name=output_json,
)

It is also possible to specify a list of super-categories.

Caveat:

  • super-categories can have the same name,
  • the lists of categories and super-categories should have the same length,
  • the order matters, as it will be used to match categories and super-categories.
import convert as via2coco

input_dir = '/content/data/balloon/train/'
input_json = input_dir + 'via_region_data.json'
categories = ['balloon']

super_categories = ['N/A']

coco_dict = via2coco.convert(
    imgdir=input_dir,
    annpath=input_json,
    categories=categories,
    super_categories=super_categories,
)

References