Commit
·
98dff60
1
Parent(s):
17d778a
refactoring
Browse files- src/app.py +33 -20
- src/control.py +24 -7
- src/utils.py +4 -1
- src/visuals.py +10 -6
src/app.py
CHANGED
@@ -17,8 +17,9 @@ if not os.path.isdir(path_to_images):
|
|
17 |
st.title("There is no directory: " + path_to_images)
|
18 |
else:
|
19 |
# select interface type
|
20 |
-
interface_type = st.sidebar.radio(
|
21 |
-
|
|
|
22 |
|
23 |
# select image
|
24 |
status, image = select_image(path_to_images, interface_type)
|
@@ -40,20 +41,26 @@ else:
|
|
40 |
)
|
41 |
|
42 |
# select a transformation
|
43 |
-
if interface_type ==
|
44 |
-
transform_names = [
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
# in the professional mode you can choose several transforms
|
48 |
-
elif interface_type ==
|
49 |
-
transform_names = [
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
transform_names = transform_names[:-1]
|
58 |
|
59 |
transforms = []
|
@@ -69,8 +76,10 @@ else:
|
|
69 |
error = 0
|
70 |
except ValueError:
|
71 |
error = 1
|
72 |
-
st.title(
|
73 |
-
|
|
|
|
|
74 |
|
75 |
if error == 0:
|
76 |
augmented_image = data["image"]
|
@@ -83,14 +92,18 @@ else:
|
|
83 |
)
|
84 |
|
85 |
st.image(image, caption="Original image", width=width_original)
|
86 |
-
st.image(
|
|
|
|
|
87 |
|
88 |
# random values used to get transformations
|
89 |
-
if interface_type ==
|
90 |
st.subheader("Random params used")
|
91 |
random_values = {}
|
92 |
for applied_params in data["replay"]["transforms"]:
|
93 |
-
random_values[
|
|
|
|
|
94 |
st.write(random_values)
|
95 |
|
96 |
# print additional info
|
|
|
17 |
st.title("There is no directory: " + path_to_images)
|
18 |
else:
|
19 |
# select interface type
|
20 |
+
interface_type = st.sidebar.radio(
|
21 |
+
"Select the interface type", ["Simple", "Professional"]
|
22 |
+
)
|
23 |
|
24 |
# select image
|
25 |
status, image = select_image(path_to_images, interface_type)
|
|
|
41 |
)
|
42 |
|
43 |
# select a transformation
|
44 |
+
if interface_type == "Simple":
|
45 |
+
transform_names = [
|
46 |
+
st.sidebar.selectbox(
|
47 |
+
"Select a transformation:", sorted(list(augmentations.keys()))
|
48 |
+
)
|
49 |
+
]
|
50 |
# in the professional mode you can choose several transforms
|
51 |
+
elif interface_type == "Professional":
|
52 |
+
transform_names = [
|
53 |
+
st.sidebar.selectbox(
|
54 |
+
"Select transformation №1:", sorted(list(augmentations.keys()))
|
55 |
+
)
|
56 |
+
]
|
57 |
+
while transform_names[-1] != "None":
|
58 |
+
transform_names.append(
|
59 |
+
st.sidebar.selectbox(
|
60 |
+
f"Select transformation №{len(transform_names) + 1}:",
|
61 |
+
["None"] + sorted(list(augmentations.keys())),
|
62 |
+
)
|
63 |
+
)
|
64 |
transform_names = transform_names[:-1]
|
65 |
|
66 |
transforms = []
|
|
|
76 |
error = 0
|
77 |
except ValueError:
|
78 |
error = 1
|
79 |
+
st.title(
|
80 |
+
"The error has occurred. Most probably you have passed wrong set of parameters. \
|
81 |
+
Check transforms that change the shape of image."
|
82 |
+
)
|
83 |
|
84 |
if error == 0:
|
85 |
augmented_image = data["image"]
|
|
|
92 |
)
|
93 |
|
94 |
st.image(image, caption="Original image", width=width_original)
|
95 |
+
st.image(
|
96 |
+
augmented_image, caption="Transformed image", width=width_transformed
|
97 |
+
)
|
98 |
|
99 |
# random values used to get transformations
|
100 |
+
if interface_type == "Professional":
|
101 |
st.subheader("Random params used")
|
102 |
random_values = {}
|
103 |
for applied_params in data["replay"]["transforms"]:
|
104 |
+
random_values[
|
105 |
+
applied_params["__class_fullname__"].split(".")[-1]
|
106 |
+
] = applied_params["params"]
|
107 |
st.write(random_values)
|
108 |
|
109 |
# print additional info
|
src/control.py
CHANGED
@@ -3,10 +3,16 @@ import streamlit as st
|
|
3 |
# TODO: rename and refactor everything
|
4 |
|
5 |
|
6 |
-
def select_num_interval(
|
|
|
|
|
7 |
st.sidebar.subheader(param_name)
|
8 |
min_max_interval = st.sidebar.slider(
|
9 |
-
"",
|
|
|
|
|
|
|
|
|
10 |
)
|
11 |
return min_max_interval
|
12 |
|
@@ -22,16 +28,24 @@ 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,
|
|
|
|
|
|
|
|
|
26 |
)
|
27 |
)
|
28 |
return tuple(result)
|
29 |
|
30 |
|
31 |
-
def select_min_max(
|
|
|
|
|
32 |
assert len(param_name) == 2
|
33 |
result = list(
|
34 |
-
select_num_interval(
|
|
|
|
|
35 |
)
|
36 |
if result[1] - result[0] < min_diff:
|
37 |
diff = min_diff - result[1] + result[0]
|
@@ -49,7 +63,8 @@ def select_RGB(param_name, n_for_hash, **kwargs):
|
|
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 |
|
@@ -69,7 +84,9 @@ def select_radio(param_name, options_list, n_for_hash, **kwargs):
|
|
69 |
|
70 |
def select_checkbox(param_name, defaults, n_for_hash, **kwargs):
|
71 |
st.sidebar.subheader(param_name)
|
72 |
-
result = st.sidebar.checkbox(
|
|
|
|
|
73 |
return result
|
74 |
|
75 |
|
|
|
3 |
# TODO: rename and refactor everything
|
4 |
|
5 |
|
6 |
+
def select_num_interval(
|
7 |
+
param_name: str, limits_list: list, defaults, n_for_hash, **kwargs
|
8 |
+
):
|
9 |
st.sidebar.subheader(param_name)
|
10 |
min_max_interval = st.sidebar.slider(
|
11 |
+
"",
|
12 |
+
limits_list[0],
|
13 |
+
limits_list[1],
|
14 |
+
defaults,
|
15 |
+
key=hash(param_name + str(n_for_hash)),
|
16 |
)
|
17 |
return min_max_interval
|
18 |
|
|
|
28 |
for name, limits, defaults in zip(subparam_names, limits_list, defaults_list):
|
29 |
result.append(
|
30 |
st.sidebar.slider(
|
31 |
+
name,
|
32 |
+
limits[0],
|
33 |
+
limits[1],
|
34 |
+
defaults,
|
35 |
+
key=hash(param_name + name + str(n_for_hash)),
|
36 |
)
|
37 |
)
|
38 |
return tuple(result)
|
39 |
|
40 |
|
41 |
+
def select_min_max(
|
42 |
+
param_name, limits_list, defaults_list, n_for_hash, min_diff=0, **kwargs
|
43 |
+
):
|
44 |
assert len(param_name) == 2
|
45 |
result = list(
|
46 |
+
select_num_interval(
|
47 |
+
" & ".join(param_name), limits_list, defaults_list, n_for_hash
|
48 |
+
)
|
49 |
)
|
50 |
if result[1] - result[0] < min_diff:
|
51 |
diff = min_diff - result[1] + result[0]
|
|
|
63 |
param_name,
|
64 |
subparam_names=["Red", "Green", "Blue"],
|
65 |
limits_list=[[0, 255], [0, 255], [0, 255]],
|
66 |
+
defaults_list=[0, 0, 0],
|
67 |
+
n_for_hash=n_for_hash,
|
68 |
)
|
69 |
return tuple(result)
|
70 |
|
|
|
84 |
|
85 |
def select_checkbox(param_name, defaults, n_for_hash, **kwargs):
|
86 |
st.sidebar.subheader(param_name)
|
87 |
+
result = st.sidebar.checkbox(
|
88 |
+
"True", defaults, key=hash(param_name + str(n_for_hash))
|
89 |
+
)
|
90 |
return result
|
91 |
|
92 |
|
src/utils.py
CHANGED
@@ -49,12 +49,15 @@ def upload_image(bgr2rgb: bool = True):
|
|
49 |
Args:
|
50 |
bgr2rgb (bool): converts BGR image to RGB if True
|
51 |
"""
|
52 |
-
file = st.sidebar.file_uploader(
|
|
|
|
|
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"
|
|
|
49 |
Args:
|
50 |
bgr2rgb (bool): converts BGR image to RGB if True
|
51 |
"""
|
52 |
+
file = st.sidebar.file_uploader(
|
53 |
+
"Upload your image (jpg, jpeg, or png)", ["jpg", "jpeg", "png"]
|
54 |
+
)
|
55 |
image = cv2.imdecode(np.fromstring(file.read(), np.uint8), 1)
|
56 |
if bgr2rgb:
|
57 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
58 |
return image
|
59 |
|
60 |
+
|
61 |
@st.cache
|
62 |
def load_augmentations_config(
|
63 |
placeholder_params: dict, path_to_config: str = "configs/augmentations.json"
|
src/visuals.py
CHANGED
@@ -9,17 +9,19 @@ 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 =
|
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 ==
|
18 |
-
image_name = st.sidebar.selectbox(
|
|
|
|
|
19 |
else:
|
20 |
image_name = st.sidebar.selectbox("Select an image:", image_names_list)
|
21 |
|
22 |
-
if image_name !=
|
23 |
try:
|
24 |
image = load_image(image_name, path_to_images)
|
25 |
return 1, image
|
@@ -43,11 +45,13 @@ def show_transform_control(transform_params: dict, n_for_hash: int) -> dict:
|
|
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
|
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(
|
|
|
|
|
51 |
return param_values
|
52 |
|
53 |
|
|
|
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(
|
19 |
+
"Select an image:", image_names_list + ["Upload my image"]
|
20 |
+
)
|
21 |
else:
|
22 |
image_name = st.sidebar.selectbox("Select an image:", image_names_list)
|
23 |
|
24 |
+
if image_name != "Upload my image":
|
25 |
try:
|
26 |
image = load_image(image_name, path_to_images)
|
27 |
return 1, image
|
|
|
45 |
for param in transform_params:
|
46 |
control_function = param2func[param["type"]]
|
47 |
if isinstance(param["param_name"], list):
|
48 |
+
returned_values = control_function(**param, n_for_hash=n_for_hash)
|
49 |
for name, value in zip(param["param_name"], returned_values):
|
50 |
param_values[name] = value
|
51 |
else:
|
52 |
+
param_values[param["param_name"]] = control_function(
|
53 |
+
**param, n_for_hash=n_for_hash
|
54 |
+
)
|
55 |
return param_values
|
56 |
|
57 |
|