{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# How to Load and Visualize\n\nThis notebook contains a python code on how to load and visualize the data. We will read the .json file, make a bounding box, and draw it into the image so we can understand how the data works.","metadata":{}},{"cell_type":"code","source":"import json\nimport os\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport shutil\n\nimport random","metadata":{"execution":{"iopub.status.busy":"2023-04-06T09:11:39.035737Z","iopub.execute_input":"2023-04-06T09:11:39.036128Z","iopub.status.idle":"2023-04-06T09:11:39.041336Z","shell.execute_reply.started":"2023-04-06T09:11:39.036088Z","shell.execute_reply":"2023-04-06T09:11:39.040230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Read the .json file\n\nTo read the .json file, we can make a **load_annotation** function here that return json as a dictionary.","metadata":{}},{"cell_type":"code","source":"def load_annotation(json_file):\n    with open(json_file, 'r') as f:\n        annotation = json.load(f)\n    return annotation","metadata":{"execution":{"iopub.status.busy":"2023-04-06T09:11:39.043227Z","iopub.execute_input":"2023-04-06T09:11:39.043597Z","iopub.status.idle":"2023-04-06T09:11:39.055280Z","shell.execute_reply.started":"2023-04-06T09:11:39.043553Z","shell.execute_reply":"2023-04-06T09:11:39.054245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Plot the image\n\nTo show the picture, we will first load the picture and attempt to create a rectangular frame around it. The rectangular frame is created using the polygon coordinates in the provided .json file.","metadata":{}},{"cell_type":"code","source":"def plot_image_with_annotations(image_path, annotation):\n    # Load image\n    img = cv2.imread(image_path)\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    # Plot the annotations\n    for text_item in annotation['text']:\n        polygon = text_item['polygon']\n        text = text_item['text']\n        role = text_item['role']\n\n        # Create a bounding box rectangle using the polygon coordinates\n        x, y, w, h = polygon['x0'], polygon['y0'], polygon['x1'] - polygon['x0'], polygon['y2'] - polygon['y0']\n        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 1)\n\n        # Add the text label to the plot\n        cv2.putText(img, f'{text} ({role})', (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (255, 0, 0), 1)\n\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T09:18:08.201296Z","iopub.execute_input":"2023-04-06T09:18:08.201923Z","iopub.status.idle":"2023-04-06T09:18:08.209775Z","shell.execute_reply.started":"2023-04-06T09:18:08.201885Z","shell.execute_reply":"2023-04-06T09:18:08.208618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Put the images and annotations into a list","metadata":{}},{"cell_type":"code","source":"# Set paths\nroot_dir = '/kaggle/input/benetech-making-graphs-accessible'\ntrain_folder = os.path.join(root_dir, 'train')\nannotations_folder = os.path.join(train_folder, 'annotations')\nimages_folder = os.path.join(train_folder, 'images')\n\n# Get all the images name\nimages = sorted([os.path.join(images_folder, f) for f in os.listdir(images_folder)])\nannotations = sorted([os.path.join(annotations_folder, f) for f in os.listdir(annotations_folder)])\n\n# Choose an image and its corresponding annotation\nchose = 2\nimage_chosen = images[chose]\nannotation_chosen = annotations[chose]\n\n# Load and plot the image with annotations\nannotation_data = load_annotation(annotation_chosen)\nplot_image_with_annotations(image_chosen, annotation_data)","metadata":{"execution":{"iopub.status.busy":"2023-04-06T09:28:26.180243Z","iopub.execute_input":"2023-04-06T09:28:26.181252Z","iopub.status.idle":"2023-04-06T09:28:26.709218Z","shell.execute_reply.started":"2023-04-06T09:28:26.181195Z","shell.execute_reply":"2023-04-06T09:28:26.708212Z"},"trusted":true},"execution_count":null,"outputs":[]}]}