{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np\nimport pandas as pd \nimport os\nimport json\nimport cv2\nimport matplotlib.pyplot as plt\n\nimage_folder = '/kaggle/input/benetech-making-graphs-accessible/train/images'\nannotation_folder = '/kaggle/input/benetech-making-graphs-accessible/train/annotations/'\n\nimage_files = sorted(os.listdir(image_folder))\nannotation_files = sorted(os.listdir(annotation_folder))\n\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-31T13:02:32.043353Z","iopub.execute_input":"2023-03-31T13:02:32.043770Z","iopub.status.idle":"2023-03-31T13:02:33.496563Z","shell.execute_reply.started":"2023-03-31T13:02:32.043737Z","shell.execute_reply":"2023-03-31T13:02:33.495259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations = []\n\nfor annotation_file in annotation_files:\n    with open(os.path.join(annotation_folder, annotation_file), 'r') as f:\n        annotation = json.load(f)\n    annotations.append(annotation)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:02:33.498509Z","iopub.execute_input":"2023-03-31T13:02:33.499232Z","iopub.status.idle":"2023-03-31T13:08:01.735684Z","shell.execute_reply.started":"2023-03-31T13:02:33.499195Z","shell.execute_reply":"2023-03-31T13:08:01.734498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations_df = pd.DataFrame(annotations)\nannotations_df","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:14:43.211299Z","iopub.execute_input":"2023-03-31T13:14:43.211818Z","iopub.status.idle":"2023-03-31T13:14:43.426525Z","shell.execute_reply.started":"2023-03-31T13:14:43.211774Z","shell.execute_reply":"2023-03-31T13:14:43.425664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"annotations_df[\"chart-type\"].value_counts()\n# Excruciatingly small number of horizontal bar and dot charts.","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:15:47.941968Z","iopub.execute_input":"2023-03-31T13:15:47.942414Z","iopub.status.idle":"2023-03-31T13:15:47.968480Z","shell.execute_reply.started":"2023-03-31T13:15:47.942375Z","shell.execute_reply":"2023-03-31T13:15:47.967297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#   **Visualizing the Images from the Dataset**","metadata":{}},{"cell_type":"code","source":"def visualize_image(image_file, annotation, image_folder):\n    img = cv2.imread(os.path.join(image_folder, image_file))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n\n    # Extract chart type and other relevant information from the annotation\n    chart_type = annotation['chart-type']\n\n    plt.figure(figsize=(10, 10))\n    plt.title(f'Chart Type: {chart_type}')\n    plt.imshow(img)\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:21:57.809475Z","iopub.execute_input":"2023-03-31T13:21:57.809885Z","iopub.status.idle":"2023-03-31T13:21:57.816671Z","shell.execute_reply.started":"2023-03-31T13:21:57.809851Z","shell.execute_reply":"2023-03-31T13:21:57.815288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    visualize_image(image_files[i], annotations[i], image_folder)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:21:58.069955Z","iopub.execute_input":"2023-03-31T13:21:58.070636Z","iopub.status.idle":"2023-03-31T13:21:59.858739Z","shell.execute_reply.started":"2023-03-31T13:21:58.070597Z","shell.execute_reply":"2023-03-31T13:21:59.857410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Lets Check the Dimensions of Images","metadata":{}},{"cell_type":"code","source":"image_dimensions = []\n\nfor image_file in image_files:\n    img = cv2.imread(os.path.join(image_folder, image_file))\n    height, width, channels = img.shape\n    image_dimensions.append((height, width, channels))","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:27:12.196233Z","iopub.execute_input":"2023-03-31T13:27:12.196666Z","iopub.status.idle":"2023-03-31T13:33:39.165044Z","shell.execute_reply.started":"2023-03-31T13:27:12.196633Z","shell.execute_reply":"2023-03-31T13:33:39.163960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"aspect_ratios = [dim[1] / dim[0] for dim in image_dimensions]\nheights = [dim[0] for dim in image_dimensions]\nwidths = [dim[1] for dim in image_dimensions]\n\nprint(f\"Min height: {np.min(heights)}, Max height: {np.max(heights)}, Avg height: {np.mean(heights)}\")\nprint(f\"Min width: {np.min(widths)}, Max width: {np.max(widths)}, Avg width: {np.mean(widths)}\")\nprint(f\"Min aspect ratio: {np.min(aspect_ratios)}, Max aspect ratio: {np.max(aspect_ratios)}, Avg aspect ratio: {np.mean(aspect_ratios)}\")","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:38:07.965668Z","iopub.execute_input":"2023-03-31T13:38:07.966502Z","iopub.status.idle":"2023-03-31T13:38:08.039619Z","shell.execute_reply.started":"2023-03-31T13:38:07.966455Z","shell.execute_reply":"2023-03-31T13:38:08.038405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(12, 4))\n\nplt.subplot(1, 3, 1)\nplt.hist(heights, bins=50)\nplt.xlabel('Height')\nplt.ylabel('Frequency')\nplt.title('Height Distribution')\n\nplt.subplot(1, 3, 2)\nplt.hist(widths, bins=50)\nplt.xlabel('Width')\nplt.ylabel('Frequency')\nplt.title('Width Distribution')\n\nplt.subplot(1, 3, 3)\nplt.hist(aspect_ratios, bins=50)\nplt.xlabel('Aspect Ratio (Width / Height)')\nplt.ylabel('Frequency')\nplt.title('Aspect Ratio Distribution')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:38:36.106527Z","iopub.execute_input":"2023-03-31T13:38:36.106965Z","iopub.status.idle":"2023-03-31T13:38:37.524949Z","shell.execute_reply.started":"2023-03-31T13:38:36.106909Z","shell.execute_reply":"2023-03-31T13:38:37.523542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> We can see that there is considerable variation in the height, width and the aspect ratios of the images. We may want to create a standard input for our images (e.g., 224x224 or 299x299 for pretrained models like ResNet or Inception)\n> \n> By preprocessing the images to a consistent size and aspect ratio, you can help ensure that your model receives uniform input during training. This can lead to better performance and generalization.","metadata":{}},{"cell_type":"markdown","source":" # **Pre-Processing**","metadata":{}},{"cell_type":"code","source":"def resize_and_pad(img, target_size, pad_value=0):\n    height, width, channels = img.shape\n    aspect_ratio = width / height\n\n    # Calculate new dimensions while maintaining the aspect ratio\n    if height > width:\n        new_height = target_size\n        new_width = int(aspect_ratio * new_height)\n    else:\n        new_width = target_size\n        new_height = int(new_width / aspect_ratio)\n\n    # Resize the image\n    resized_img = cv2.resize(img, (new_width, new_height))\n\n    # Pad the image to reach the target size\n    pad_height = target_size - new_height\n    pad_width = target_size - new_width\n\n    top_pad = pad_height // 2\n    bottom_pad = pad_height - top_pad\n    left_pad = pad_width // 2\n    right_pad = pad_width - left_pad\n\n    padded_img = cv2.copyMakeBorder(resized_img, top_pad, bottom_pad, left_pad, right_pad,\n                                    cv2.BORDER_CONSTANT, value=[pad_value, pad_value, pad_value])\n\n    return padded_img\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:49:53.848302Z","iopub.execute_input":"2023-03-31T13:49:53.849181Z","iopub.status.idle":"2023-03-31T13:49:53.857675Z","shell.execute_reply.started":"2023-03-31T13:49:53.849138Z","shell.execute_reply":"2023-03-31T13:49:53.856292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target_size = 299  # Change this to the desired size\n\npreprocessed_images = []\n\nfor image_file in image_files:\n    img = cv2.imread(os.path.join(image_folder, image_file))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    preprocessed_img = resize_and_pad(img, target_size)\n    preprocessed_images.append(preprocessed_img)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T13:50:17.894297Z","iopub.execute_input":"2023-03-31T13:50:17.895583Z","iopub.status.idle":"2023-03-31T13:55:00.794111Z","shell.execute_reply.started":"2023-03-31T13:50:17.895527Z","shell.execute_reply":"2023-03-31T13:55:00.792582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}