text_plain_output_1.png
from PIL import Image, ImageDraw

from benetech_annotation_parser.annotation_api import AnnotationParser, Axis

from typing import Dict

import matplotlib.pyplot as plt

import random

train_dataset_path = '/kaggle/input/benetech-making-graphs-accessible/train'
annotation_parser = AnnotationParser(train_dataset_path)

p = annotation_parser.get_annotation(0)

from typing import Dict
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
def draw_rect_angle(img: Image.Image, rectangle_coord: Dict[str, int]):
    x0 = rectangle_coord['x0']
    y0 = rectangle_coord['y0']
    h = rectangle_coord['height']
    w = rectangle_coord['width']
    draw = ImageDraw.Draw(img)
    draw.rectangle([(x0, y0), (x0 + w, y0 + h)], outline='blue', width=0)
def draw_rect_angle_rotate(img: Image.Image, rectangle_coord: Dict[str, int], annotation_type: str):
    x0 = rectangle_coord['x0']
    x1 = rectangle_coord['x1']
    x2 = rectangle_coord['x2']
    x3 = rectangle_coord['x3']
    y0 = rectangle_coord['y0']
    y1 = rectangle_coord['y1']
    y2 = rectangle_coord['y2']
    y3 = rectangle_coord['y3']
    draw = ImageDraw.Draw(img)
    color = {'tick_label': (255, 0, 0), 'chart_title': (0, 192, 192), 'axis_title': (255, 255, 0)}[annotation_type]
    draw.line([x0, y0, x1, y1], fill=color, width=2)
    draw.line([x1, y1, x2, y2], fill=color, width=2)
    draw.line([x2, y2, x3, y3], fill=color, width=2)
    draw.line([x3, y3, x0, y0], fill=color, width=2)
def draw_point(img: Image.Image, coord: Dict[str, int]):
    draw = ImageDraw.Draw(img)
    draw.ellipse([(coord['x'] - 1, coord['y'] - 1), (coord['x'] + 1, coord['y'] + 1)], fill='lime', outline='lime', width=10)

def visualization_show(annotation_parser: AnnotationParser, index: int):
    ap = annotation_parser.get_annotation(index=index)
    img = Image.open(ap.image_path)
    polygon = ap.text(filter='polygon')
    role = ap.text(filter='role')
    ticks_x = ap.ticks(axis=Axis.X, filter='tick_pt')
    ticks_y = ap.ticks(axis=Axis.Y, filter='tick_pt')
    plt.axis('off')

import random
for i in range(5):
    random_index = random.randint(0, len(annotation_parser))
    visualization_show(annotation_parser, random_index)