将测试图片网页展示

安装与准备

pip install dominate

html.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import dominate
from dominate.tags import meta, h3, table, tr, td, p, a, img, br
import os


class HTML:
"""This HTML class allows us to save images and write texts into a single HTML file.

It consists of functions such as <add_header> (add a text header to the HTML file),
<add_images> (add a row of images to the HTML file), and <save> (save the HTML to the disk).
It is based on Python library 'dominate', a Python library for creating and manipulating HTML documents using a DOM API.
"""

def __init__(self, web_dir, title, refresh=0):
"""Initialize the HTML classes

Parameters:
web_dir (str) -- a directory that stores the webpage. HTML file will be created at <web_dir>/index.html; images will be saved at <web_dir/images/
title (str) -- the webpage name
refresh (int) -- how often the website refresh itself; if 0; no refreshing
"""
self.title = title
self.web_dir = web_dir
self.img_dir = os.path.join(self.web_dir, 'images')
if not os.path.exists(self.web_dir):
os.makedirs(self.web_dir)
if not os.path.exists(self.img_dir):
os.makedirs(self.img_dir)

self.doc = dominate.document(title=title)
if refresh > 0:
with self.doc.head:
meta(http_equiv="refresh", content=str(refresh))

def get_image_dir(self):
"""Return the directory that stores images"""
return self.img_dir

def add_header(self, text):
"""Insert a header to the HTML file

Parameters:
text (str) -- the header text
"""
with self.doc:
h3(text)

def add_images(self, ims, txts, links, width=400):
"""add images to the HTML file

Parameters:
ims (str list) -- a list of image paths
txts (str list) -- a list of image names shown on the website
links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page
"""
self.t = table(border=1, style="table-layout: fixed;") # Insert a table
self.doc.add(self.t)
with self.t:
with tr():
for im, txt, link in zip(ims, txts, links):
with td(style="word-wrap: break-word;", halign="center", valign="top"):
with p():
with a(href=os.path.join('images', link)):
img(style="width:%dpx" % width, src=os.path.join('images', im))
br()
p(txt)

def save(self):
"""save the current content to the HMTL file"""
html_file = '%s/index.html' % self.web_dir
f = open(html_file, 'wt')
f.write(self.doc.render())
f.close()


if __name__ == '__main__': # we show an example usage here.
html = HTML('web/', 'test_html')
html.add_header('hello world')

ims, txts, links = [], [], []
for n in range(4):
ims.append('image_%d.png' % n)
txts.append('text_%d' % n)
links.append('image_%d.png' % n)
html.add_images(ims, txts, links)
html.save()

测试时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from utils import html
def save_web_images(self, webpage, data, images, epoch):
if self.cfg.model.type == "Baseline":
img_path_relative = (data["image_A_path"][0]).split("/")[-1]
save_path = self.results_path / img_path_relative
save_path.parent.mkdir(exist_ok=True, parents=True)
webpage.add_header(save_path.stem)
txts = ['input','enhanced','GT','changemap','mask']
ims, links = [], []
width = 450
str_epoch = '_'+str(epoch)
root = str(save_path.parent)+'/'+str(save_path.stem)+str_epoch+".png"
self.to_pil(self.denorm(data["image_A"][0])).save(root)
self.to_pil(self.denorm(images["fake_B"][0])).save(save_path.parent / f"{save_path.stem}_enhanced.png")
self.to_pil(self.denorm(images["latent"][0]/3)).save(save_path.parent / f"{save_path.stem}_latent.png")
self.to_pil(self.denorm(data["image_B"][0])).save(save_path.parent / f"{save_path.stem}_gt.png")
self.to_pil((data["input_mask"])[0]).save(save_path.parent / f"{save_path.stem}_mask.png")
ims.append(root)
ims.append(save_path.parent / f"{save_path.stem}_enhanced.png")
ims.append(save_path.parent / f"{save_path.stem}_gt.png")
ims.append(save_path.parent / f"{save_path.stem}_latent.png")
ims.append(save_path.parent / f"{save_path.stem}_mask.png")
webpage.add_images(ims, txts, ims, width=width)
else:
raise "Unknown visualisation config"
web_dir = 'where images folder'
webpage = html.HTML(web_dir, 'Experiment = %s, Epoch = %s' % (cfg.model.type, cfg.model.load_epoch))

for images in results
    visualizer.save_web_images(webpage, data, images, epoch)