-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchiadulieu_test.py
More file actions
28 lines (22 loc) · 1.03 KB
/
chiadulieu_test.py
File metadata and controls
28 lines (22 loc) · 1.03 KB
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
import os
import shutil
import random
# Đường dẫn thư mục test và thư mục lưu ảnh mẫu
test_folder = "test"
output_folder = "data_test/data_test3"
# Tạo thư mục lưu 10 ảnh mẫu
os.makedirs(output_folder, exist_ok=True)
# Lấy danh sách các thư mục con (tên thư mục là tên class)
class_folders = [f for f in os.listdir(test_folder) if os.path.isdir(os.path.join(test_folder, f))]
selected_images = []
# Duyệt qua từng class và lấy một ảnh ngẫu nhiên
for class_name in class_folders:
class_path = os.path.join(test_folder, class_name)
images = [img for img in os.listdir(class_path) if img.lower().endswith((".png", ".jpg", ".jpeg"))]
if images:
selected_image = random.choice(images) # Chọn 1 ảnh ngẫu nhiên
src = os.path.join(class_path, selected_image)
dst = os.path.join(output_folder, f"{class_name}_{selected_image}")
shutil.copy(src, dst)
selected_images.append(dst)
print(f"Đã lưu 10 ảnh mẫu vào thư mục: {output_folder}")