{"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 cv2\nimport json\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport random\nimport shutil\n\n\nfrom glob import glob\nfrom tqdm import tqdm\n\n!mkdir images","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-04-18T11:27:59.407485Z","iopub.execute_input":"2023-04-18T11:27:59.407846Z","iopub.status.idle":"2023-04-18T11:27:59.675671Z","shell.execute_reply.started":"2023-04-18T11:27:59.407808Z","shell.execute_reply":"2023-04-18T11:27:59.673957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def extract_plot_bb(data):\n    x0 = data[\"plot-bb\"][\"x0\"]\n    y0 = data[\"plot-bb\"][\"y0\"]\n    w = data[\"plot-bb\"][\"width\"]\n    h = data[\"plot-bb\"][\"height\"]\n    return [x0, y0, w, h, \"plot-bb\"]\n\n\ndef extract_label_bbox(data, rows, img_id):\n    common = []\n    common+= [img_id, data[\"source\"], data[\"chart-type\"]]\n    for box in data[\"text\"]:\n        x0 = box[\"polygon\"][\"x0\"]\n        y0 = box[\"polygon\"][\"y0\"]\n        w = box[\"polygon\"][\"x1\"] - box[\"polygon\"][\"x0\"]\n        h = box[\"polygon\"][\"y3\"] - box[\"polygon\"][\"y0\"]\n        label = box[\"role\"]\n        box_row = common + [x0, y0, w, h, label]\n        rows.append(box_row)\n    rows.append(common + extract_plot_bb(data)) \n    return rows\n        \ndef extract_tick_bbox(data, rows, img_id):\n    common = []\n    common+= [img_id , data[\"source\"], data[\"chart-type\"]]\n    for axis in data[\"axes\"].keys():\n        for box in data[\"axes\"][axis][\"ticks\"]:\n            x0 = box[\"tick_pt\"][\"x\"] - 5\n            y0 = box[\"tick_pt\"][\"y\"] - 5\n            w = 10\n            h = 10\n            label = axis + \"-tick\"\n            box_row = common + [x0, y0, w, h, label]\n            rows.append(box_row)\n    return rows","metadata":{"execution":{"iopub.status.busy":"2023-04-18T11:32:35.855217Z","iopub.execute_input":"2023-04-18T11:32:35.855681Z","iopub.status.idle":"2023-04-18T11:32:35.867590Z","shell.execute_reply.started":"2023-04-18T11:32:35.855642Z","shell.execute_reply":"2023-04-18T11:32:35.865419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ANNOTATION = \"/kaggle/input/benetech-making-graphs-accessible/train/annotations/*.json\"\nrows = []\nfor file_name in tqdm(glob(ANNOTATION)):\n    label_bbox = []\n    tick_bbox = []\n    with open(file_name) as f:\n        data = json.load(f)\n        img_id = file_name.split(\"/\")[-1].split(\".\")[0]\n        label_bbox = extract_label_bbox(data, label_bbox, img_id)\n        tick_bbox = extract_tick_bbox(data, tick_bbox, img_id)\n        data_rows = label_bbox + tick_bbox\n    rows += data_rows","metadata":{"execution":{"iopub.status.busy":"2023-04-18T11:32:40.500400Z","iopub.execute_input":"2023-04-18T11:32:40.500755Z","iopub.status.idle":"2023-04-18T11:39:48.849937Z","shell.execute_reply.started":"2023-04-18T11:32:40.500725Z","shell.execute_reply":"2023-04-18T11:39:48.848335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(rows)\ndf.columns = [\"image_id\", \"source\", \"chart\", \"x0\", \"y0\", \"w\", \"h\", \"label\"]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-04-18T11:40:09.240128Z","iopub.execute_input":"2023-04-18T11:40:09.240568Z","iopub.status.idle":"2023-04-18T11:40:12.160288Z","shell.execute_reply.started":"2023-04-18T11:40:09.240532Z","shell.execute_reply":"2023-04-18T11:40:12.159037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.patches as patches\nfig, ax = plt.subplots()\nplt.rcParams[\"figure.figsize\"] = (20,15)\nimage_id = random.choice(df[\"image_id\"].unique().tolist())\nsample = df[df[\"image_id\"]==image_id]\nfor index, row in sample.iterrows():\n    x0, y0, W, H = row[\"x0\"], row[\"y0\"], row[\"w\"], row[\"h\"]\n    rect = patches.Rectangle((x0, y0), W, H,\n                             linewidth=1,\n                             edgecolor='g',\n                             facecolor='none')\n    ax.add_patch(rect)\n\nTRAIN_FOLDER = \"/kaggle/input/benetech-making-graphs-accessible/train/images/\"\nimage = cv2.imread(TRAIN_FOLDER + image_id + \".jpg\")\n# Display the image\nax.imshow(image)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-18T11:41:00.759966Z","iopub.execute_input":"2023-04-18T11:41:00.760378Z","iopub.status.idle":"2023-04-18T11:41:01.544298Z","shell.execute_reply.started":"2023-04-18T11:41:00.760341Z","shell.execute_reply":"2023-04-18T11:41:01.542309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv(\"train.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T11:41:25.150327Z","iopub.execute_input":"2023-04-18T11:41:25.150734Z","iopub.status.idle":"2023-04-18T11:41:30.619507Z","shell.execute_reply.started":"2023-04-18T11:41:25.150697Z","shell.execute_reply":"2023-04-18T11:41:30.617803Z"},"trusted":true},"execution_count":null,"outputs":[]}]}