|
a |
|
b/visualizer.py |
|
|
1 |
import numpy as np |
|
|
2 |
import torch |
|
|
3 |
|
|
|
4 |
import os |
|
|
5 |
import io |
|
|
6 |
import base64 |
|
|
7 |
|
|
|
8 |
import numpy as np |
|
|
9 |
import pandas as pd |
|
|
10 |
import cv2 |
|
|
11 |
import matplotlib.pyplot as plt |
|
|
12 |
import matplotlib.colors as colors |
|
|
13 |
|
|
|
14 |
import time |
|
|
15 |
from IPython.display import clear_output |
|
|
16 |
from IPython.display import HTML |
|
|
17 |
|
|
|
18 |
|
|
|
19 |
def show_data_augmentations(img_tensor: torch.Tensor, |
|
|
20 |
mask_tensor: torch.Tensor, |
|
|
21 |
mean: tuple = (0.485, 0.456, 0.406), |
|
|
22 |
std: tuple = (0.229, 0.224, 0.225), |
|
|
23 |
labels: list=["image", "lung", "heart", "trachea"]): |
|
|
24 |
|
|
|
25 |
img = img_tensor.numpy().transpose(1, 2, 0) |
|
|
26 |
img = (img * std + mean).astype("float32") |
|
|
27 |
img = np.clip(img, 0, 1) |
|
|
28 |
mask = mask_tensor.numpy().transpose(1, 2, 0) |
|
|
29 |
data_to_plot = [img, *[mask[:,:, i] for i in range(3)]] |
|
|
30 |
|
|
|
31 |
fig, axes = plt.subplots(nrows=1, ncols=4, figsize=(15, 8)) |
|
|
32 |
for i, ax in enumerate(axes): |
|
|
33 |
ax.imshow(data_to_plot[i]) |
|
|
34 |
ax.set_title(labels[i]) |
|
|
35 |
|
|
|
36 |
plt.show() |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
def show_video(video_path: str): |
|
|
40 |
""" |
|
|
41 |
show video in jupyter notebook, agent interaction in environment. |
|
|
42 |
Takes - path to video file. |
|
|
43 |
Returns - html video player in jupyter notebook. |
|
|
44 |
""" |
|
|
45 |
video = io.open(video_path, 'r+b').read() |
|
|
46 |
encoded = base64.b64encode(video) |
|
|
47 |
|
|
|
48 |
return HTML(data='''<video alt="test" controls> |
|
|
49 |
<source src="data:video/mp4;base64,{0}" type="video/mp4" /> </video>''' |
|
|
50 |
.format(encoded.decode('ascii'))) |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
def get_color_info(classes: list = ['lung', 'heart', 'trachea']): |
|
|
54 |
|
|
|
55 |
def get_color(cmap): |
|
|
56 |
new_cmap = colors.LinearSegmentedColormap.from_list( |
|
|
57 |
(cmap.name, 0.0, 0.0,), |
|
|
58 |
cmap(np.linspace(0.0, 0.0, 100)) |
|
|
59 |
) |
|
|
60 |
return new_cmap |
|
|
61 |
|
|
|
62 |
colormaps = [plt.get_cmap(cmap_name) for cmap_name in |
|
|
63 |
['cool', 'autumn', 'autumn_r'] |
|
|
64 |
] |
|
|
65 |
arr = np.linspace(0, 50, 100).reshape((10, 10)) |
|
|
66 |
fig, ax = plt.subplots(1, 3, figsize=(15, 5)) |
|
|
67 |
for i in range(3): |
|
|
68 |
cmap = get_color(colormaps[i]) |
|
|
69 |
ax[i].axis('off') |
|
|
70 |
ax[i].imshow(arr, cmap=cmap) |
|
|
71 |
ax[i].set_title(classes[i], fontsize=15) |
|
|
72 |
|
|
|
73 |
fig.suptitle("Color definition", fontsize=20, y=0.99) |
|
|
74 |
fig.savefig(f"color_definition.png", bbox_inches='tight', |
|
|
75 |
pad_inches=0.2, dpi=100, format="png") |
|
|
76 |
|
|
|
77 |
plt.savefig(f"color_definition.svg", bbox_inches='tight', |
|
|
78 |
pad_inches=0.2, dpi=100, format="svg") |
|
|
79 |
plt.show() |