# How to import annotations for an Object Detection task

Imagine having an Object Detection dataset with the annotations in the COCO format. How would you import annotations in such a case? Well, you can simply follow the steps mentioned on the Import annotations (Beta) page. Let's see how it works.

First, you need to create your Hasty project and upload images to it. These steps are straightforward and should not cause any trouble.

Second, you must create the label classes and attributes you will use in your project. Simply follow the guide from the documentation, and it will take you seconds to do.

Third, export your project in the Hasty JSON v1.1 format through the Export data feature.

In the ZIP archive, there will be a JSON file containing the information about your project.

Since there are no annotations in your project yet, the list accessible by the 'labels' key for each image will be empty.

Please note that in Hasty JSON v1.1 format, all the information about images is stored as a Python list under the 'images' key. Also, images' data is represented in the form of a Python dictionary. So, annotations should be stored under the 'labels' key for each image. Let's expand our JSON file and add annotations to the corresponding images.

As mentioned above, your initial labels are in the COCO format, so it will require some coding to add the annotations to Hasty’s JSON file. However, if you are familiar with Python, it will not be a problem for you. Otherwise, please check the example script for converting COCO Object Detection annotations to Hasty ones below.

import json

path_to_initial_labels = 'path/to/the/file/with/initial/labels' # should be in COCO format
path_to_hasty_file = 'path/to/the/exported/file/' # this one will be expanded

with open(path_to_initial_labels, 'r') as file: # reading the file with initial labels

with open(path_to_hasty_file, 'r') as file: # reading the file that will be expanded

def return_class(idx): # this function returns a class label based on the index from COCO annotations
if idx == 1:
return 'Class 1'
if idx == 2:
return 'Class 2'
if idx == 3:
return 'Class 3'
...

def cocobbox2hastybbox(x, y, w, h): # COCO bbox format is [X_top_left, Y_top_left, Bbox_width, Bbox_height]
x1, y1 = x, y
x2, y2 = x + w, y + h
return [x1, y1, x2, y2] # Hasty bbox format should be [X_top_left, Y_top_left, X_bottom_right, Y_bottom_right]

def check_an_upload(initial_labels, name): # checking if the image is uploaded to Hasty (some images might fail to upload)
for j in initial_labels:
if name == j['file_name']:
return j['id']
return -1

for i in hasty_file['images']: # going through the images

if  idx == -1: # if the image is not uploaded move on to the next one
continue

for j in initial_labels['annotations']: # expanding the Hasty file by adding annotations to the corresponding images
if z['image_id'] - 1 == idx:
break
if z['image_id'] == idx:
i['labels'].append({'class_name': return_class(z['category_id']),
'bbox': yolobbox2bbox(z['bbox'][0], z['bbox'][1], z['bbox'][2], z['bbox'][3])}.copy())

with open(path_to_hasty_file, "w") as out_file: # saving the expanded file
json.dump(hasty_file, out_file)

Now you have a JSON file in the Hasty supported format that contains all the annotations. To ensure that your file is ready to be imported, double-check whether it corresponds to JSON format. There are a lot of free online checkers that are up to the task, so you will quickly get the answer. If there is some error, return to your code and search for the mistake that messes up the JSON format.

The last step is to upload the expanded JSON file to the Hasty project. At this point, you have already done the most challenging part, so you can relax and enjoy the growing download and parsing percentage. If your import is successful, you will see the following picture.

Excellent, the annotations are imported, and you can start analyzing them with Hasty capabilities, for example, assure the quality of these labels with AI Consensus Scoring. Hopefully, this example and general import workflow will help you to import annotations to a project successfully.

Last updated on Jun 01, 2022

## Get AI confident. Start using Hasty today.

Automate 90% of the work, reduce your time to deployment by 40%, and replace your whole ML software stack with our platform.