<jupyter_start><jupyter_text>Image tag parsing and annotation previewVersion 2021-01-04  Load libraries<jupyter_code># Library for data visualisation
# pip install opencv-python
from cv2 import cv2

# Built-in package for json parsing
import json

# Library for handling data paths
# pip install pathlib
import pathlib<jupyter_output><empty_output><jupyter_text>Select data locationSet *root_dir* to point to "Supervisely" project folder<jupyter_code>#Set path to root dir (wrist folder)
root_dir = pathlib.Path('Path_to_Supervisely_project_folder')

# Path to annotation dir
ann_dir = pathlib.Path(root_dir, 'ann')

# Path to image dir
img_dir = pathlib.Path(root_dir, 'img')

# Lists
list_files = lambda start_dir: [str(item) for item in start_dir.iterdir()]
ann_list = list_files(ann_dir)

print('Number of annotations: {}'.format(len(ann_list)))<jupyter_output><empty_output><jupyter_text>Functions used to parse and preview data<jupyter_code># Function that is parsing json file and printing all usefull data
def parse_json(path_to_annotation):
    #Load data
    with open(path_to_annotation) as json_file:  
        # Load json
        data = json.load(json_file)
        
        # Print tags:
        print('Tags:')
        for tag in data['tags']:
            print(tag)
            
        print('*******************')
        print('Objects:')
        #Print objects:
        for object_dict in data['objects']:
            print('Object type: {}\nObject points: {}'.format(object_dict['classTitle'], 
                                                                   object_dict['points']['exterior']))
            print('*******************')    
            

def display_annotations(path_to_annotation, image_dir, objectname, scale = 1):
    #Load data
    with open(path_to_annotation) as json_file:
        
        # Get name of the file
        name = pathlib.PurePath(path_to_annotation).name.split('.')[0]
        image = image_dir / (str(name) + '.png')

        # Load image
        original_image = cv2.imread(str(image))
        original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)
        annotated_image = original_image.copy()
        
        # Load json
        data = json.load(json_file)
        
        # Draw fractures
        for object_dict in data['objects']:
            # Check if object is fracture
            if object_dict['classTitle'] == objectname:
               
                # Get points and convert them to int for display purposes
                top_left_point, bottom_right_point = object_dict['points']['exterior']
                top_left_point = list(map(int,top_left_point))
                bottom_right_point = list(map(int, bottom_right_point))
                
                # Draw points
                cv2.rectangle(annotated_image, (top_left_point[0], top_left_point[1]), 
                              (bottom_right_point[0], bottom_right_point[1]), (0, 0, 255), 5)
        
        # Scale images for display purposes
        original_image = cv2.resize(original_image, (0,0), fx=scale, fy=scale) 
        annotated_image = cv2.resize(annotated_image, (0,0), fx=scale, fy=scale) 
        
        # Display images
        cv2.imshow('Original', original_image)
        cv2.imshow('Annotated', annotated_image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()<jupyter_output><empty_output><jupyter_text>Main functionSelect index name of the image/object that you want to preview.<jupyter_code># Select index of the image to preview
index = 0
objectname = 'fracture'

# To close images press any key
parse_json(ann_list[index])
display_annotations(ann_list[index], img_dir, objectname, scale = 0.75)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10591397849462365, 'fraction_numerical': 0.01129032258064516, 'mean_word_length': 2.8242548818088387, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '245201', 'license': 'MIT', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1095, 'n_words': 271, 'path': 'annotation_preview.ipynb', 'repo_name': 'tschauner-s/dataset_tools', 'size': '3720'}