-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquality_curves.py
174 lines (127 loc) · 4.76 KB
/
quality_curves.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os, sys
os.environ['OPENCV_IO_ENABLE_JASPER']='True'
import cv2
import hydra
from matplotlib import pyplot as plt
from PIL import Image
import numpy as np
from omegaconf import DictConfig, OmegaConf
from pathlib import Path
import time
from ..data import load_img
#import os, sys
#sys.path.append(os.path.abspath('../implicit_image'))
from data import load_img
import json
def _plot_image_dict(img_dict, x_key: str, y_key: str, path: Path):
_label_dict = {"psnr": "PSNR (in dB)", "quality": "Quality", "size": "Size (in KB)"}
# plt.figure(figsize=(8, 8))
for img_name in img_dict:
plt.plot(
img_dict[img_name][x_key],
img_dict[img_name][y_key],
linestyle="--",
marker="o",
)
plt.xlabel(_label_dict[x_key])
plt.ylabel(_label_dict[y_key])
# plt.title(f"JPEG {y_key.capitalize()} vs {x_key.capitalize()}")
plt.legend(img_dict.keys())
plt.grid()
plt.tight_layout()
plt.savefig(
path / f"jpeg_{y_key}_vs_{x_key}.pdf",
dpi=150,
)
plt.show()
@hydra.main(config_name="config", config_path="../../conf")
def main(cfg: DictConfig):
jpeg_plot(cfg, "flower_foveon.ppm")
#jpeg_2000_plot(cfg,"flower_foveon.ppm")
#webp_plot(cfg, "flower_foveon.ppm")
def jpeg_2000_plot(cfg, img_name):
# Initialize directories
path = Path(f"{hydra.utils.get_original_cwd()}/outputs/plots/")
path.mkdir(exist_ok=True, parents=True)
dump_path = Path("/tmp/jpeg_dump")
dump_path.mkdir(exist_ok=True, parents=True)
single_cfg = cfg.copy()
single_cfg.img.path = str(Path(single_cfg.img.path).parent / img_name)
img = load_img(**single_cfg.img) # H x W x 3
img = (img * 255.0).numpy()[:, :, ::-1]
#specifies the sizes the jpg2000 will compress to
rates = np.linspace(100, 300, 20)
img_dict = {}
psnr_ll = []
size_ll = []
for rate in rates:
print("rate: " + str(rate))
print(type(img))
image_pil = Image.fromarray(img,"RGB")
image_pil.save("test2.jpg")
image_pil.save('test_name.jp2', quality_mode = "rates", quality_layers = [rate])
decimg = Image.open('test_name.jp2')
r, g, b = decimg.split()
decimg = Image.merge("RGB", (r, g, b))
data = np.asarray(decimg)
dump_file = dump_path / f"{img_name}_quality_{rate}.jp2"
cv2.imwrite(str(dump_file), data)
psnr = 10 * np.log10(255 ** 2 / ((data - img) ** 2).mean())
psnr_ll.append(psnr)
print(psnr)
time.sleep(0.2)
size = dump_file.stat().st_size
size_ll.append(size // 1024)
print(size)
img_dict[img_name] = {"psnr": psnr_ll, "size": size_ll}
# Convert to list
for name in img_dict:
for metric, metric_ll in img_dict[name].items():
if isinstance(metric_ll, np.ndarray):
img_dict[name][metric] = metric_ll.tolist()
with open(path.parent / "csv" / "jpeg_dump.json", "w") as f:
json.dump(img_dict, f, indent=4, sort_keys=True)
def webp_plot(cfg, img_name):
# Initialize directories
path = Path(f"{hydra.utils.get_original_cwd()}/outputs/plots/")
path.mkdir(exist_ok=True, parents=True)
dump_path = Path("/tmp/jpeg_dump")
dump_path.mkdir(exist_ok=True, parents=True)
single_cfg = cfg.copy()
single_cfg.img.path = str(Path(single_cfg.img.path).parent / img_name)
img = load_img(**single_cfg.img) # H x W x 3
img = (img * 255.0).numpy()[:, :, ::-1]
#specifies the sizes the jpg2000 will compress to
rates = np.linspace(1, 100, 20)
img_dict = {}
psnr_ll = []
size_ll = []
for rate in rates:
print("rate: " + str(rate))
print(type(img))
image_pil = Image.fromarray(img, "RGB")
image_pil.save('test_name.webp', quality = rate)
#image_pil.save("test.jpg")
decimg = Image.open('test_name.webp')
r, g, b = decimg.split()
decimg = Image.merge("RGB", (r, g, b))
data = np.asarray(decimg)
dump_file = dump_path / f"{img_name}_quality_{rate}.webp"
cv2.imwrite(str(dump_file), data)
psnr = 10 * np.log10(255 ** 2 / ((data - img) ** 2).mean())
psnr_ll.append(psnr)
print(psnr)
time.sleep(0.2)
size = dump_file.stat().st_size
size_ll.append(size // 1024)
print(size)
img_dict[img_name] = {"psnr": psnr_ll, "size": size_ll}
# Convert to list
for name in img_dict:
for metric, metric_ll in img_dict[name].items():
if isinstance(metric_ll, np.ndarray):
img_dict[name][metric] = metric_ll.tolist()
with open(path.parent / "csv" / "jpeg_dump.json", "w") as f:
json.dump(img_dict, f, indent=4, sort_keys=True)
if __name__ == "__main__":
main()