|
|
| __author__ = 'Andreas Sjolander, Gemini'
|
| __version__ = ['1.0']
|
| __version_date__ = '2025-11-25'
|
| __maintainer__ = 'Andreas Sjolander'
|
| __github__ = 'andreassjolander'
|
| __email__ = 'asjola@kth.se'
|
|
|
| """
|
| 1c_create_classification.py
|
| this scripts reads the csv files that contain information about images with and
|
| withou cracks. Based on this, three classification datasets are created in the
|
| folder "3_classification", i.e. TA, TB and TC. Each folder contains the
|
| subfolder "crack" and "no_crack"
|
| """
|
|
|
|
|
|
|
|
|
| import os
|
| import shutil
|
| import pandas as pd
|
| import sys
|
|
|
|
|
|
|
|
|
|
|
| project_root = os.getcwd()
|
|
|
|
|
|
|
| input_csv_folder = os.path.join(project_root, "../2_model_input")
|
|
|
| source_img_folder = os.path.join(project_root, "../3_img")
|
|
|
| output_base_folder = os.path.join(project_root, "../3_classification")
|
|
|
|
|
|
|
|
|
|
|
| def sort_classification_data():
|
| print(f"--- Starting Classification Sorting ---")
|
| print(f"Root Directory: {project_root}")
|
| print(f"Source Images : {source_img_folder}")
|
| print(f"Input CSVs : {input_csv_folder}")
|
|
|
|
|
| datasets = ["TA", "TB", "TC"]
|
|
|
| for dataset in datasets:
|
| print(f"\nProcessing Dataset: {dataset}...")
|
|
|
|
|
| csv_file = f"{dataset}_dataset_labels.csv"
|
| csv_path = os.path.join(input_csv_folder, csv_file)
|
|
|
|
|
| if not os.path.exists(csv_path):
|
| print(f" [WARNING] CSV not found: {csv_path}. Skipping.")
|
| continue
|
|
|
|
|
| try:
|
| df = pd.read_csv(csv_path)
|
| except Exception as e:
|
| print(f" [Error] Could not read CSV: {e}")
|
| continue
|
|
|
|
|
| count_crack = 0
|
| count_no_crack = 0
|
| count_missing = 0
|
|
|
|
|
| for index, row in df.iterrows():
|
|
|
|
|
| raw_path = str(row['filename'])
|
| filename = os.path.basename(raw_path)
|
|
|
|
|
| label = str(row['label']).strip().lower()
|
|
|
|
|
|
|
| src_path = os.path.join(source_img_folder, filename)
|
|
|
|
|
|
|
| dest_dir = os.path.join(output_base_folder, dataset, label)
|
| dest_path = os.path.join(dest_dir, filename)
|
|
|
|
|
| if os.path.exists(src_path):
|
|
|
| os.makedirs(dest_dir, exist_ok=True)
|
|
|
| shutil.copy2(src_path, dest_path)
|
|
|
| if "no_crack" in label:
|
| count_no_crack += 1
|
| else:
|
| count_crack += 1
|
| else:
|
|
|
| if count_missing < 5:
|
| print(f" [Missing] Could not find image: {src_path}")
|
| count_missing += 1
|
|
|
| print(f" Summary for {dataset}:")
|
| print(f" - Cracks copied : {count_crack}")
|
| print(f" - No Cracks copied: {count_no_crack}")
|
| if count_missing > 0:
|
| print(f" - Missing images : {count_missing} (Check filenames or source folder)")
|
|
|
| print("\n--- Processing Complete ---")
|
|
|
| if __name__ == "__main__":
|
| sort_classification_data() |