{"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":"import json\nfrom pathlib import Path\nfrom PIL import Image\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-20T21:11:07.124938Z","iopub.execute_input":"2023-04-20T21:11:07.125558Z","iopub.status.idle":"2023-04-20T21:11:07.132796Z","shell.execute_reply.started":"2023-04-20T21:11:07.125497Z","shell.execute_reply":"2023-04-20T21:11:07.131503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations\")\nimages = Path(\"/kaggle/input/benetech-making-graphs-accessible/train/images\")\nall_annotations = list(annotations.iterdir())","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:11:07.135398Z","iopub.execute_input":"2023-04-20T21:11:07.136227Z","iopub.status.idle":"2023-04-20T21:11:07.461797Z","shell.execute_reply.started":"2023-04-20T21:11:07.136175Z","shell.execute_reply":"2023-04-20T21:11:07.460638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_with_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': \"blue\",\n        'chart_title':\"green\",\n        'legend_label':\"yellow\",\n        'other':\"grey\",\n        'tick_grouping':\"brown\",\n        'tick_label':\"purple\",\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-20T21:11:07.463107Z","iopub.execute_input":"2023-04-20T21:11:07.463454Z","iopub.status.idle":"2023-04-20T21:11:07.474185Z","shell.execute_reply.started":"2023-04-20T21:11:07.463419Z","shell.execute_reply":"2023-04-20T21:11:07.473293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nrows, ncols = 3, 3\nfig, ax = plt.subplots(nrows, ncols, figsize=(15, 10))\nfig.tight_layout()\nax = ax.flatten()\n\nidxs = np.random.randint(low=0, high=len(all_annotations), size=nrows*ncols)\n\nfor i, idx in enumerate(idxs):\n    plot_with_annotations(all_annotations[idx], ax[i])\n    \nplt.savefig(\"annotated_examples.pdf\", bbox_inches=\"tight\")","metadata":{"execution":{"iopub.status.busy":"2023-04-20T21:11:07.476295Z","iopub.execute_input":"2023-04-20T21:11:07.476649Z","iopub.status.idle":"2023-04-20T21:11:10.269710Z","shell.execute_reply.started":"2023-04-20T21:11:07.476617Z","shell.execute_reply":"2023-04-20T21:11:10.268853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}