|
import os |
|
from PIL import Image |
|
from collections import Counter |
|
|
|
def analyze_images(directory): |
|
analysis_results = {} |
|
|
|
for root, dirs, files in os.walk(directory): |
|
if files: |
|
model_folder_name = os.path.basename(root) |
|
if model_folder_name not in analysis_results: |
|
analysis_results[model_folder_name] = { |
|
'image_count': 0, |
|
'total_size': 0, |
|
'resolutions': Counter() |
|
} |
|
|
|
for file in files: |
|
file_path = os.path.join(root, file) |
|
|
|
|
|
analysis_results[model_folder_name]['image_count'] += 1 |
|
|
|
|
|
try: |
|
with Image.open(file_path) as img: |
|
|
|
file_size = os.path.getsize(file_path) |
|
analysis_results[model_folder_name]['total_size'] += file_size |
|
|
|
|
|
width, height = img.size |
|
analysis_results[model_folder_name]['resolutions'][(width, height)] += 1 |
|
except Exception as e: |
|
print(f"Error reading file {file_path}: {e}") |
|
|
|
return analysis_results |
|
|
|
def print_and_log_analysis_results(analysis_results, dataset_name, log_file): |
|
|
|
max_model_length = max(len(model) for model in analysis_results.keys()) |
|
model_column_width = max(max_model_length, 20) |
|
|
|
|
|
image_count_width = 12 |
|
total_size_width = 14 |
|
resolution_width = 25 |
|
|
|
|
|
header = f"{'Model':<{model_column_width}} | {'Image Count':>{image_count_width}} | {'Total Size (MB)':>{total_size_width}} | {'Most Common Resolution':<{resolution_width}}" |
|
separator = "-" * (model_column_width + image_count_width + total_size_width + resolution_width + 7) |
|
|
|
result_lines = [] |
|
result_lines.append(f"Analysis for {dataset_name}:\n") |
|
result_lines.append(header + "\n") |
|
result_lines.append(separator + "\n") |
|
|
|
for model, data in analysis_results.items(): |
|
total_size_mb = data['total_size'] / (1024 * 1024) |
|
most_common_resolution = data['resolutions'].most_common(1) |
|
|
|
if most_common_resolution: |
|
common_res = f"{most_common_resolution[0][0][0]}x{most_common_resolution[0][0][1]} ({most_common_resolution[0][1]} images)" |
|
else: |
|
common_res = "None" |
|
|
|
result_lines.append(f"{model:<{model_column_width}} | {data['image_count']:>{image_count_width}} | {total_size_mb:>{total_size_width}.2f} | {common_res:<{resolution_width}}\n") |
|
|
|
result_lines.append("\n") |
|
|
|
|
|
for line in result_lines: |
|
print(line, end='') |
|
|
|
|
|
with open(log_file, 'a') as f: |
|
f.writelines(result_lines) |
|
|
|
def main(): |
|
|
|
generated_dir = 'resampledEvalSet' |
|
real_dir = 'real' |
|
log_file = 'analysis_results.txt' |
|
|
|
|
|
with open(log_file, 'w') as f: |
|
pass |
|
|
|
|
|
generated_analysis_results = analyze_images(generated_dir) |
|
print_and_log_analysis_results(generated_analysis_results, "Generated Images", log_file) |
|
|
|
|
|
real_analysis_results = analyze_images(real_dir) |
|
print_and_log_analysis_results(real_analysis_results, "Real Images", log_file) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|