<jupyter_start><jupyter_text>Generate Annotations for DatasetsObjective: Use the Rendered.ai SDK to generate dataset annotations locally.Reference the latest SDK Documentation at https://sdk.rendered.ai  Two types of AnnotationsRendered.ai supports creation of two types of annotations:1. Conversion of annotations from our Platform format to other popularly recognized formats, such as COCO, PASCAL, or KITTI. These type of annotations can get generated via the SDK or via the Platform UI.2. Draw annotations, which include 2d bounding box, 3d bounding box, and segmentation drawings. These type of annotations are currently only able to be generated locally via the SDK.This tutorial will guide you through generating both of these types of annotations. Note that you must have a dataset from Rendered.ai downloaded to your local. For all the annotation examples here, we are using the dataset from [getting started section's dataset folder](../1_getting_started/6e396ea6-e394-4566-8259-76bcadc16ab7).   Get the latest `anatools`<jupyter_code>pip install anatools --upgrade<jupyter_output><empty_output><jupyter_text>Set up imports and log into the SDKThese imports are required for this Notebook to run. Enter your credentials at the prompts. Logging in successfully will list the organizations and workspaces you have access to.<jupyter_code>import os
import glob
import yaml, json
import wget
import time
from zipfile import ZipFile
from matplotlib import pyplot as plt
import numpy as np
from PIL import Image
import pprint 
import anatools

pp = pprint.PrettyPrinter(indent=4)
sdk = anatools.client()<jupyter_output>Enter your credentials for the Rendered.ai Platform.
Email: <EMAIL>
········
These are your organizations and workspaces:
    default Organization                          e8af8e7e-60d3-415f-a88d-9f4e191afec4              
	Example                                   c721696e-648f-4572-a9ca-5dd72f1e8f58              
