import os import random image_dir = "litter_detection/negative/images" target_count = 2000 if not os.path.exists(image_dir): print(f"Directory not found: {image_dir}") exit(1) images = [f for f in os.listdir(image_dir) if os.path.isfile(os.path.join(image_dir, f))] current_count = len(images) #calculate how many to delete to_delete = current_count - target_count #randomly select images to delete images_to_delete = random.sample(images, to_delete) # delete images for i, img in enumerate(images_to_delete, 1): filepath = os.path.join(image_dir, img) os.remove(filepath) if i % 1000 == 0: print(f"Deleted {i}/{to_delete}") print(f"Deleted {to_delete}. Remaining: {target_count}")