{"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":"<div style=\"color:white;\n           display:fill;\n           border-radius:2px;\n           background-color:Black;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n<p style=\"padding: 10px;\n          text-align: center;\n          font-size:150%;\n          color:brown;\">\n           🎨Benetech Making Graphs Accessible📊\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n    \n</div>","metadata":{"execution":{"iopub.status.busy":"2023-06-16T14:59:52.379335Z","iopub.execute_input":"2023-06-16T14:59:52.379808Z","iopub.status.idle":"2023-06-16T14:59:53.468868Z","shell.execute_reply.started":"2023-06-16T14:59:52.379773Z","shell.execute_reply":"2023-06-16T14:59:53.467524Z"}}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:yellow;\">\n          📖Loading Library and Reading dataset💎\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport path","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:51.033239Z","iopub.execute_input":"2023-06-17T06:42:51.033694Z","iopub.status.idle":"2023-06-17T06:42:51.040399Z","shell.execute_reply.started":"2023-06-17T06:42:51.033660Z","shell.execute_reply":"2023-06-17T06:42:51.039113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tr_img = '/kaggle/input/benetech-making-graphs-accessible/train/images'\ntr_an  = '/kaggle/input/benetech-making-graphs-accessible/train/annotations'\nte_img = '/kaggle/input/benetech-making-graphs-accessible/test/images'\ndf_sub = pd.read_csv('/kaggle/input/benetech-making-graphs-accessible/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:51.045508Z","iopub.execute_input":"2023-06-17T06:42:51.045888Z","iopub.status.idle":"2023-06-17T06:42:51.063573Z","shell.execute_reply.started":"2023-06-17T06:42:51.045859Z","shell.execute_reply":"2023-06-17T06:42:51.062003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:green;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:yellow;\">\n          📖Reading the Dataset💎\n</p>\n<style>\n        h1{text-align: center;}\n </style>  \n","metadata":{}},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:skyblue;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:blue;\">\n           📚📚Checking the Type of data in Image 🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style>","metadata":{}},{"cell_type":"code","source":"img = cv2.imread('/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg')\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:51.065812Z","iopub.execute_input":"2023-06-17T06:42:51.066465Z","iopub.status.idle":"2023-06-17T06:42:51.378234Z","shell.execute_reply.started":"2023-06-17T06:42:51.066430Z","shell.execute_reply":"2023-06-17T06:42:51.377116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:skyblue;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:blue;\">\n           📚📚Loading a Train group of image in window 🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style>","metadata":{}},{"cell_type":"code","source":"import os\nimport matplotlib.pyplot as plt\n\nfig, axs = plt.subplots(4,4, figsize=(20, 12))\n\naxs = axs.flatten()\n\n\nfor img_path, ax in zip(os.listdir('/kaggle/input/benetech-making-graphs-accessible/train/images/'), axs):\n    \n    img = plt.imread(os.path.join('/kaggle/input/benetech-making-graphs-accessible/train/images/', img_path))\n    ax.imshow(img)\n    ax.set_title(img_path)\n    ax.axis('off')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:51.380421Z","iopub.execute_input":"2023-06-17T06:42:51.380932Z","iopub.status.idle":"2023-06-17T06:42:53.772969Z","shell.execute_reply.started":"2023-06-17T06:42:51.380891Z","shell.execute_reply":"2023-06-17T06:42:53.771737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:skyblue;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:blue;\">\n           📚📚Loading a Test group of image in window 🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style>","metadata":{}},{"cell_type":"code","source":"fig,axs=plt.subplots(2,3,figsize=(20,20))\naxs = axs.flatten()\nfor img_path,ax in zip(os.listdir(te_img),axs):\n    img = plt.imread(os.path.join(te_img,img_path))\n    ax.imshow(img)\n    ax.set_title(img_path)\n    ax.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:53.774801Z","iopub.execute_input":"2023-06-17T06:42:53.775237Z","iopub.status.idle":"2023-06-17T06:42:54.848079Z","shell.execute_reply.started":"2023-06-17T06:42:53.775203Z","shell.execute_reply":"2023-06-17T06:42:54.847218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<div style=\"color:white;\n           display:fill;\n           border-radius:5px;\n           background-color:skyblue;\n           font-size:250%;\n           font-family:Verdana;\n           letter-spacing:0.5px\">\n\n<p style=\"padding:3px;\n          text-align: center;\n          font-size:100%;\n          color:blue;\">\n           📚📚we'll use json to parse the JSON files, PIL to load and display the images, and matplotlib to visualize the images and annotations. 🧾📑\n</p>\n<style>\n        h1{text-align: center;}\n </style>","metadata":{}},{"cell_type":"code","source":"import json\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:54.850439Z","iopub.execute_input":"2023-06-17T06:42:54.851363Z","iopub.status.idle":"2023-06-17T06:42:54.856229Z","shell.execute_reply.started":"2023-06-17T06:42:54.851328Z","shell.execute_reply":"2023-06-17T06:42:54.855094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_json(file_path):\n    with open(file_path) as json_file:\n        data = json.load(json_file)\n    return data   ","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:54.857883Z","iopub.execute_input":"2023-06-17T06:42:54.858288Z","iopub.status.idle":"2023-06-17T06:42:54.869670Z","shell.execute_reply.started":"2023-06-17T06:42:54.858249Z","shell.execute_reply":"2023-06-17T06:42:54.868698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = tr_an+'/0000ae6cbdb1.json'\nload_json(file_path)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:54.871142Z","iopub.execute_input":"2023-06-17T06:42:54.871919Z","iopub.status.idle":"2023-06-17T06:42:54.905323Z","shell.execute_reply.started":"2023-06-17T06:42:54.871876Z","shell.execute_reply":"2023-06-17T06:42:54.904207Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nfrom PIL import Image, ImageDraw\n\ndef load_annotations(image_path, json_path, output_path):\n    # Load the image\n    image = Image.open(image_path)\n\n    # Load the JSON annotations\n    with open(json_path) as json_file:\n        data = json.load(json_file)\n\n    # Access the annotations based on the structure of your JSON file\n    # Modify the following lines according to your JSON structure\n    \n    # Get the visual elements from the JSON data\n    visual_elements = data['visual-elements']\n    \n    # Get the bars from the visual elements\n    bars = visual_elements['bars']\n    \n    # Create a drawing object\n    draw = ImageDraw.Draw(image)\n    \n    # Iterate over the bars and draw them on the image\n    for bar in bars:\n        x0 = bar['x0']\n        y0 = bar['y0']\n        width = bar['width']\n        height = bar['height']\n        # Draw a rectangle on the image using the bar coordinates\n        draw.rectangle([(x0, y0), (x0 + width, y0 + height)], outline='red')\n    \n    # Save the annotated image to the specified output path\n    image.save(output_path)\n\n# Example usage\nimage_path = \"/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg\"\njson_path = \"/kaggle/input/benetech-making-graphs-accessible/train/annotations/0000ae6cbdb1.json\"\noutput_path = \"/kaggle/working/annotated_image.jpg\"\n\nload_annotations(image_path, json_path, output_path)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:54.906942Z","iopub.execute_input":"2023-06-17T06:42:54.907323Z","iopub.status.idle":"2023-06-17T06:42:54.921325Z","shell.execute_reply.started":"2023-06-17T06:42:54.907292Z","shell.execute_reply":"2023-06-17T06:42:54.920040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = cv2.imread('/kaggle/working/annotated_image.jpg')\nplt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:54.923219Z","iopub.execute_input":"2023-06-17T06:42:54.923696Z","iopub.status.idle":"2023-06-17T06:42:55.235748Z","shell.execute_reply.started":"2023-06-17T06:42:54.923666Z","shell.execute_reply":"2023-06-17T06:42:55.234360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport json\n\n# Load JSON data from the file\nwith open('/kaggle/input/benetech-making-graphs-accessible/train/annotations/0000ae6cbdb1.json') as json_file:\n    data = json.load(json_file)\n\n# Load the image\nimage = plt.imread('/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg')\n\n# Create a figure and axes\nfig, ax = plt.subplots()\n\n# Display the image\nax.imshow(image)\n\n# Process the text elements\nfor text in data['text']:\n    x0, y0 = text['polygon']['x0'], text['polygon']['y0']\n    width = text['polygon']['x1'] - x0\n    height = text['polygon']['y2'] - y0\n    rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='r', facecolor='none')\n    ax.add_patch(rect)\n    ax.text(x0, y0, text['text'], fontsize=8, color='r')\n\n# Process the bars\nfor bar in data['visual-elements']['bars']:\n    x0, y0 = bar['x0'], bar['y0']\n    width, height = bar['width'], bar['height']\n    rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='b', facecolor='none')\n    ax.add_patch(rect)\n\n# Set the axis labels\nax.set_xlabel(data['axes']['x-axis'].get('label', ''))\nax.set_ylabel(data['axes']['y-axis'].get('label', ''))\n\n# Set the tick labels for x-axis and y-axis\nx_ticks = data['axes']['x-axis']['ticks']\ny_ticks = data['axes']['y-axis']['ticks']\n\nax.set_xticks([tick['tick_pt']['x'] for tick in x_ticks])\n#ax.set_xticklabels([tick['label'] for tick in x_ticks], rotation=90)\nax.set_xticklabels([tick.get('label', '') for tick in x_ticks], rotation=90)\n\nax.set_yticks([tick['tick_pt']['y'] for tick in y_ticks])\n#ax.set_yticklabels([tick['label'] for tick in y_ticks])\n\nax.set_yticklabels([tick.get('label', '') for tick in y_ticks])\n# Show the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:55.237513Z","iopub.execute_input":"2023-06-17T06:42:55.237878Z","iopub.status.idle":"2023-06-17T06:42:55.628249Z","shell.execute_reply.started":"2023-06-17T06:42:55.237846Z","shell.execute_reply":"2023-06-17T06:42:55.627031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport json\n\ndef process_image_with_annotations(image_path, json_path):\n    # Load JSON data from the file\n    with open(json_path) as json_file:\n        data = json.load(json_file)\n\n    # Load the image\n    image = plt.imread(image_path)\n\n    # Create a figure and axes\n    fig, ax = plt.subplots()\n\n    # Display the image\n    ax.imshow(image)\n\n    # Process the text elements\n    for text in data['text']:\n        x0, y0 = text['polygon']['x0'], text['polygon']['y0']\n        width = text['polygon']['x1'] - x0\n        height = text['polygon']['y2'] - y0\n        rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='r', facecolor='none')\n        ax.add_patch(rect)\n        ax.text(x0, y0, text['text'], fontsize=8, color='r')\n\n    # Process the bars\n    for bar in data['visual-elements']['bars']:\n        x0, y0 = bar['x0'], bar['y0']\n        width, height = bar['width'], bar['height']\n        rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='b', facecolor='none')\n        ax.add_patch(rect)\n\n    # Set the axis labels\n    ax.set_xlabel(data['axes']['x-axis'].get('label', ''))\n    ax.set_ylabel(data['axes']['y-axis'].get('label', ''))\n\n    # Set the tick labels for x-axis and y-axis\n    x_ticks = data['axes']['x-axis']['ticks']\n    y_ticks = data['axes']['y-axis']['ticks']\n\n    ax.set_xticks([tick['tick_pt']['x'] for tick in x_ticks])\n    ax.set_xticklabels([tick.get('label', '') for tick in x_ticks], rotation=90)\n\n    ax.set_yticks([tick['tick_pt']['y'] for tick in y_ticks])\n    ax.set_yticklabels([tick.get('label', '') for tick in y_ticks])\n\n    # Show the plot\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:55.631518Z","iopub.execute_input":"2023-06-17T06:42:55.631891Z","iopub.status.idle":"2023-06-17T06:42:55.651128Z","shell.execute_reply.started":"2023-06-17T06:42:55.631860Z","shell.execute_reply":"2023-06-17T06:42:55.649693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = '/kaggle/input/benetech-making-graphs-accessible/train/images/0003a50817cf.jpg'\njson_path = '/kaggle/input/benetech-making-graphs-accessible/train/annotations/0003a50817cf.json'\n\nprocess_image_with_annotations(image_path, json_path)","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:42:55.654096Z","iopub.execute_input":"2023-06-17T06:42:55.654537Z","iopub.status.idle":"2023-06-17T06:42:56.024746Z","shell.execute_reply.started":"2023-06-17T06:42:55.654501Z","shell.execute_reply":"2023-06-17T06:42:56.023579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nimport json\n\ndef plot_images_with_annotations(image_paths, json_paths):\n    num_images = len(image_paths)\n    num_cols = 2  # Number of columns in the subplot grid\n    num_rows = (num_images + 1) // num_cols  # Number of rows in the subplot grid\n\n    # Create the subplot grid\n    fig, axs = plt.subplots(num_rows, num_cols, figsize=(10, 5))\n\n    # Flatten the axes array if it's a single row or column\n    if num_rows == 1:\n        axs = axs.reshape(1, -1)\n    elif num_cols == 1:\n        axs = axs.reshape(-1, 1)\n\n    # Iterate over the images and annotations\n    for i in range(num_images):\n        image_path = image_paths[i]\n        json_path = json_paths[i]\n        ax = axs[i // num_cols, i % num_cols]  # Get the corresponding axis\n\n        # Load JSON data from the file\n        with open(json_path) as json_file:\n            data = json.load(json_file)\n\n        # Load the image\n        image = plt.imread(image_path)\n\n        # Display the image\n        ax.imshow(image)\n\n        # Process the text elements\n        for text in data['text']:\n            x0, y0 = text['polygon']['x0'], text['polygon']['y0']\n            width = text['polygon']['x1'] - x0\n            height = text['polygon']['y2'] - y0\n            rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='r', facecolor='none')\n            ax.add_patch(rect)\n           # ax.text(x0, y0, text['text'], fontsize=8, color='r')\n\n        # Process the bars\n        for bar in data['visual-elements']['bars']:\n            x0, y0 = bar['x0'], bar['y0']\n            width, height = bar['width'], bar['height']\n            rect = patches.Rectangle((x0, y0), width, height, linewidth=1, edgecolor='b', facecolor='none')\n            ax.add_patch(rect)\n\n        # Set the axis labels\n        ax.set_xlabel(data['axes']['x-axis'].get('label', ''))\n        ax.set_ylabel(data['axes']['y-axis'].get('label', ''))\n\n        # Set the tick labels for x-axis and y-axis\n        x_ticks = data['axes']['x-axis']['ticks']\n        y_ticks = data['axes']['y-axis']['ticks']\n\n        ax.set_xticks([tick['tick_pt']['x'] for tick in x_ticks])\n        ax.set_xticklabels([tick.get('label', '') for tick in x_ticks], rotation=90)\n\n        ax.set_yticks([tick['tick_pt']['y'] for tick in y_ticks])\n        ax.set_yticklabels([tick.get('label', '') for tick in y_ticks])\n\n    # Adjust the spacing between subplots\n    fig.tight_layout()\n\n    # Show the plot\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:49:41.807874Z","iopub.execute_input":"2023-06-17T06:49:41.808290Z","iopub.status.idle":"2023-06-17T06:49:41.828344Z","shell.execute_reply.started":"2023-06-17T06:49:41.808258Z","shell.execute_reply":"2023-06-17T06:49:41.827348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_paths = [\n    '/kaggle/input/benetech-making-graphs-accessible/train/images/0000ae6cbdb1.jpg',\n    '/kaggle/input/benetech-making-graphs-accessible/train/images/0003a50817cf.jpg',\n    '/kaggle/input/benetech-making-graphs-accessible/train/images/0005413054c9.jpg',\n    '/kaggle/input/benetech-making-graphs-accessible/train/images/0005e64fdc6e.jpg'\n]\n\njson_paths = [\n    '/kaggle/input/benetech-making-graphs-accessible/train/annotations/0000ae6cbdb1.json',\n    '/kaggle/input/benetech-making-graphs-accessible/train/annotations/0003a50817cf.json',\n    '/kaggle/input/benetech-making-graphs-accessible/train/annotations/0005413054c9.json',\n    '/kaggle/input/benetech-making-graphs-accessible/train/annotations/0005e64fdc6e.json'\n]\n\nplot_images_with_annotations(image_paths, json_paths)\n","metadata":{"execution":{"iopub.status.busy":"2023-06-17T06:50:30.637090Z","iopub.execute_input":"2023-06-17T06:50:30.638705Z","iopub.status.idle":"2023-06-17T06:50:31.780621Z","shell.execute_reply.started":"2023-06-17T06:50:30.638653Z","shell.execute_reply":"2023-06-17T06:50:31.779646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}