{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "9QLlZv6DlPC1" }, "outputs": [], "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')\n", "%cd /content/drive/MyDrive/sta_663/soybean/" ] }, { "cell_type": "code", "source": [ "import pandas as pd\n", "\n", "# Function to read ids from a file and return them as a list with leading zeros\n", "def read_ids(file_path):\n", " with open(file_path, 'r') as file:\n", " # Read the IDs, ensuring they are 6 digits long with leading zeros\n", " return [line.zfill(6) for line in file.read().splitlines()]\n", "\n", "# Function to read ids from a file and assign a set type\n", "def assign_set_type(file_path, set_type):\n", " # Read the file content\n", " with open(file_path, 'r') as file:\n", " ids = file.read().splitlines()\n", " # Update the 'sets' column based on the ids in the file\n", " df.loc[df['unique_id'].isin(ids), 'sets'] = set_type\n", "\n" ], "metadata": { "id": "prkF3wVLld_k" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Read unique_ids from all.txt\n", "all_file_path = '/content/drive/MyDrive/sta_663/soybean/ImageSets/Segmentation/all.txt'\n", "unique_ids = read_ids(all_file_path)\n", "\n", "# Initialize the DataFrame with unique_ids and default 'train' set\n", "df = pd.DataFrame(unique_ids, columns=['unique_id'])\n", "df['sets'] = 'train'\n", "\n", "# Assign 'test' to the sets column for IDs from test.txt\n", "test_file_path = '/content/drive/MyDrive/sta_663/soybean/ImageSets/Segmentation/test.txt'\n", "assign_set_type(test_file_path, 'test')\n", "\n", "# Assign 'valid' to the sets column for IDs from val.txt\n", "val_file_path = '/content/drive/MyDrive/sta_663/soybean/ImageSets/Segmentation/val.txt'\n", "assign_set_type(val_file_path, 'valid')\n", "\n" ], "metadata": { "id": "nsFdyvBzlgB_" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "file_path = '/content/drive/MyDrive/sta_663/soybean/dataset.csv'\n", "df.to_csv(file_path, index=False)" ], "metadata": { "id": "KjRGHJivliym" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "Download the dataset.csv file and put into the same directory as the downloaded zip file" ], "metadata": { "id": "qyyjofnUmXsh" } }, { "cell_type": "code", "source": [ "import os\n", "import pandas as pd\n", "import shutil" ], "metadata": { "id": "hm7ZaB5ImAeA" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Replace with the path to your CSV file\n", "csv_file_path = 'D:\\STA 663\\project_1\\dataset.csv'\n", "# Replace with the directory where your images are currently stored\n", "images_directory = 'D:\\STA 663\\project_1\\soybean\\JPEGImages'\n", "# Replace with the directory where you want to create test/train/validate directories\n", "output_base_directory = 'D:\\STA 663\\project_1'\n", "\n", "# Read the dataset\n", "df = pd.read_csv(csv_file_path)\n", "df['unique_id'] = df['unique_id'].astype(str).str.zfill(6)" ], "metadata": { "id": "iTKsnTUdmI3N" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Create directories for the sets if they don't exist\n", "for set_type in ['test', 'train', 'valid']:\n", " set_directory = os.path.join(output_base_directory, set_type)\n", " if not os.path.exists(set_directory):\n", " os.makedirs(set_directory)\n", "\n", "# Function to move and rename files\n", "def move_and_rename_files(row):\n", " file_name = f\"{row['unique_id']}.jpg\" # Assuming the images are .jpg\n", " original_path = os.path.join(images_directory, file_name)\n", " if os.path.isfile(original_path):\n", " set_type = row['sets']\n", " new_name = f\"{row['unique_id']}_original.jpg\"\n", " new_path = os.path.join(output_base_directory, set_type, new_name)\n", " # Move and rename the file\n", " shutil.copy(original_path, new_path) # Use shutil.copy if you want to keep the originals\n", "\n", "# Apply the function to each row in the dataframe\n", "df.apply(move_and_rename_files, axis=1)" ], "metadata": { "id": "2dvMgZcOmLt2" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "### Do the same thing for segmentation class" ], "metadata": { "id": "aZb9yoXumrrp" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Replace with the path to your CSV file\n", "csv_file_path = 'D:\\STA 663\\project_1\\dataset.csv'\n", "# Replace with the directory where your images are currently stored\n", "images_directory = 'D:\\STA 663\\project_1\\soybean\\SegmentationClass'\n", "# Replace with the directory where you want to create test/train/validate directories\n", "output_base_directory = 'D:\\STA 663\\project_1'" ], "metadata": { "id": "Ud79pkDMmyA_" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "# Function to move and rename files\n", "def move_and_rename_files(row):\n", " file_name = f\"{row['unique_id']}.png\" # Assuming the images are .jpg\n", " original_path = os.path.join(images_directory, file_name)\n", " if os.path.isfile(original_path):\n", " set_type = row['sets']\n", " new_name = f\"{row['unique_id']}_segmentation.jpg\"\n", " new_path = os.path.join(output_base_directory, set_type, new_name)\n", " # Move and rename the file\n", " shutil.copy(original_path, new_path) # Use shutil.copy if you want to keep the originals\n", "\n", "# Apply the function to each row in the dataframe\n", "df.apply(move_and_rename_files, axis=1)" ], "metadata": { "id": "UoJLs5-Dm2u5" }, "execution_count": null, "outputs": [] } ] }