{"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":"# <span style=\"color:orange; text-decoration: underline;\"> About this notebook</span>\nThe annotations for this competition are somewhat complex, so I have developed a simple API to handle the data more easily.\nIn this notebook, I introduce how to use the API and showcase a practical examples using the API.\nThe code is also available on GitHub[[https://github.com/Kololu777/benetech-annotation-parser]](https://github.com/Kololu777/benetech-annotation-parser), so please refer to it as needed.","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:12:50.187456Z","iopub.execute_input":"2023-05-10T10:12:50.188000Z","iopub.status.idle":"2023-05-10T10:12:50.199031Z","shell.execute_reply.started":"2023-05-10T10:12:50.187935Z","shell.execute_reply":"2023-05-10T10:12:50.196014Z"}}},{"cell_type":"markdown","source":"# <span style=\"color:orange; text-decoration: underline;\"> Install</span>","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:02:18.863742Z","iopub.execute_input":"2023-05-10T10:02:18.864587Z","iopub.status.idle":"2023-05-10T10:02:18.870292Z","shell.execute_reply.started":"2023-05-10T10:02:18.864523Z","shell.execute_reply":"2023-05-10T10:02:18.868988Z"}}},{"cell_type":"code","source":"# api install \n!pip install benetech-annotation-parser\n","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:18:38.391251Z","iopub.execute_input":"2023-05-10T10:18:38.391643Z","iopub.status.idle":"2023-05-10T10:18:56.282946Z","shell.execute_reply.started":"2023-05-10T10:18:38.391613Z","shell.execute_reply":"2023-05-10T10:18:56.281067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:orange; text-decoration: underline;\"> how to use the API</span>","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:01:56.658712Z","iopub.execute_input":"2023-05-10T10:01:56.659144Z","iopub.status.idle":"2023-05-10T10:01:56.665846Z","shell.execute_reply.started":"2023-05-10T10:01:56.659112Z","shell.execute_reply":"2023-05-10T10:01:56.664004Z"}}},{"cell_type":"code","source":"from benetech_annotation_parser.annotation_api import AnnotationParser, Axis ","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:37.174641Z","iopub.execute_input":"2023-05-10T10:19:37.175164Z","iopub.status.idle":"2023-05-10T10:19:37.188697Z","shell.execute_reply.started":"2023-05-10T10:19:37.175129Z","shell.execute_reply":"2023-05-10T10:19:37.186639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset_path = \"/kaggle/input/benetech-making-graphs-accessible/train\"\nannotation_parser = AnnotationParser(train_dataset_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:38.290246Z","iopub.execute_input":"2023-05-10T10:19:38.290704Z","iopub.status.idle":"2023-05-10T10:19:40.103934Z","shell.execute_reply.started":"2023-05-10T10:19:38.290669Z","shell.execute_reply":"2023-05-10T10:19:40.102608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (0) \"Extract one JSON data from a list of JSON file paths.\"\np = annotation_parser.get_annotation(0)\nprint(p.name)\n\nprint(p.json_path)\n\nprint(p.image_path)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:40.662368Z","iopub.execute_input":"2023-05-10T10:19:40.664359Z","iopub.status.idle":"2023-05-10T10:19:40.679829Z","shell.execute_reply.started":"2023-05-10T10:19:40.664298Z","shell.execute_reply":"2023-05-10T10:19:40.678438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (1) source Literal[\"generated\", \"extracted\"]\nprint(p.source)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:41.219797Z","iopub.execute_input":"2023-05-10T10:19:41.220537Z","iopub.status.idle":"2023-05-10T10:19:41.227146Z","shell.execute_reply.started":"2023-05-10T10:19:41.220497Z","shell.execute_reply":"2023-05-10T10:19:41.225617Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (2) chart-type Literal[\"dot\", \"horaizontal_bar\", \"vertical_bar\", \"line\", \"scatter\"]\nprint(p.chart_type)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:41.612333Z","iopub.execute_input":"2023-05-10T10:19:41.612846Z","iopub.status.idle":"2023-05-10T10:19:41.619694Z","shell.execute_reply.started":"2023-05-10T10:19:41.612810Z","shell.execute_reply":"2023-05-10T10:19:41.618148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (3) plot-bb\nprint(p.plot_bb)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:41.988382Z","iopub.execute_input":"2023-05-10T10:19:41.988788Z","iopub.status.idle":"2023-05-10T10:19:41.994832Z","shell.execute_reply.started":"2023-05-10T10:19:41.988759Z","shell.execute_reply":"2023-05-10T10:19:41.993580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (4) text\nprint(p.text())","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:42.350587Z","iopub.execute_input":"2023-05-10T10:19:42.351040Z","iopub.status.idle":"2023-05-10T10:19:42.357593Z","shell.execute_reply.started":"2023-05-10T10:19:42.351005Z","shell.execute_reply":"2023-05-10T10:19:42.355976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## (4-1) text/id\nprint(p.text(filter=\"id\"))\n\nprint(\"-\"*30)\n\n## (4-2) text/polygon\nprint(p.text(filter=\"polygon\"))\n\nprint(\"-\"*30)\n\n## (4-3) text/text\nprint(p.text(filter=\"text\"))\n\nprint(\"-\"*30)\n\n## (4-4) text/role\nprint(p.text(filter=\"role\"))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:42.681877Z","iopub.execute_input":"2023-05-10T10:19:42.682361Z","iopub.status.idle":"2023-05-10T10:19:42.691990Z","shell.execute_reply.started":"2023-05-10T10:19:42.682324Z","shell.execute_reply":"2023-05-10T10:19:42.690226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (5) axes\nprint(p.axes)\n\nprint(\"-\"*30)\n\n## (5-1) axes/{x|y}-axis\n### (5-1-x) axes/x-axis\nprint(p.axis(axis=Axis.X))\n\nprint(\"-\"*30)\n\n### (5-1-y) axes/y-axis\nprint(p.axis(axis=Axis.Y))\n\nprint(\"-\"*30)\n\n## (5-2) axes/{x|y}-axis/ticks\nprint(p.ticks(axis=Axis.X))\n\nprint(\"-\"*30)\n\n### (5-2-1) axes/{x|y}-axis/ticks/id\nprint(p.ticks(axis=Axis.X, filter=\"id\"))\n\nprint(\"-\"*30)\n\n### (5-2-2) axes/{x|y}-axis/ticks/tick_pt\nprint(p.ticks(axis=Axis.X, filter=\"tick_pt\"))\n\nprint(\"-\"*30)\n\n### (5-2-3) axes/{x|y}-axis/tick-type\nprint(p.tick_type(axis=Axis.X))\n\nprint(\"-\"*30)\n\n### (5-2-4) axes/{x|y}-axis/values-type\nprint(p.values_type(axis=Axis.X))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:43.034366Z","iopub.execute_input":"2023-05-10T10:19:43.034809Z","iopub.status.idle":"2023-05-10T10:19:43.047109Z","shell.execute_reply.started":"2023-05-10T10:19:43.034775Z","shell.execute_reply":"2023-05-10T10:19:43.044657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# (6) data-series/{x|y}\nprint(p.data_series())\n\nprint(\"-\"*30)\n\n## (6-x) data-series/x\nprint(p.data_series(filter=\"x\"))\n\nprint(\"-\"*30)\n\n## (6-y) data-series/y\nprint(p.data_series(filter=\"y\"))","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:19:43.409639Z","iopub.execute_input":"2023-05-10T10:19:43.410069Z","iopub.status.idle":"2023-05-10T10:19:43.418971Z","shell.execute_reply.started":"2023-05-10T10:19:43.410037Z","shell.execute_reply":"2023-05-10T10:19:43.417590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:orange; text-decoration: underline;\">Visualiztion</span>\n","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:06:05.621906Z","iopub.execute_input":"2023-05-10T10:06:05.622345Z","iopub.status.idle":"2023-05-10T10:06:05.628479Z","shell.execute_reply.started":"2023-05-10T10:06:05.622313Z","shell.execute_reply":"2023-05-10T10:06:05.626991Z"}}},{"cell_type":"code","source":"# utils function for visualization\nfrom typing import Dict\n\nimport matplotlib.pyplot as plt\nfrom PIL import Image, ImageDraw\n\ndef draw_rect_angle(img:Image.Image, rectangle_coord:Dict[str, int]):\n    x0 = rectangle_coord['x0']\n    y0 = rectangle_coord['y0']\n    h = rectangle_coord['height']\n    w = rectangle_coord['width']\n    draw = ImageDraw.Draw(img)\n    draw.rectangle([(x0, y0),(x0 + w, y0 + h)], outline=\"blue\", width=0)\n    \n    \ndef draw_rect_angle_rotate(img:Image.Image, rectangle_coord:Dict[str,int], annotation_type:str):\n    x0 = rectangle_coord['x0']\n    x1 = rectangle_coord['x1']\n    x2 = rectangle_coord['x2']\n    x3 = rectangle_coord['x3']\n    y0 = rectangle_coord['y0']\n    y1 = rectangle_coord['y1']\n    y2 = rectangle_coord['y2']\n    y3 = rectangle_coord['y3']\n    draw = ImageDraw.Draw(img)\n    color = {\"tick_label\": (255, 0, 0), \"chart_title\": (0, 192, 192), \"axis_title\": (255, 255, 0)}[annotation_type]\n    draw.line([x0, y0, x1, y1], fill=color, width=2)\n    draw.line([x1, y1, x2, y2], fill=color, width=2)\n    draw.line([x2, y2, x3, y3], fill=color, width=2)\n    draw.line([x3, y3, x0, y0], fill=color, width=2)\n    \ndef draw_point(img:Image.Image, coord:Dict[str, int]):\n    draw = ImageDraw.Draw(img)\n    draw.ellipse([(coord['x'] - 1 , coord['y']-1), (coord['x']+1, coord['y']+1)], fill=\"lime\", outline=\"lime\", width=10)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:26:58.849384Z","iopub.execute_input":"2023-05-10T10:26:58.849820Z","iopub.status.idle":"2023-05-10T10:26:58.868137Z","shell.execute_reply.started":"2023-05-10T10:26:58.849787Z","shell.execute_reply":"2023-05-10T10:26:58.865876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualization_show(annotation_parser:AnnotationParser, index:int):\n    # bentech data path (i.e. /mnt/data/train/)\n    ap = annotation_parser.get_annotation(index=index)\n    img = Image.open(ap.image_path)\n\n    draw_rect_angle(img, rectangle_coord=ap.plot_bb)\n\n    polygon = ap.text(filter=\"polygon\")\n    role = ap.text(filter=\"role\")\n    for pg, rl in zip(polygon, role):\n        draw_rect_angle_rotate(img=img, rectangle_coord=pg,annotation_type=rl)\n\n    ticks_x = ap.ticks(axis=Axis.X, filter=\"tick_pt\")\n    ticks_y = ap.ticks(axis=Axis.Y, filter=\"tick_pt\")\n\n    for tick_x in ticks_x:\n        draw_point(img, tick_x)\n    for tick_y in ticks_y:\n        draw_point(img, tick_y)\n\n    #img.save(\"sample1.png\")\n    plt.imshow(img)\n    plt.axis('off')  # 軸を非表示にする\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:32:10.146772Z","iopub.execute_input":"2023-05-10T10:32:10.147289Z","iopub.status.idle":"2023-05-10T10:32:10.158708Z","shell.execute_reply.started":"2023-05-10T10:32:10.147255Z","shell.execute_reply":"2023-05-10T10:32:10.157228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random\nfor i in range(5):\n    random_index = random.randint(0, len(annotation_parser))\n    visualization_show(annotation_parser, random_index)","metadata":{"execution":{"iopub.status.busy":"2023-05-10T10:32:18.197742Z","iopub.execute_input":"2023-05-10T10:32:18.198220Z","iopub.status.idle":"2023-05-10T10:32:19.173044Z","shell.execute_reply.started":"2023-05-10T10:32:18.198186Z","shell.execute_reply":"2023-05-10T10:32:19.171810Z"},"trusted":true},"execution_count":null,"outputs":[]}]}