The current workspaces is: c721696e-648f-4572-a9ca-5dd72f1e8f58<jupyter_text>Convert Annotations to a Problem Specific FormatRendered.ai synthetic datasets can be used with various CV model domains. The `annotations()` anatools conversion classes are used to convert the default annotations into common formats. The current formats supported are:- [COCO](https://cocodataset.org/home)- [PASCAL VOC](http://host.robots.ox.ac.uk/pascal/VOC/)- [KITTI](https://docs.nvidia.com/tao/tao-toolkit/text/data_annotation_format.htmllabel-files)Here, we will walk through how to generate COCO annotations. To get started on generating these types of annotations locally, first obtain a Channel's mappings file. For the Example Channel, this `default.yml` mappings file can be found under the `/mappings` folder.<jupyter_code># Download the dataset and the default object mapping files
# Other availailable mappings can be used: https://github.com/Rendered-ai/ana/tree/main/ana/channels/example/mappings
mappings_file_name = 'default.yml'
if os.path.isfile(mappings_file_name):
    print("Found mapping file " + mappings_file_name)
else:
    download_filename = wget.download('https://raw.githubusercontent.com/Rendered-ai/ana/main/ana/channels/example/mappings/default.yml')
    print("Downloaded " + download_filename)

# Define dataDir here, default is the datasets in the Getting Started Section
data_dir = "../1_getting_started/6e396ea6-e394-4566-8259-76bcadc16ab7"
    
#Create the needed annotations directory
annotation_dir = os.path.join(data_dir, 'annotation_labels')
if not os.path.isdir(annotation_dir):
    os.mkdir(annotation_dir)<jupyter_output>Found mapping file default.yml<jupyter_text>Next, generate the COCO annotations with `annotations().dump_coco()`. Some sample output of the COCO annotations for an Example channel dataset is below. It's quite a long annotation so it has been modified for brevity.<jupyter_code># Create the annotations
from anatools import annotations

annotations().dump_coco(data_dir, annotation_dir, mappings_file_name)
print("Created Annotation Labels")

# # View some kitti labels
filename = os.listdir(os.path.join(data_dir, 'annotation_labels'))[0]
filepath = os.path.join(data_dir, 'annotation_labels', filename)
print(filepath)
with open(filepath) as fin:
    for i in range(3):
        print(fin.readline())<jupyter_output>Created Annotation Labels
../1_getting_started/6e396ea6-e394-4566-8259-76bcadc16ab7/annotation_labels/coco.json
{"info": {"description": "Rendered.AI Synthetic Dataset", "url": "https://rendered.ai/", "contributor": "<EMAIL>", "version": "1.0", "year": "2022", "date_created": "2022-06-16T20:58:52.048879"}, "licenses": [{"id": 0, "url": "https://rendered.ai/", "name": "Rendered.AI License"}], "images": [{"id": 0, "file_name": "0000000000-250-RGBCamera.png", "date_captured": "2022-04-11T21:48:47.334134", "license": 0}], "categories": [{"id": 0, "name": "BubbleBottle", "supercategory": "none"}, {"id": 1, "name": "Playdough", "supercategory": "none"}, {"id": 2, "name": "YoYo", "supercategory": "none"}, {"id": 3, "name": "Skateboard", "supercategory": "none"}, {"id": 4, "name": "RubiksCube", "supercategory": "none"}], "annotations": [{"id": 0, "image_id": 0, "category_id": 0, "segmentation": [[179, 360, 178, 361, 177, 361, 176, 361, 175, 361, 174, 362, 173, 362, 172, 362, 171, 363,[...]<jupyter_text>Draw AnnotationsThere are three types of drawing annotations.- Draw 2d box- Draw 3d box- Draw segmentationThe 2d Example can be found in the [Generate Synthetic Data with the Example Channel Notebook](../1_getting_started/Generate%20Synthetic%20Data%20with%20the%20Example%20Channel.ipynb)  Create an image Annotated with 3d Bounding BoxesThis example will walk through how to generate images with 3d bounding boxes on your local system.Use the `bounding_box_3d` function to generate the annotated image from the single image file within the dataset. Here, we will choose to filter the drawings on the PlayDough type and set line thickness=2.<jupyter_code>from anatools import annotations

# Define dataDir here, default is the datasets in the Getting Started Section
data_dir = "../1_getting_started/6e396ea6-e394-4566-8259-76bcadc16ab7"

#Create the needed annotations directory
draw_dir = os.path.join(data_dir, 'draw_annotations')
if not os.path.isdir(draw_dir):
    os.mkdir(draw_dir)
    
annotations = annotations()
annotations.bounding_box_3d(image_path=glob.glob(data_dir+"/images/*.png")[0], 
                                out_dir=draw_dir, object_types=['PlayDough'], line_thickness=2)
image_file_name = glob.glob(draw_dir+"/*3d.png")[0].split('/')[4]
annotated_image_3d = Image.open(draw_dir+"/"+image_file_name)
plt.figure(figsize = (8,8))
plt.imshow(np.asarray(annotated_image_3d))<jupyter_output><empty_output><jupyter_text>Create an image Annotated with SegmentationThis example will walk through how to generate images with segmentation on your local system.Use the `segmentation` function to generate the annotated image from the single image file within the dataset. Here, we will choose to filter the drawings on the Skateboard type and set line thickness=2. Note that each skateboard in the resulting image has a green outline.<jupyter_code>from anatools import annotations

# Define data_dir here, default is the datasets in the Getting Started Section
data_dir = "../1_getting_started/6e396ea6-e394-4566-8259-76bcadc16ab7"


#Create the needed annotations directory
draw_dir = os.path.join(data_dir, 'draw_annotations')
if not os.path.isdir(draw_dir):
    os.mkdir(draw_dir)

annotations = annotations()
annotations.segmentation(image_path=glob.glob(data_dir+"/images/*.png")[0], 
                                out_dir=draw_dir, object_types=['Skateboard'], line_thickness=2)


image_file_name = glob.glob(draw_dir+"/*segmentation.png")[0].split('/')[4]
annotated_image_segmentation = Image.open(draw_dir+"/"+image_file_name)
plt.figure(figsize = (8,8))
plt.imshow(np.asarray(annotated_image_segmentation))<jupyter_output>Image saved to ../1_getting_started/6e396ea6-e394-4566-8259-76bcadc16ab7/draw_annotations/0000000000-250-RGBCamera-annotated-segmentation.png
{'fraction_non_alphanumeric': 0.10613353079106504, 'fraction_numerical': 0.04590892262125139, 'mean_word_length': 5.356078431372549, 'pattern_counts': {'":': 37, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 7, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '335084', 'license': 'Apache-2.0', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2464, 'n_words': 767, 'path': '3_anatools_for_ML/Generate Annotations for Datasets.ipynb', 'repo_name': 'Rendered-ai/resources', 'size': '8122'}