|
|
import os |
|
|
import shutil |
|
|
import random |
|
|
|
|
|
from google.colab import drive |
|
|
drive.mount('/content/drive') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
source_images_dir = '/content/drive/My Drive/dataset/grechka' |
|
|
|
|
|
source_labels_dir = '/content/drive/My Drive/dataset/labels' |
|
|
|
|
|
|
|
|
destination_base_dir = '/content/drive/My Drive/yolo_grechka_dataset' |
|
|
|
|
|
|
|
|
train_ratio = 0.8 |
|
|
val_ratio = 0.15 |
|
|
test_ratio = 0.05 |
|
|
|
|
|
|
|
|
os.makedirs(os.path.join(destination_base_dir, 'images', 'train'), exist_ok=True) |
|
|
os.makedirs(os.path.join(destination_base_dir, 'images', 'val'), exist_ok=True) |
|
|
os.makedirs(os.path.join(destination_base_dir, 'images', 'test'), exist_ok=True) |
|
|
os.makedirs(os.path.join(destination_base_dir, 'labels', 'train'), exist_ok=True) |
|
|
os.makedirs(os.path.join(destination_base_dir, 'labels', 'val'), exist_ok=True) |
|
|
os.makedirs(os.path.join(destination_base_dir, 'labels', 'test'), exist_ok=True) |
|
|
|
|
|
|
|
|
|
|
|
image_files = [f for f in os.listdir(source_images_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.webp'))] |
|
|
|
|
|
|
|
|
random.seed(42) |
|
|
random.shuffle(image_files) |
|
|
|
|
|
|
|
|
total_files = len(image_files) |
|
|
train_count = int(total_files * train_ratio) |
|
|
val_count = int(total_files * val_ratio) |
|
|
test_count = total_files - train_count - val_count |
|
|
|
|
|
print(f"Всего изображений: {total_files}") |
|
|
print(f"Для тренировки: {train_count}") |
|
|
print(f"Для валидации: {val_count}") |
|
|
print(f"Для теста: {test_count}") |
|
|
|
|
|
|
|
|
current_index = 0 |
|
|
|
|
|
|
|
|
for i in range(train_count): |
|
|
img_file = image_files[current_index] |
|
|
base_name, ext = os.path.splitext(img_file) |
|
|
label_file = base_name + '.txt' |
|
|
|
|
|
|
|
|
shutil.copy(os.path.join(source_images_dir, img_file), os.path.join(destination_base_dir, 'images', 'train', img_file)) |
|
|
|
|
|
if os.path.exists(os.path.join(source_labels_dir, label_file)): |
|
|
shutil.copy(os.path.join(source_labels_dir, label_file), os.path.join(destination_base_dir, 'labels', 'train', label_file)) |
|
|
else: |
|
|
print(f"Внимание: Файл аннотации для {img_file} не найден в {source_labels_dir}. Изображение скопировано, но без метки.") |
|
|
|
|
|
|
|
|
current_index += 1 |
|
|
|
|
|
|
|
|
for i in range(val_count): |
|
|
img_file = image_files[current_index] |
|
|
base_name, ext = os.path.splitext(img_file) |
|
|
label_file = base_name + '.txt' |
|
|
|
|
|
shutil.copy(os.path.join(source_images_dir, img_file), os.path.join(destination_base_dir, 'images', 'val', img_file)) |
|
|
if os.path.exists(os.path.join(source_labels_dir, label_file)): |
|
|
shutil.copy(os.path.join(source_labels_dir, label_file), os.path.join(destination_base_dir, 'labels', 'val', label_file)) |
|
|
else: |
|
|
print(f"Внимание: Файл аннотации для {img_file} не найден в {source_labels_dir}. Изображение скопировано, но без метки.") |
|
|
|
|
|
current_index += 1 |
|
|
|
|
|
|
|
|
for i in range(test_count): |
|
|
img_file = image_files[current_index] |
|
|
base_name, ext = os.path.splitext(img_file) |
|
|
label_file = base_name + '.txt' |
|
|
|
|
|
shutil.copy(os.path.join(source_images_dir, img_file), os.path.join(destination_base_dir, 'images', 'test', img_file)) |
|
|
if os.path.exists(os.path.join(source_labels_dir, label_file)): |
|
|
shutil.copy(os.path.join(source_labels_dir, label_file), os.path.join(destination_base_dir, 'labels', 'test', label_file)) |
|
|
else: |
|
|
print(f"Внимание: Файл аннотации для {img_file} не найден в {source_labels_dir}. Изображение скопировано, но без метки.") |
|
|
|
|
|
current_index += 1 |
|
|
|
|
|
print("\nРазбиение датасета завершено.") |
|
|
print(f"Датасет в формате YOLO создан по пути: {destination_base_dir}") |
|
|
|
|
|
|
|
|
data_yaml_content = f""" |
|
|
train: ../images/train |
|
|
val: ../images/val |
|
|
test: ../images/test # Опционально, если вы разбили на тест |
|
|
|
|
|
nc: 1 # Укажите количество ваших классов |
|
|
names: ['impurity'] # Укажите названия ваших классов в списке |
|
|
""" |
|
|
|
|
|
data_yaml_path = os.path.join(destination_base_dir, 'data.yaml') |
|
|
with open(data_yaml_path, 'w') as f: |
|
|
f.write(data_yaml_content) |
|
|
|
|
|
print(f"Создан placeholder data.yaml файл: {data_yaml_path}") |
|
|
print("!!! ВАЖНО: Отредактируйте data.yaml файл, указав правильное количество и названия ваших классов. !!!") |