{"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":"# Load Data","metadata":{}},{"cell_type":"markdown","source":"Based in https://www.kaggle.com/code/durvalslompojunior/bmga-train-dataset-csv-meta/edit","metadata":{}},{"cell_type":"code","source":"from glob import glob\nimport PIL.Image as Image\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nannotations = glob(\"/kaggle/input/benetech-making-graphs-accessible/train/annotations/*.json\")\n\"Total Training Samples\", len(annotations)","metadata":{"_uuid":"9f14266d-1f62-4d83-9ac1-c26e23169386","_cell_guid":"2e7ca305-7133-4415-af83-e24a63313465","collapsed":false,"_kg_hide-input":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-03-31T01:30:22.508732Z","iopub.execute_input":"2023-03-31T01:30:22.509221Z","iopub.status.idle":"2023-03-31T01:30:23.282639Z","shell.execute_reply.started":"2023-03-31T01:30:22.509179Z","shell.execute_reply":"2023-03-31T01:30:23.281274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nfrom tqdm import tqdm\nannotations_data = [json.load(open(path,'r')) for path in tqdm(annotations, total=len(annotations))]","metadata":{"_uuid":"bf05d0eb-ba94-4130-9ffb-8b6930f2b9a9","_cell_guid":"db599ec0-a997-47fd-895e-b247eb7b4e7a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-03-31T01:30:23.285451Z","iopub.execute_input":"2023-03-31T01:30:23.286591Z","iopub.status.idle":"2023-03-31T01:36:19.192399Z","shell.execute_reply.started":"2023-03-31T01:30:23.286540Z","shell.execute_reply":"2023-03-31T01:36:19.191100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = annotations_data[0]\nprint(test.keys())\nprint(test['plot-bb'].keys())\nprint(test['axes'].keys())","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:36:19.194003Z","iopub.execute_input":"2023-03-31T01:36:19.194374Z","iopub.status.idle":"2023-03-31T01:36:19.201726Z","shell.execute_reply.started":"2023-03-31T01:36:19.194337Z","shell.execute_reply":"2023-03-31T01:36:19.200378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain = pd.DataFrame(annotations, columns=['path'])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:36:19.204996Z","iopub.execute_input":"2023-03-31T01:36:19.205510Z","iopub.status.idle":"2023-03-31T01:36:19.220479Z","shell.execute_reply.started":"2023-03-31T01:36:19.205469Z","shell.execute_reply":"2023-03-31T01:36:19.218432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train\ntrain['name'] = train['path'].apply(lambda x: x.split('/')[-1].replace(\".json\", \"\"))\ntrain['annotation'] = \"annotations/\"+train['name']+\".json\"\ntrain['image'] = \"images/\"+train['name']+\".jpg\"\ntrain['source'] = [x['source'] for x in annotations_data]\ntrain['chart-type'] = [x['chart-type'] for x in annotations_data]\nfor key in test['plot-bb'].keys():\n    train[f\"{key}\"] = [x['plot-bb'][key] for x in annotations_data]\n    #train[f\"{key}_len\"] = [len(x['plot-bb'][key]) for x in annotations_data]\nfor key in test['axes'].keys():\n    train[f\"{key}\"] = [x['axes'][key] for x in annotations_data]\n    #train[f\"{key}_len\"] = [len(x['axes'][key]) for x in annotations_data]\ntrain['text'] = [x['text'] for x in annotations_data]\n\ntrain['data-series'] = [x['data-series'] for x in annotations_data]\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:36:20.215227Z","iopub.execute_input":"2023-03-31T01:36:20.215593Z","iopub.status.idle":"2023-03-31T01:36:21.187978Z","shell.execute_reply.started":"2023-03-31T01:36:20.215559Z","shell.execute_reply":"2023-03-31T01:36:21.186992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(by=['source', 'chart-type'])['path'].count()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:36:21.189386Z","iopub.execute_input":"2023-03-31T01:36:21.190688Z","iopub.status.idle":"2023-03-31T01:36:21.271643Z","shell.execute_reply.started":"2023-03-31T01:36:21.190631Z","shell.execute_reply":"2023-03-31T01:36:21.269922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby(by=['chart-type'])['path'].count()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:36:46.605611Z","iopub.execute_input":"2023-03-31T01:36:46.606033Z","iopub.status.idle":"2023-03-31T01:36:46.649785Z","shell.execute_reply.started":"2023-03-31T01:36:46.605997Z","shell.execute_reply":"2023-03-31T01:36:46.648455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv(\"train.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:38:37.346712Z","iopub.execute_input":"2023-03-31T01:38:37.347149Z","iopub.status.idle":"2023-03-31T01:38:47.103456Z","shell.execute_reply.started":"2023-03-31T01:38:37.347112Z","shell.execute_reply":"2023-03-31T01:38:47.102345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = \"/kaggle/input/benetech-making-graphs-accessible/train\"","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:38:55.881073Z","iopub.execute_input":"2023-03-31T01:38:55.882077Z","iopub.status.idle":"2023-03-31T01:38:55.886120Z","shell.execute_reply.started":"2023-03-31T01:38:55.882032Z","shell.execute_reply":"2023-03-31T01:38:55.885261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Load an graph","metadata":{}},{"cell_type":"code","source":"index = 10\ngraph = np.array(Image.open(root_dir+\"/\"+train['image'][index]+\"\"))\nfig, ax = plt.subplots(1,1)\nax.set_title(label=train['name'][index])\nax.imshow(graph, cmap='gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:40:36.419800Z","iopub.execute_input":"2023-03-31T01:40:36.420421Z","iopub.status.idle":"2023-03-31T01:40:36.626206Z","shell.execute_reply.started":"2023-03-31T01:40:36.420383Z","shell.execute_reply":"2023-03-31T01:40:36.624931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rect = (train['x0'][index], train['y0'][index], train['width'][index], train['height'][index])\nfig, ax = plt.subplots()\nax.set_title(label=f\"{train['name'][index]}-chart\")\nax.imshow(graph, cmap = \"gray\")\npatch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\nax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:42:12.346272Z","iopub.execute_input":"2023-03-31T01:42:12.347207Z","iopub.status.idle":"2023-03-31T01:42:12.551541Z","shell.execute_reply.started":"2023-03-31T01:42:12.347168Z","shell.execute_reply":"2023-03-31T01:42:12.550783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nax.set_title(label=f\"{train['name'][index]}-x-axis-ticks\")\nwidth_step = 4/2\nax.imshow(graph, cmap = \"gray\")\nfor xaxis in  train['x-axis'][index]['ticks']:\n    rect = (xaxis['tick_pt']['x']-width_step, xaxis['tick_pt']['y']-width_step, +width_step, +width_step)\n    patch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\n    ax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:43:38.898033Z","iopub.execute_input":"2023-03-31T01:43:38.898457Z","iopub.status.idle":"2023-03-31T01:43:39.124422Z","shell.execute_reply.started":"2023-03-31T01:43:38.898422Z","shell.execute_reply":"2023-03-31T01:43:39.122642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nwidth_step = 4/2\nax.imshow(graph, cmap = \"gray\")\nax.set_title(label=f\"{train['name'][index]}-y-axis-ticks\")\nfor yaxis in  train['y-axis'][index]['ticks']:\n    rect = (yaxis['tick_pt']['x']-width_step, yaxis['tick_pt']['y']-width_step, +width_step, +width_step)\n    patch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\n    ax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:43:55.147483Z","iopub.execute_input":"2023-03-31T01:43:55.147877Z","iopub.status.idle":"2023-03-31T01:43:55.359364Z","shell.execute_reply.started":"2023-03-31T01:43:55.147842Z","shell.execute_reply":"2023-03-31T01:43:55.357947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nwidth_step = 4/2\nax.imshow(graph, cmap = \"gray\")\nax.set_title(label=f\"{train['name'][index]}-axis-data-and-chart-meta\")\nfor text in  train['text'][index]:\n    rect = (text['polygon']['x0'],text['polygon']['y0'], text['polygon']['x1'] - text['polygon']['x0'], text['polygon']['y2'] - text['polygon']['y1'])\n    patch = patches.Rectangle((rect[0], rect[1]), rect[2], rect[3], linewidth=2, edgecolor='r', facecolor='none')\n    ax.add_patch(patch)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:44:08.407740Z","iopub.execute_input":"2023-03-31T01:44:08.408251Z","iopub.status.idle":"2023-03-31T01:44:08.641387Z","shell.execute_reply.started":"2023-03-31T01:44:08.408190Z","shell.execute_reply":"2023-03-31T01:44:08.640292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['chart-type'][index], train['data-series'][index]","metadata":{"execution":{"iopub.status.busy":"2023-03-31T01:44:20.665400Z","iopub.execute_input":"2023-03-31T01:44:20.665846Z","iopub.status.idle":"2023-03-31T01:44:20.675243Z","shell.execute_reply.started":"2023-03-31T01:44:20.665793Z","shell.execute_reply":"2023-03-31T01:44:20.673906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resize images","metadata":{}},{"cell_type":"code","source":"train.describe()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:23:47.363399Z","iopub.execute_input":"2023-03-31T02:23:47.363798Z","iopub.status.idle":"2023-03-31T02:23:47.433982Z","shell.execute_reply.started":"2023-03-31T02:23:47.363764Z","shell.execute_reply":"2023-03-31T02:23:47.432878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport PIL\nimport os\nimport glob\n\nindex = 1\nimage = Image.open(root_dir+\"/\"+train['image'][index]+\"\")\nwidth = image.size[0]\nheight = image.size[1]\nprint(f'index: {index}')\nprint(f'width: {width}')\nprint(f'height: {height}')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:46:08.868947Z","iopub.execute_input":"2023-03-31T02:46:08.869982Z","iopub.status.idle":"2023-03-31T02:46:08.879887Z","shell.execute_reply.started":"2023-03-31T02:46:08.869949Z","shell.execute_reply":"2023-03-31T02:46:08.878607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:46:35.046269Z","iopub.execute_input":"2023-03-31T02:46:35.047484Z","iopub.status.idle":"2023-03-31T02:46:35.070996Z","shell.execute_reply.started":"2023-03-31T02:46:35.047442Z","shell.execute_reply":"2023-03-31T02:46:35.070129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_width = 360\n\nwidth_percent = (base_width / float(image.size[0]))\nhsize = int((float(image.size[1]) * float(width_percent)))\nimage = image.resize((base_width, hsize), PIL.Image.LANCZOS)\nimage = image.convert('L')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:52:16.506285Z","iopub.execute_input":"2023-03-31T02:52:16.507522Z","iopub.status.idle":"2023-03-31T02:52:16.513668Z","shell.execute_reply.started":"2023-03-31T02:52:16.507464Z","shell.execute_reply":"2023-03-31T02:52:16.512461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image ","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:52:20.296881Z","iopub.execute_input":"2023-03-31T02:52:20.298139Z","iopub.status.idle":"2023-03-31T02:52:20.308060Z","shell.execute_reply.started":"2023-03-31T02:52:20.298074Z","shell.execute_reply":"2023-03-31T02:52:20.306875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.save('resized_compressed_image.jpg')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T02:52:23.213856Z","iopub.execute_input":"2023-03-31T02:52:23.214262Z","iopub.status.idle":"2023-03-31T02:52:23.220936Z","shell.execute_reply.started":"2023-03-31T02:52:23.214213Z","shell.execute_reply":"2023-03-31T02:52:23.219937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('/kaggle/working/train/images')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_and_grayscale_image(root_dir, file_path, save = True, base_width = 360):\n    image = Image.open(root_dir+\"/\"+file_path+\"\")\n    width_percent = (base_width / float(image.size[0]))\n    hsize = int((float(image.size[1]) * float(width_percent)))\n    image = image.resize((base_width, hsize), PIL.Image.LANCZOS)\n    image = image.convert('L')\n    if save:\n        image.save('/kaggle/working/train/'+file_path)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-03-31T03:15:46.280014Z","iopub.execute_input":"2023-03-31T03:15:46.280488Z","iopub.status.idle":"2023-03-31T03:15:46.287075Z","shell.execute_reply.started":"2023-03-31T03:15:46.280457Z","shell.execute_reply":"2023-03-31T03:15:46.285568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file_path in train['image']:\n    resize_and_grayscale_image(root_dir, file_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T03:01:40.962500Z","iopub.execute_input":"2023-03-31T03:01:40.963137Z","iopub.status.idle":"2023-03-31T03:15:46.278452Z","shell.execute_reply.started":"2023-03-31T03:01:40.963093Z","shell.execute_reply":"2023-03-31T03:15:46.277499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting Chart Types","metadata":{}},{"cell_type":"code","source":"# Next steps...","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting X and Y and OCR","metadata":{}},{"cell_type":"code","source":"# Next steps...","metadata":{},"execution_count":null,"outputs":[]}]}