{"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":"code","source":"'''\nDescription: Importing all python libraries\n'''\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport json\nfrom pathlib import Path\nfrom PIL import Image","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-22T11:09:55.366167Z","iopub.execute_input":"2023-04-22T11:09:55.366566Z","iopub.status.idle":"2023-04-22T11:09:55.373071Z","shell.execute_reply.started":"2023-04-22T11:09:55.366529Z","shell.execute_reply":"2023-04-22T11:09:55.371454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nDescription: Loading project data from train folder and storing all path in python list\n'''\nannote = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations\")\nimages = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/images\")\nannotations_list = list(annote.iterdir())\n\n#Checking annotation path for index 0\nprint(annotations_list [0])","metadata":{"execution":{"iopub.status.busy":"2023-04-22T11:11:26.679550Z","iopub.execute_input":"2023-04-22T11:11:26.680202Z","iopub.status.idle":"2023-04-22T11:11:26.837401Z","shell.execute_reply.started":"2023-04-22T11:11:26.680150Z","shell.execute_reply":"2023-04-22T11:11:26.836165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_annotations(filename, ax):\n    with open(filename, \"r\") as f:\n        row = pd.json_normalize(json.load(f)).iloc[0]\n        \n    img = Image.open(images / filename.with_suffix(\".jpg\").name)\n    \n    ax.imshow(img)\n    bbox = patches.Rectangle(\n        (row[\"plot-bb.x0\"], row[\"plot-bb.y0\"]), \n        row[\"plot-bb.width\"], \n        row[\"plot-bb.height\"], \n        linewidth=1, \n        edgecolor=\"r\", \n        facecolor=\"none\"\n    )\n    ax.add_patch(bbox)\n    \n    role2color = {\n        'axis_title': \"black\",\n        'chart_title':\"green\",\n        'legend_label':\"yellow\",\n        'other':\"grey\",\n        'tick_grouping':\"pink\",\n        'tick_label':\"green\",\n    }\n    \n    for text in row[\"text\"]:\n        polygon = text[\"polygon\"]\n        xs = np.array([v for k, v in polygon.items() if \"x\" in k])\n        ys = np.array([v for k, v in polygon.items() if \"y\" in k])\n        xy = np.vstack([xs, ys]).T\n        text_polygon = patches.Polygon(xy, linewidth=1, edgecolor=role2color[text[\"role\"]], facecolor=\"none\")\n        ax.add_patch(text_polygon)\n\n    \n    ax.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-04-22T11:11:43.178408Z","iopub.execute_input":"2023-04-22T11:11:43.179260Z","iopub.status.idle":"2023-04-22T11:11:43.189311Z","shell.execute_reply.started":"2023-04-22T11:11:43.179208Z","shell.execute_reply":"2023-04-22T11:11:43.188421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nDescription: Load and Visualize the Data\n'''\n\nnrows, ncols = 4, 2\nfig, ax = plt.subplots(nrows, ncols, figsize=(20, 15))\nfig.tight_layout()\nax = ax.flatten()\n\nidxs = np.random.randint(low=0, high=len(annotations_list), size=nrows*ncols)\n\nfor i, idx in enumerate(idxs):\n    plot_annotations(annotations_list [idx], ax[i])\n    \nplt.savefig(\"annotated_examples.pdf\", bbox_inches=\"tight\")","metadata":{"execution":{"iopub.status.busy":"2023-04-22T11:14:44.033117Z","iopub.execute_input":"2023-04-22T11:14:44.033522Z","iopub.status.idle":"2023-04-22T11:14:46.671402Z","shell.execute_reply.started":"2023-04-22T11:14:44.033485Z","shell.execute_reply":"2023-04-22T11:14:46.669954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}