|
|
|
|
|
|
|
|
import os
|
|
|
import pandas as pd
|
|
|
import struct
|
|
|
|
|
|
|
|
|
|
|
|
SOURCE_DATA_DIR = os.path.join(
|
|
|
'data',
|
|
|
'Nuclear Cataract Database for Biomedical and Machine Learning Applications',
|
|
|
'Nuclear Cataract Dataset'
|
|
|
)
|
|
|
OUTPUT_CSV_PATH = 'labels.csv'
|
|
|
|
|
|
|
|
|
all_image_data = []
|
|
|
|
|
|
print(f"Memindai folder '{SOURCE_DATA_DIR}'...")
|
|
|
|
|
|
for patient_id in sorted(os.listdir(SOURCE_DATA_DIR)):
|
|
|
patient_path = os.path.join(SOURCE_DATA_DIR, patient_id)
|
|
|
if not os.path.isdir(patient_path): continue
|
|
|
|
|
|
for eye_folder in os.listdir(patient_path):
|
|
|
eye_path = os.path.join(patient_path, eye_folder)
|
|
|
if not os.path.isdir(eye_path): continue
|
|
|
|
|
|
datafile_path = os.path.join(eye_path, 'DATAFILE')
|
|
|
|
|
|
|
|
|
if not os.path.exists(datafile_path):
|
|
|
print(f"Peringatan: Tidak ada DATAFILE di folder {eye_path}")
|
|
|
continue
|
|
|
|
|
|
try:
|
|
|
with open(datafile_path, 'rb') as f:
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
|
|
|
|
possible_grades = [g[0] for g in struct.iter_unpack('<d', content)]
|
|
|
|
|
|
|
|
|
valid_grades = [round(g) for g in possible_grades if round(g) != 0]
|
|
|
|
|
|
if not valid_grades:
|
|
|
|
|
|
print(f"Peringatan: Tidak ditemukan grade valid di {datafile_path}")
|
|
|
continue
|
|
|
|
|
|
|
|
|
grade = max(valid_grades)
|
|
|
|
|
|
|
|
|
for filename in os.listdir(eye_path):
|
|
|
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
|
|
|
relative_path = os.path.join(patient_id, eye_folder, filename).replace('\\', '/')
|
|
|
print(f"Ditemukan: file='{relative_path}', grade={grade}")
|
|
|
all_image_data.append({'image': relative_path, 'grade': grade})
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
print(f"Error saat memproses {datafile_path}: {e}. Melanjutkan...")
|
|
|
continue
|
|
|
|
|
|
print(f"\nTotal {len(all_image_data)} gambar ditemukan dan diberi label.")
|
|
|
|
|
|
|
|
|
if all_image_data:
|
|
|
df = pd.DataFrame(all_image_data)
|
|
|
df.to_csv(OUTPUT_CSV_PATH, index=False)
|
|
|
print(f"File '{OUTPUT_CSV_PATH}' yang sudah terisi lengkap berhasil dibuat.")
|
|
|
else:
|
|
|
print("Tidak ada data valid yang ditemukan untuk dibuat menjadi file CSV.")
|
|
|
|
|
|
print(f"\n--- Selesai ---") |