Update Autolens_train_test_split.py
Browse files- Autolens_train_test_split.py +41 -0
Autolens_train_test_split.py
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import shutil
|
| 4 |
+
|
| 5 |
+
# Define folder paths
|
| 6 |
+
images_folder = "images_folder"
|
| 7 |
+
xml_folder = "xml_folder"
|
| 8 |
+
test_images_folder = "test_images"
|
| 9 |
+
test_xml_folder = "test_xml"
|
| 10 |
+
|
| 11 |
+
# Create test folders if they don't exist
|
| 12 |
+
os.makedirs(test_images_folder, exist_ok=True)
|
| 13 |
+
os.makedirs(test_xml_folder, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
# Get list of all XML files
|
| 16 |
+
xml_files = [f for f in os.listdir(xml_folder) if f.endswith(".xml")]
|
| 17 |
+
|
| 18 |
+
# Select 20% of files randomly
|
| 19 |
+
num_to_move = int(len(xml_files) * 0.2)
|
| 20 |
+
selected_files = random.sample(xml_files, num_to_move)
|
| 21 |
+
|
| 22 |
+
# Move files to test folders
|
| 23 |
+
for xml_file in selected_files:
|
| 24 |
+
# Get corresponding image filename
|
| 25 |
+
file_name = os.path.splitext(xml_file)[0] # Remove extension
|
| 26 |
+
image_path = os.path.join(images_folder, file_name + ".jpg") # Adjust extension if needed
|
| 27 |
+
xml_path = os.path.join(xml_folder, xml_file)
|
| 28 |
+
|
| 29 |
+
# Define destination paths
|
| 30 |
+
test_image_path = os.path.join(test_images_folder, file_name + ".jpg")
|
| 31 |
+
test_xml_path = os.path.join(test_xml_folder, xml_file)
|
| 32 |
+
|
| 33 |
+
# Move files if both exist
|
| 34 |
+
if os.path.exists(image_path) and os.path.exists(xml_path):
|
| 35 |
+
shutil.move(image_path, test_image_path)
|
| 36 |
+
shutil.move(xml_path, test_xml_path)
|
| 37 |
+
print(f"Moved: {xml_file} & {file_name}.jpg")
|
| 38 |
+
else:
|
| 39 |
+
print(f"Skipping: {xml_file} (Missing corresponding image)")
|
| 40 |
+
|
| 41 |
+
print("✅ 20% of files moved successfully!")
|