| import os |
| from PIL import Image |
|
|
| |
| base_dir = 'data/chest_xray' |
| train_dir = os.path.join(base_dir, 'train') |
| val_dir = os.path.join(base_dir, 'val') |
|
|
|
|
| |
| def count_images(directory, category): |
| category_dir = os.path.join(directory, category) |
| count = 0 |
| for root, dirs, files in os.walk(category_dir): |
| count += len([f for f in files if f.endswith(('.jpg', '.jpeg', '.png'))]) |
| return count |
|
|
|
|
| |
| def check_corrupted_images(directory, category): |
| category_dir = os.path.join(directory, category) |
| corrupted_files = [] |
| for root, dirs, files in os.walk(category_dir): |
| for file in files: |
| if file.endswith(('.jpg', '.jpeg', '.png')): |
| try: |
| img = Image.open(os.path.join(root, file)) |
| img.verify() |
| except (IOError, SyntaxError) as e: |
| corrupted_files.append(os.path.join(root, file)) |
| return corrupted_files |
|
|
|
|
| |
| train_normal_count = count_images(train_dir, 'NORMAL') |
| train_pneumonia_count = count_images(train_dir, 'PNEUMONIA') |
| val_normal_count = count_images(val_dir, 'NORMAL') |
| val_pneumonia_count = count_images(val_dir, 'PNEUMONIA') |
|
|
| |
| train_normal_corrupted = check_corrupted_images(train_dir, 'NORMAL') |
| train_pneumonia_corrupted = check_corrupted_images(train_dir, 'PNEUMONIA') |
| val_normal_corrupted = check_corrupted_images(val_dir, 'NORMAL') |
| val_pneumonia_corrupted = check_corrupted_images(val_dir, 'PNEUMONIA') |
|
|
| |
| print(f"Training NORMAL images: {train_normal_count}") |
| print(f"Training PNEUMONIA images: {train_pneumonia_count}") |
| print(f"Validation NORMAL images: {val_normal_count}") |
| print(f"Validation PNEUMONIA images: {val_pneumonia_count}") |
|
|
| print(f"Corrupted images in training NORMAL: {train_normal_corrupted}") |
| print(f"Corrupted images in training PNEUMONIA: {train_pneumonia_corrupted}") |
| print(f"Corrupted images in validation NORMAL: {val_normal_corrupted}") |
| print(f"Corrupted images in validation PNEUMONIA: {val_pneumonia_corrupted}") |
|
|