{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"import numpy as np # linear algebra\nimport pandas as pd #data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-09T12:34:17.943216Z","iopub.execute_input":"2023-12-09T12:34:17.944241Z","iopub.status.idle":"2023-12-09T12:34:17.950833Z","shell.execute_reply.started":"2023-12-09T12:34:17.944182Z","shell.execute_reply":"2023-12-09T12:34:17.949147Z"}}},{"cell_type":"code","source":"import pandas as pd\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:06:27.353529Z","iopub.execute_input":"2023-12-09T13:06:27.353986Z","iopub.status.idle":"2023-12-09T13:06:27.364393Z","shell.execute_reply.started":"2023-12-09T13:06:27.353951Z","shell.execute_reply":"2023-12-09T13:06:27.363396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full_train_image_path = \"/kaggle/input/UBC-OCEAN/train_images\"\nthumb_train_image_path = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"","metadata":{"execution":{"iopub.status.busy":"2023-12-09T12:34:17.975586Z","iopub.execute_input":"2023-12-09T12:34:17.975956Z","iopub.status.idle":"2023-12-09T12:34:17.980868Z","shell.execute_reply.started":"2023-12-09T12:34:17.975917Z","shell.execute_reply":"2023-12-09T12:34:17.979886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"/kaggle/input/UBC-OCEAN/train_thumbnails/10077_thumbnail.png\"","metadata":{"execution":{"iopub.status.busy":"2023-12-09T12:34:17.982283Z","iopub.execute_input":"2023-12-09T12:34:17.982863Z","iopub.status.idle":"2023-12-09T12:34:17.993873Z","shell.execute_reply.started":"2023-12-09T12:34:17.982828Z","shell.execute_reply":"2023-12-09T12:34:17.992856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import imageio\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nfrom imageio.plugins import freeimage\nimport warnings\nimport PIL\n\nfreeimage.FI_DecompressionBombError = lambda *args: None\nPIL.Image.MAX_IMAGE_PIXELS = 999999999999\n\ndef show_image_from_dataframe(df, index, target_size=(300, 300)):\n    image_id = df.loc[index, 'image_id']\n    label = df.loc[index, 'label']\n    width = df.loc[index, 'image_width']\n    height = df.loc[index, 'image_height']\n    is_tma = df.loc[index, 'is_tma']\n\n    # Load the original image\n    path_original = f'/kaggle/input/UBC-OCEAN/train_images/{image_id}.png'\n    if os.path.exists(path_original):\n        img_original = imageio.imread(path_original)\n    else:\n        warnings.warn(f\"Original image not found for image_id {image_id}.\")\n        return None, None\n\n    # Check if thumbnail exists for is_tma==False\n    if not is_tma:\n        path_thumbnail = f'/kaggle/input/UBC-OCEAN/train_thumbnails/{image_id}_thumbnail.png'\n        if os.path.exists(path_thumbnail):\n            img_thumbnail = imageio.imread(path_thumbnail)\n        else:\n            warnings.warn(f\"Thumbnail not found for image_id {image_id}. Using the original image.\")\n            img_thumbnail = img_original\n    else:\n        path_thumbnail = f'/kaggle/input/UBC-OCEAN/train_thumbnails/{image_id}_thumbnail.png'\n        if os.path.exists(path_thumbnail):\n            img_thumbnail = imageio.imread(path_thumbnail)\n        else:\n            warnings.warn(f\"Thumbnail not found for image_id {image_id}. Using the original image.\")\n            img_thumbnail = img_original\n\n    # Resize the original image using OpenCV\n    img_original_resized = cv2.resize(img_original, target_size, interpolation=cv2.INTER_AREA)\n\n    # Resize the thumbnail using OpenCV\n    img_thumbnail_resized = cv2.resize(img_thumbnail, target_size, interpolation=cv2.INTER_AREA)\n\n    # Compute width and height ratios\n    width_ratio = img_original.shape[1] / img_thumbnail.shape[1]\n    height_ratio = img_original.shape[0] / img_thumbnail.shape[0]\n\n    # Display the resized original image and thumbnail\n    plt.subplot(1, 2, 1)\n    plt.imshow(img_original_resized)\n    plt.title(f'Original Image\\nLabel: {label}, Width: {width}, Height: {height}, is_tma: {is_tma}')\n    plt.axis('off')\n\n    plt.subplot(1, 2, 2)\n    plt.imshow(img_thumbnail_resized)\n    plt.title('Thumbnail')\n    plt.axis('off')\n\n    plt.show()\n\n    print(f\"Width Ratio (Original/Thumbnail): {width_ratio}\")\n    print(f\"Height Ratio (Original/Thumbnail): {height_ratio}\")\n\n    return img_original_resized, img_thumbnail_resized\n\n# Specify the index you want to visualize\nindex_to_visualize = 0\n\nimg_original_resized, img_thumbnail_resized = show_image_from_dataframe(train_csv, index_to_visualize)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:00:53.436237Z","iopub.execute_input":"2023-12-09T13:00:53.437583Z","iopub.status.idle":"2023-12-09T13:01:17.686996Z","shell.execute_reply.started":"2023-12-09T13:00:53.43753Z","shell.execute_reply":"2023-12-09T13:01:17.685866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 1)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:01:17.688896Z","iopub.execute_input":"2023-12-09T13:01:17.689323Z","iopub.status.idle":"2023-12-09T13:03:15.942663Z","shell.execute_reply.started":"2023-12-09T13:01:17.689271Z","shell.execute_reply":"2023-12-09T13:03:15.941082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 2)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:03:15.944538Z","iopub.execute_input":"2023-12-09T13:03:15.945029Z","iopub.status.idle":"2023-12-09T13:03:17.015288Z","shell.execute_reply.started":"2023-12-09T13:03:15.944981Z","shell.execute_reply":"2023-12-09T13:03:17.014344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 3)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:03:17.017656Z","iopub.execute_input":"2023-12-09T13:03:17.018387Z","iopub.status.idle":"2023-12-09T13:03:47.310073Z","shell.execute_reply.started":"2023-12-09T13:03:17.018346Z","shell.execute_reply":"2023-12-09T13:03:47.308745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 4)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:03:47.311877Z","iopub.execute_input":"2023-12-09T13:03:47.31238Z","iopub.status.idle":"2023-12-09T13:04:40.462166Z","shell.execute_reply.started":"2023-12-09T13:03:47.312333Z","shell.execute_reply":"2023-12-09T13:04:40.461258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 5)","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:06:33.658444Z","iopub.execute_input":"2023-12-09T13:06:33.659142Z","iopub.status.idle":"2023-12-09T13:08:03.620556Z","shell.execute_reply.started":"2023-12-09T13:06:33.6591Z","shell.execute_reply":"2023-12-09T13:08:03.619324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 6)","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:08:03.623006Z","iopub.execute_input":"2023-12-09T13:08:03.623575Z","iopub.status.idle":"2023-12-09T13:09:50.875142Z","shell.execute_reply.started":"2023-12-09T13:08:03.623527Z","shell.execute_reply":"2023-12-09T13:09:50.873922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 7)","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:09:50.87665Z","iopub.execute_input":"2023-12-09T13:09:50.877011Z","iopub.status.idle":"2023-12-09T13:10:25.661878Z","shell.execute_reply.started":"2023-12-09T13:09:50.876977Z","shell.execute_reply":"2023-12-09T13:10:25.660637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 8)","metadata":{"execution":{"iopub.status.busy":"2023-12-09T13:10:25.664126Z","iopub.execute_input":"2023-12-09T13:10:25.664641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 9)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 10)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_resized = show_image_from_dataframe(train_csv, 11)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 12)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 13)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 14)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 15)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 16)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 17)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 18)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 19)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 20)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 21)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 22)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 23)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 24)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 25)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 26)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 27)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 28)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 29)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" img_resized = show_image_from_dataframe(train_csv, 30)","metadata":{},"execution_count":null,"outputs":[]}]}