| import os |
| import os.path as osp |
| import base64 |
| from jinja2 import Environment, BaseLoader |
| step = 4 |
| |
| caption_visualized_folders = [ |
| f"dataset/spotlight_sketch_cat/GT", |
| f"dataset/spotlight_sketch_cat/epoch0" |
| |
| |
| |
| |
| ] |
|
|
| |
| model_names = ["GT", "sktech"] |
|
|
| |
| IMG_EXT_TO_MIME = { |
| ".jpg": "image/jpeg", |
| ".jpeg": "image/jpeg", |
| ".png": "image/png", |
| ".bmp": "image/bmp", |
| ".tiff": "image/tiff", |
| ".webp": "image/webp" |
| } |
| IMG_EXTS = set(IMG_EXT_TO_MIME.keys()) |
|
|
| |
| all_basenames = set() |
| for folder in caption_visualized_folders: |
| folder_local = osp.join(os.getcwd(), folder) |
| if osp.isdir(folder_local): |
| for fname in os.listdir(folder_local): |
| ext = osp.splitext(fname)[1].lower() |
| if ext in IMG_EXTS: |
| all_basenames.add(osp.splitext(fname)[0]) |
| else: |
| print(f"Warning: folder not found: {folder_local}") |
|
|
| res_data = [] |
| for base in sorted(all_basenames): |
| |
| res_row_info = [base, base] |
|
|
| |
| for folder in caption_visualized_folders: |
| base64_img = None |
| img_filename = None |
| folder_local = osp.join(os.getcwd(), folder) |
| |
| if osp.isdir(folder_local): |
| |
| found = False |
| for ext in [".jpg", ".jpeg", ".png", ".bmp", ".tiff", ".webp"]: |
| candidate = osp.join(folder_local, base + ext) |
| if osp.isfile(candidate): |
| img_filename = base + ext |
| |
| with open(candidate, "rb") as f: |
| img_data = f.read() |
| mime_type = IMG_EXT_TO_MIME[ext] |
| |
| base64_img = f"data:{mime_type};base64,{base64.b64encode(img_data).decode('utf-8')}" |
| found = True |
| break |
| |
| if not found: |
| for fname in sorted(os.listdir(folder_local)): |
| fname_base, fname_ext = osp.splitext(fname) |
| fname_ext_lower = fname_ext.lower() |
| if fname_base == base and fname_ext_lower in IMG_EXTS: |
| img_filename = fname |
| candidate = osp.join(folder_local, fname) |
| with open(candidate, "rb") as f: |
| img_data = f.read() |
| mime_type = IMG_EXT_TO_MIME[fname_ext_lower] |
| base64_img = f"data:{mime_type};base64,{base64.b64encode(img_data).decode('utf-8')}" |
| found = True |
| break |
| |
| res_row_info.append({ |
| "base64": base64_img, |
| "filename": img_filename |
| }) |
|
|
| res_data.append(res_row_info) |
|
|
| |
| template_string = """ |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>Images</title> |
| <meta charset="utf-8"> |
| <style> |
| img { max-width: 320px; height: auto; display:block; margin:6px auto } |
| td { vertical-align: top; text-align: center } |
| table { width: 100%; border-collapse: collapse } |
| th, td { border: 1px solid #ddd; padding: 8px } |
| th { background: #f4f4f4 } |
| .fname { font-size: 12px; color:#555; word-break:break-all; margin-top: 4px } |
| </style> |
| </head> |
| <body> |
| <h2>epoch0-1</h2> |
| <table> |
| <thead> |
| <tr> |
| <th>#</th> |
| <th>GT</th> |
| <th>sketch</th> |
| </tr> |
| </thead> |
| <tbody> |
| {% for row in data %} |
| <tr> |
| <td>{{ loop.index }}</td> |
| {% for img_info in row[2:4] %} {# 要改 #} |
| <td> |
| {% if img_info.base64 %} |
| <img src="{{ img_info.base64 }}" alt="img"> |
| <div class="fname">{{ img_info.filename or '未知文件' }}</div> |
| {% else %} |
| <div class="fname">(无图)</div> |
| {% endif %} |
| </td> |
| {% endfor %} |
| </tr> |
| {% endfor %} |
| </tbody> |
| </table> |
| </body> |
| </html> |
| """ |
|
|
| |
| template = Environment(loader=BaseLoader).from_string(template_string) |
|
|
| |
| output_html_path = f"showcase1203.html" |
| with open(output_html_path, "w", encoding='utf-8') as f: |
| f.write(template.render(data=res_data, model_names=model_names)) |
|
|
| print(f"Base64版HTML文件已保存为 {output_html_path}") |