|
|
import os |
|
|
import random |
|
|
import shutil |
|
|
|
|
|
|
|
|
images_folder = "images_folder" |
|
|
xml_folder = "xml_folder" |
|
|
test_images_folder = "test_images" |
|
|
test_xml_folder = "test_xml" |
|
|
|
|
|
|
|
|
os.makedirs(test_images_folder, exist_ok=True) |
|
|
os.makedirs(test_xml_folder, exist_ok=True) |
|
|
|
|
|
|
|
|
xml_files = [f for f in os.listdir(xml_folder) if f.endswith(".xml")] |
|
|
|
|
|
|
|
|
num_to_move = int(len(xml_files) * 0.2) |
|
|
selected_files = random.sample(xml_files, num_to_move) |
|
|
|
|
|
|
|
|
for xml_file in selected_files: |
|
|
|
|
|
file_name = os.path.splitext(xml_file)[0] |
|
|
image_path = os.path.join(images_folder, file_name + ".jpg") |
|
|
xml_path = os.path.join(xml_folder, xml_file) |
|
|
|
|
|
|
|
|
test_image_path = os.path.join(test_images_folder, file_name + ".jpg") |
|
|
test_xml_path = os.path.join(test_xml_folder, xml_file) |
|
|
|
|
|
|
|
|
if os.path.exists(image_path) and os.path.exists(xml_path): |
|
|
shutil.move(image_path, test_image_path) |
|
|
shutil.move(xml_path, test_xml_path) |
|
|
print(f"Moved: {xml_file} & {file_name}.jpg") |
|
|
else: |
|
|
print(f"Skipping: {xml_file} (Missing corresponding image)") |
|
|
|
|
|
print("✅ 20% of files moved successfully!") |
|
|
|