import os import glob import base64 from collections import defaultdict def get_base64_image(image_path): with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()).decode('utf-8') ext = os.path.splitext(image_path)[1].lower().replace('.', '') if ext == 'jpg': ext = 'jpeg' return f"data:image/{ext};base64,{encoded_string}" def generate_report(): directory = r"E:\University\LEVEL 4\Graduation Project\Damage Detection\small_Mod\Apartments\Apartment_01\Before\segmented_objects_b" image_files = glob.glob(os.path.join(directory, "*.*")) image_files = [f for f in image_files if f.lower().endswith(('.jpg', '.png', '.jpeg')) and not f.endswith('collage.jpg')] objects = [] class_counts = defaultdict(int) for filepath in image_files: filename = os.path.basename(filepath) name_parts = os.path.splitext(filename)[0].split('-') # Parse class and ID if len(name_parts) >= 2: obj_class = name_parts[0].capitalize().replace('_', ' ') obj_id = name_parts[1] else: obj_class = "Unknown" obj_id = "N/A" class_counts[obj_class] += 1 objects.append({ 'class': obj_class, 'id': obj_id, 'filename': filename, 'path': filepath, 'status': 'Undamaged', # As verified previously 'b64_image': get_base64_image(filepath) }) # Sort objects by class then ID objects.sort(key=lambda x: (x['class'], x['id'])) total_objects = len(objects) damaged_objects = 0 # HTML Template html_content = f""" Apartment 01 - Damage Detection Report

Damage Detection Report

Apartment 01 - "Before" State Analysis

Total Objects

{total_objects}

Intact / Undamaged

{total_objects - damaged_objects}

Damaged

{damaged_objects}

Object Inventory & Status

""" for obj in objects: badge_class = "badge damaged" if obj['status'] == 'Damaged' else "badge" html_content += f"""
{obj['class']} {obj['id']}

{obj['class']}

ID: #{obj['id']}
{obj['status']}
""" html_content += """
""" output_path = r"E:\University\LEVEL 4\Graduation Project\Damage Detection\small_Mod\Apartments\Apartment_01\Before\Damage_Report.html" with open(output_path, "w", encoding="utf-8") as f: f.write(html_content) print(f"HTML Report successfully generated at: {output_path}") if __name__ == "__main__": generate_report()