{"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":30587,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\ndata=pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ndata.head()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-07T07:15:21.837876Z","iopub.execute_input":"2023-12-07T07:15:21.838387Z","iopub.status.idle":"2023-12-07T07:15:21.865114Z","shell.execute_reply.started":"2023-12-07T07:15:21.838261Z","shell.execute_reply":"2023-12-07T07:15:21.864233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a function to generate the paths based on conditions\ndef generate_paths(row):\n    if row['is_tma'] == 1:\n        return f'/kaggle/input/UBC-OCEAN/train_images/{row[\"image_id\"]}.png'\n    else:\n        return f'/kaggle/input/UBC-OCEAN/train_thumbnails/{row[\"image_id\"]}_thumbnail.png'\n\n# Apply the function to create new columns\ndata['image_path'] = data.apply(generate_paths, axis=1)\ndata","metadata":{"execution":{"iopub.status.busy":"2023-12-07T07:15:21.867263Z","iopub.execute_input":"2023-12-07T07:15:21.867983Z","iopub.status.idle":"2023-12-07T07:15:21.89853Z","shell.execute_reply.started":"2023-12-07T07:15:21.867944Z","shell.execute_reply":"2023-12-07T07:15:21.897232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport cv2\nimport zipfile\nimport os\nimport pandas as pd\n\n# Function to apply denoising to an image\ndef denoise_image(image):\n    # Replace this with your denoising logic\n    # For example, using GaussianBlur\n    denoised_image = cv2.GaussianBlur(image, (5, 5), 0)\n    return denoised_image\n\n# Specify the directory to save processed images\nprocessed_images_directory = '/kaggle/working/processed_images'\nos.makedirs(processed_images_directory, exist_ok=True)\n\n# Iterate over the DataFrame and process images\nfor index, row in data.iterrows():\n    # Read the image using the image path in the 'image_path' column\n    image_path = row['image_path']\n    image = cv2.imread(image_path, 1)  # Read as color image\n    image_bw = cv2.imread(image_path, 0)  # Read as grayscale image\n\n    # Apply denoising to grayscale image\n    denoised_image_bw = denoise_image(image_bw)\n\n    # Convert the denoised grayscale image to color (RGB)\n    denoised_image_color = cv2.cvtColor(denoised_image_bw, cv2.COLOR_GRAY2BGR)\n\n    # Create subdirectory based on the label\n    label_directory = os.path.join(processed_images_directory, row['label'])\n    os.makedirs(label_directory, exist_ok=True)\n\n    # Save processed images in their respective label subdirectory\n    cv2.imwrite(os.path.join(label_directory, f\"processed_{index}.png\"), image)  # Use 'image' instead of 'denoised_image_bw'\n\n# Specify the path and name of the zip file to be created\nzip_file_path = '/kaggle/working/processed_images.zip'\n\n# Create a ZipFile object in write mode\nwith zipfile.ZipFile(zip_file_path, 'w') as zip_file:\n    # Walk through the directory and add each file to the zip file\n    for foldername, subfolders, filenames in os.walk(processed_images_directory):\n        for filename in filenames:\n            file_path = os.path.join(foldername, filename)\n            arcname = os.path.relpath(file_path, processed_images_directory)\n            zip_file.write(file_path, arcname)\n\nprint(f'Zip file created: {zip_file_path}')","metadata":{"execution":{"iopub.status.busy":"2023-12-07T07:15:21.900091Z","iopub.execute_input":"2023-12-07T07:15:21.900503Z"},"trusted":true},"execution_count":null,"outputs":[]}]}