IliaLarchenko
commited on
Commit
•
17d778a
1
Parent(s):
590bb1b
fixed Professional mode
Browse files- requirements.txt +1 -1
- src/app.py +37 -30
- src/control.py +12 -12
- src/utils.py +12 -0
- src/visuals.py +24 -11
requirements.txt
CHANGED
@@ -61,7 +61,7 @@ s3transfer==0.2.1
|
|
61 |
scikit-image==0.16.2
|
62 |
scipy==1.3.2
|
63 |
six==1.13.0
|
64 |
-
streamlit==0.
|
65 |
toml==0.10.0
|
66 |
toolz==0.10.0
|
67 |
tornado==5.1.1
|
|
|
61 |
scikit-image==0.16.2
|
62 |
scipy==1.3.2
|
63 |
six==1.13.0
|
64 |
+
streamlit==0.52.2
|
65 |
toml==0.10.0
|
66 |
toolz==0.10.0
|
67 |
tornado==5.1.1
|
src/app.py
CHANGED
@@ -21,13 +21,12 @@ else:
|
|
21 |
['Simple', 'Professional'])
|
22 |
|
23 |
# select image
|
24 |
-
status, image = select_image(path_to_images)
|
25 |
if status == 0:
|
26 |
-
st.title("Can't load image
|
|
|
|
|
27 |
else:
|
28 |
-
# show title
|
29 |
-
st.title("Demo of Albumentations")
|
30 |
-
|
31 |
placeholder_params = {
|
32 |
"image_width": image.shape[1],
|
33 |
"image_height": image.shape[0],
|
@@ -57,37 +56,45 @@ else:
|
|
57 |
))
|
58 |
transform_names = transform_names[:-1]
|
59 |
|
60 |
-
original_image = image.copy()
|
61 |
transforms = []
|
62 |
-
for transform_name in transform_names:
|
63 |
-
|
64 |
# select the params values
|
65 |
-
st.sidebar.subheader("Params of the
|
66 |
-
param_values = show_transform_control(augmentations[transform_name])
|
|
|
67 |
|
|
|
68 |
# apply the transformation to the image
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
|
72 |
-
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
# st.write(applied_params)
|
79 |
-
# st.write(data["replay"])
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
width_original / image.shape[1] * augmented_image.shape[1]
|
84 |
-
)
|
85 |
|
86 |
-
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
21 |
['Simple', 'Professional'])
|
22 |
|
23 |
# select image
|
24 |
+
status, image = select_image(path_to_images, interface_type)
|
25 |
if status == 0:
|
26 |
+
st.title("Can't load image")
|
27 |
+
if status == 2:
|
28 |
+
st.title("Please, upload the image")
|
29 |
else:
|
|
|
|
|
|
|
30 |
placeholder_params = {
|
31 |
"image_width": image.shape[1],
|
32 |
"image_height": image.shape[0],
|
|
|
56 |
))
|
57 |
transform_names = transform_names[:-1]
|
58 |
|
|
|
59 |
transforms = []
|
60 |
+
for i, transform_name in enumerate(transform_names):
|
|
|
61 |
# select the params values
|
62 |
+
st.sidebar.subheader("Params of the " + transform_name)
|
63 |
+
param_values = show_transform_control(augmentations[transform_name], i)
|
64 |
+
transforms.append(getattr(A, transform_name)(**param_values))
|
65 |
|
66 |
+
try:
|
67 |
# apply the transformation to the image
|
68 |
+
data = A.ReplayCompose(transforms)(image=image)
|
69 |
+
error = 0
|
70 |
+
except ValueError:
|
71 |
+
error = 1
|
72 |
+
st.title("The error has occurred. Most probably you have passed wrong set of parameters. \
|
73 |
+
Check transforms that change the shape of image.")
|
74 |
|
75 |
+
if error == 0:
|
76 |
+
augmented_image = data["image"]
|
77 |
+
# show title
|
78 |
+
st.title("Demo of Albumentations")
|
79 |
|
80 |
+
# show the images
|
81 |
+
width_transformed = int(
|
82 |
+
width_original / image.shape[1] * augmented_image.shape[1]
|
83 |
+
)
|
|
|
|
|
84 |
|
85 |
+
st.image(image, caption="Original image", width=width_original)
|
86 |
+
st.image(augmented_image, caption="Transformed image", width=width_transformed)
|
|
|
|
|
87 |
|
88 |
+
# random values used to get transformations
|
89 |
+
if interface_type == 'Professional':
|
90 |
+
st.subheader("Random params used")
|
91 |
+
random_values = {}
|
92 |
+
for applied_params in data["replay"]["transforms"]:
|
93 |
+
random_values[applied_params['__class_fullname__'].split('.')[-1]] = applied_params['params']
|
94 |
+
st.write(random_values)
|
95 |
|
96 |
+
# print additional info
|
97 |
+
for transform in transforms:
|
98 |
+
show_docstring(transform)
|
99 |
+
st.code(str(transform))
|
100 |
+
show_credentials()
|
src/control.py
CHANGED
@@ -3,16 +3,16 @@ import streamlit as st
|
|
3 |
# TODO: rename and refactor everything
|
4 |
|
5 |
|
6 |
-
def select_num_interval(param_name: str, limits_list: list, defaults, **kwargs):
|
7 |
st.sidebar.subheader(param_name)
|
8 |
min_max_interval = st.sidebar.slider(
|
9 |
-
"", limits_list[0], limits_list[1], defaults, key=hash(param_name)
|
10 |
)
|
11 |
return min_max_interval
|
12 |
|
13 |
|
14 |
def select_several_nums(
|
15 |
-
param_name, subparam_names, limits_list, defaults_list, **kwargs
|
16 |
):
|
17 |
st.sidebar.subheader(param_name)
|
18 |
result = []
|
@@ -22,16 +22,16 @@ def select_several_nums(
|
|
22 |
for name, limits, defaults in zip(subparam_names, limits_list, defaults_list):
|
23 |
result.append(
|
24 |
st.sidebar.slider(
|
25 |
-
name, limits[0], limits[1], defaults, key=hash(param_name + name)
|
26 |
)
|
27 |
)
|
28 |
return tuple(result)
|
29 |
|
30 |
|
31 |
-
def select_min_max(param_name, limits_list, defaults_list, min_diff=0,
|
32 |
assert len(param_name) == 2
|
33 |
result = list(
|
34 |
-
select_num_interval(" & ".join(param_name), limits_list, defaults_list)
|
35 |
)
|
36 |
if result[1] - result[0] < min_diff:
|
37 |
diff = min_diff - result[1] + result[0]
|
@@ -44,12 +44,12 @@ def select_min_max(param_name, limits_list, defaults_list, min_diff=0, **kwargs)
|
|
44 |
return tuple(result)
|
45 |
|
46 |
|
47 |
-
def select_RGB(param_name, **kwargs):
|
48 |
result = select_several_nums(
|
49 |
param_name,
|
50 |
subparam_names=["Red", "Green", "Blue"],
|
51 |
limits_list=[[0, 255], [0, 255], [0, 255]],
|
52 |
-
defaults_list=[0, 0, 0],
|
53 |
)
|
54 |
return tuple(result)
|
55 |
|
@@ -61,15 +61,15 @@ def replace_none(string):
|
|
61 |
return string
|
62 |
|
63 |
|
64 |
-
def select_radio(param_name, options_list, **kwargs):
|
65 |
st.sidebar.subheader(param_name)
|
66 |
-
result = st.sidebar.radio("", options_list, key=hash(param_name))
|
67 |
return replace_none(result)
|
68 |
|
69 |
|
70 |
-
def select_checkbox(param_name, defaults, **kwargs):
|
71 |
st.sidebar.subheader(param_name)
|
72 |
-
result = st.sidebar.checkbox("True", defaults, key=hash(param_name))
|
73 |
return result
|
74 |
|
75 |
|
|
|
3 |
# TODO: rename and refactor everything
|
4 |
|
5 |
|
6 |
+
def select_num_interval(param_name: str, limits_list: list, defaults, n_for_hash, **kwargs):
|
7 |
st.sidebar.subheader(param_name)
|
8 |
min_max_interval = st.sidebar.slider(
|
9 |
+
"", limits_list[0], limits_list[1], defaults, key=hash(param_name + str(n_for_hash))
|
10 |
)
|
11 |
return min_max_interval
|
12 |
|
13 |
|
14 |
def select_several_nums(
|
15 |
+
param_name, subparam_names, limits_list, defaults_list, n_for_hash, **kwargs
|
16 |
):
|
17 |
st.sidebar.subheader(param_name)
|
18 |
result = []
|
|
|
22 |
for name, limits, defaults in zip(subparam_names, limits_list, defaults_list):
|
23 |
result.append(
|
24 |
st.sidebar.slider(
|
25 |
+
name, limits[0], limits[1], defaults, key=hash(param_name + name + str(n_for_hash))
|
26 |
)
|
27 |
)
|
28 |
return tuple(result)
|
29 |
|
30 |
|
31 |
+
def select_min_max(param_name, limits_list, defaults_list, n_for_hash, min_diff=0, **kwargs):
|
32 |
assert len(param_name) == 2
|
33 |
result = list(
|
34 |
+
select_num_interval(" & ".join(param_name), limits_list, defaults_list, n_for_hash)
|
35 |
)
|
36 |
if result[1] - result[0] < min_diff:
|
37 |
diff = min_diff - result[1] + result[0]
|
|
|
44 |
return tuple(result)
|
45 |
|
46 |
|
47 |
+
def select_RGB(param_name, n_for_hash, **kwargs):
|
48 |
result = select_several_nums(
|
49 |
param_name,
|
50 |
subparam_names=["Red", "Green", "Blue"],
|
51 |
limits_list=[[0, 255], [0, 255], [0, 255]],
|
52 |
+
defaults_list=[0, 0, 0], n_for_hash = n_for_hash
|
53 |
)
|
54 |
return tuple(result)
|
55 |
|
|
|
61 |
return string
|
62 |
|
63 |
|
64 |
+
def select_radio(param_name, options_list, n_for_hash, **kwargs):
|
65 |
st.sidebar.subheader(param_name)
|
66 |
+
result = st.sidebar.radio("", options_list, key=hash(param_name + str(n_for_hash)))
|
67 |
return replace_none(result)
|
68 |
|
69 |
|
70 |
+
def select_checkbox(param_name, defaults, n_for_hash, **kwargs):
|
71 |
st.sidebar.subheader(param_name)
|
72 |
+
result = st.sidebar.checkbox("True", defaults, key=hash(param_name + str(n_for_hash)))
|
73 |
return result
|
74 |
|
75 |
|
src/utils.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import cv2
|
2 |
import os
|
|
|
3 |
import json
|
4 |
import argparse
|
5 |
|
@@ -43,6 +44,17 @@ def load_image(image_name: str, path_to_folder: str, bgr2rgb: bool = True):
|
|
43 |
return image
|
44 |
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
@st.cache
|
47 |
def load_augmentations_config(
|
48 |
placeholder_params: dict, path_to_config: str = "configs/augmentations.json"
|
|
|
1 |
import cv2
|
2 |
import os
|
3 |
+
import numpy as np
|
4 |
import json
|
5 |
import argparse
|
6 |
|
|
|
44 |
return image
|
45 |
|
46 |
|
47 |
+
def upload_image(bgr2rgb: bool = True):
|
48 |
+
"""Uoload the image
|
49 |
+
Args:
|
50 |
+
bgr2rgb (bool): converts BGR image to RGB if True
|
51 |
+
"""
|
52 |
+
file = st.sidebar.file_uploader("Upload your image (jpg, jpeg, or png)", ["jpg", "jpeg", "png"])
|
53 |
+
image = cv2.imdecode(np.fromstring(file.read(), np.uint8), 1)
|
54 |
+
if bgr2rgb:
|
55 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
56 |
+
return image
|
57 |
+
|
58 |
@st.cache
|
59 |
def load_augmentations_config(
|
60 |
placeholder_params: dict, path_to_config: str = "configs/augmentations.json"
|
src/visuals.py
CHANGED
@@ -2,27 +2,40 @@ import cv2
|
|
2 |
import streamlit as st
|
3 |
|
4 |
from control import param2func
|
5 |
-
from utils import get_images_list, load_image
|
6 |
|
7 |
|
8 |
def show_logo():
|
9 |
st.image(load_image("logo.png", "../images"), format="PNG")
|
10 |
|
11 |
|
12 |
-
def select_image(path_to_images: str):
|
13 |
image_names_list = get_images_list(path_to_images)
|
14 |
if len(image_names_list) < 1:
|
15 |
return 0, 0
|
16 |
else:
|
17 |
-
|
|
|
|
|
18 |
image_name = st.sidebar.selectbox("Select an image:", image_names_list)
|
19 |
-
image = load_image(image_name, path_to_images)
|
20 |
-
return 1, image
|
21 |
-
except cv2.error:
|
22 |
-
return 0, 0
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
|
|
26 |
param_values = {"p": 1.0}
|
27 |
if len(transform_params) == 0:
|
28 |
st.sidebar.text("Transform has no parameters")
|
@@ -30,11 +43,11 @@ def show_transform_control(transform_params: dict) -> dict:
|
|
30 |
for param in transform_params:
|
31 |
control_function = param2func[param["type"]]
|
32 |
if isinstance(param["param_name"], list):
|
33 |
-
returned_values = control_function(**param)
|
34 |
for name, value in zip(param["param_name"], returned_values):
|
35 |
param_values[name] = value
|
36 |
else:
|
37 |
-
param_values[param["param_name"]] = control_function(**param)
|
38 |
return param_values
|
39 |
|
40 |
|
@@ -63,5 +76,5 @@ def show_credentials():
|
|
63 |
|
64 |
def show_docstring(obj_with_ds):
|
65 |
st.markdown("* * *")
|
66 |
-
st.subheader("Docstring
|
67 |
st.text(obj_with_ds.__doc__)
|
|
|
2 |
import streamlit as st
|
3 |
|
4 |
from control import param2func
|
5 |
+
from utils import get_images_list, load_image, upload_image
|
6 |
|
7 |
|
8 |
def show_logo():
|
9 |
st.image(load_image("logo.png", "../images"), format="PNG")
|
10 |
|
11 |
|
12 |
+
def select_image(path_to_images: str, interface_type:str = 'Simple'):
|
13 |
image_names_list = get_images_list(path_to_images)
|
14 |
if len(image_names_list) < 1:
|
15 |
return 0, 0
|
16 |
else:
|
17 |
+
if interface_type == 'Professional':
|
18 |
+
image_name = st.sidebar.selectbox("Select an image:", image_names_list + ["Upload my image"])
|
19 |
+
else:
|
20 |
image_name = st.sidebar.selectbox("Select an image:", image_names_list)
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
if image_name != 'Upload my image':
|
23 |
+
try:
|
24 |
+
image = load_image(image_name, path_to_images)
|
25 |
+
return 1, image
|
26 |
+
except cv2.error:
|
27 |
+
return 0, 0
|
28 |
+
else:
|
29 |
+
try:
|
30 |
+
image = upload_image()
|
31 |
+
return 1, image
|
32 |
+
except cv2.error:
|
33 |
+
return 0, 0
|
34 |
+
except AttributeError:
|
35 |
+
return 2, 0
|
36 |
|
37 |
+
|
38 |
+
def show_transform_control(transform_params: dict, n_for_hash: int) -> dict:
|
39 |
param_values = {"p": 1.0}
|
40 |
if len(transform_params) == 0:
|
41 |
st.sidebar.text("Transform has no parameters")
|
|
|
43 |
for param in transform_params:
|
44 |
control_function = param2func[param["type"]]
|
45 |
if isinstance(param["param_name"], list):
|
46 |
+
returned_values = control_function(**param, n_for_hash = n_for_hash)
|
47 |
for name, value in zip(param["param_name"], returned_values):
|
48 |
param_values[name] = value
|
49 |
else:
|
50 |
+
param_values[param["param_name"]] = control_function(**param,n_for_hash = n_for_hash)
|
51 |
return param_values
|
52 |
|
53 |
|
|
|
76 |
|
77 |
def show_docstring(obj_with_ds):
|
78 |
st.markdown("* * *")
|
79 |
+
st.subheader("Docstring for " + obj_with_ds.__class__.__name__)
|
80 |
st.text(obj_with_ds.__doc__)
|