File size: 7,328 Bytes
1533878 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
{
"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": []
}
]
} |