IliaLarchenko commited on
Commit
b30ce1c
1 Parent(s): 6e7a74d

refactoring

Browse files
Files changed (7) hide show
  1. Procfile +1 -1
  2. docs/logo.png +0 -0
  3. run.sh +1 -1
  4. src/__init__.py +0 -0
  5. src/{main.py → app.py} +9 -7
  6. src/utils.py +12 -8
  7. src/visuals.py +12 -9
Procfile CHANGED
@@ -1 +1 @@
1
- web: sh setup.sh && streamlit run src/main.py
 
1
+ web: sh setup.sh && streamlit run src/app.py
docs/logo.png ADDED
run.sh CHANGED
@@ -1 +1 @@
1
- streamlit run src/main.py
 
1
+ streamlit run src/app.py
src/__init__.py ADDED
File without changes
src/{main.py → app.py} RENAMED
@@ -1,10 +1,9 @@
1
  import streamlit as st
2
  import albumentations as A
3
 
4
- from control import *
5
  from utils import (
6
  load_augmentations_config,
7
- generate_executable_string,
8
  )
9
  from visuals import (
10
  show_transform_control,
@@ -14,8 +13,10 @@ from visuals import (
14
  )
15
 
16
 
17
- # main
18
  st.title("Demo of Albumentations transforms")
 
 
19
  image = select_image(path_to_images="images")
20
 
21
  # load the config
@@ -30,13 +31,14 @@ transform_name = st.sidebar.selectbox(
30
  param_values = show_transform_control(augmentations[transform_name])
31
 
32
  # apply the transformation to the image
33
- executable_string = generate_executable_string(transform_name, param_values)
34
- st.text(executable_string)
35
- exec("transform = A." + executable_string)
36
  augmented_image = transform(image=image)["image"]
37
 
38
- # show the images
 
39
  st.text("Press R to update")
 
 
40
  st.image(
41
  [image, augmented_image],
42
  caption=["Original image", "Transformed image"],
 
1
  import streamlit as st
2
  import albumentations as A
3
 
 
4
  from utils import (
5
  load_augmentations_config,
6
+ get_params_string
7
  )
8
  from visuals import (
9
  show_transform_control,
 
13
  )
14
 
15
 
16
+ # show title
17
  st.title("Demo of Albumentations transforms")
18
+
19
+ # select image
20
  image = select_image(path_to_images="images")
21
 
22
  # load the config
 
31
  param_values = show_transform_control(augmentations[transform_name])
32
 
33
  # apply the transformation to the image
34
+ transform = getattr(A, transform_name)(**param_values)
 
 
35
  augmented_image = transform(image=image)["image"]
36
 
37
+ # show the params passed
38
+ st.text("Params passed:" + get_params_string(param_values))
39
  st.text("Press R to update")
40
+
41
+ # show the images
42
  st.image(
43
  [image, augmented_image],
44
  caption=["Original image", "Transformed image"],
src/utils.py CHANGED
@@ -6,15 +6,18 @@ import streamlit as st
6
 
7
 
8
  @st.cache
9
- def load_image(image_name, path_to_folder="../images"):
 
 
10
  path_to_image = os.path.join(path_to_folder, image_name)
11
- image = cv2.imread(path_to_image)
12
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
13
  return image
14
 
15
 
16
  @st.cache
17
- def get_images_list(path_to_folder):
18
  image_names_list = [
19
  x for x in os.listdir(path_to_folder) if x[-3:] in ["jpg", "peg", "png"]
20
  ]
@@ -22,15 +25,16 @@ def get_images_list(path_to_folder):
22
 
23
 
24
  @st.cache
25
- def load_augmentations_config(path_to_config: str = "configs/augmentations.json"):
 
 
26
  with open(path_to_config, "r") as config_file:
27
  augmentations = json.load(config_file)
28
  return augmentations
29
 
30
 
31
- def generate_executable_string(transform_name, param_values):
32
  params_string = ", ".join(
33
  [k + "=" + str(param_values[k]) for k in param_values.keys()] + ["p=1.0"]
34
  )
35
- params_string = "(" + params_string + ")"
36
- return transform_name + params_string
 
6
 
7
 
8
  @st.cache
9
+ def load_image(
10
+ image_name: str, path_to_folder: str = "../images", bgr2rgb: bool = True
11
+ ):
12
  path_to_image = os.path.join(path_to_folder, image_name)
13
+ image = cv2.imread(path_to_image,)
14
+ if bgr2rgb:
15
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
16
  return image
17
 
18
 
19
  @st.cache
20
+ def get_images_list(path_to_folder: str) -> list:
21
  image_names_list = [
22
  x for x in os.listdir(path_to_folder) if x[-3:] in ["jpg", "peg", "png"]
23
  ]
 
25
 
26
 
27
  @st.cache
28
+ def load_augmentations_config(
29
+ path_to_config: str = "configs/augmentations.json",
30
+ ) -> dict:
31
  with open(path_to_config, "r") as config_file:
32
  augmentations = json.load(config_file)
33
  return augmentations
34
 
35
 
36
+ def get_params_string(param_values: dict) -> str:
37
  params_string = ", ".join(
38
  [k + "=" + str(param_values[k]) for k in param_values.keys()] + ["p=1.0"]
39
  )
40
+ return params_string
 
src/visuals.py CHANGED
@@ -4,6 +4,10 @@ from control import param2func
4
  from utils import get_images_list, load_image
5
 
6
 
 
 
 
 
7
  def select_image(path_to_images: str = "images"):
8
  image_names_list = get_images_list(path_to_images)
9
  image_name = st.sidebar.selectbox("Select an image:", image_names_list)
@@ -14,10 +18,11 @@ def select_image(path_to_images: str = "images"):
14
  def show_transform_control(transform_params: dict):
15
  param_values = {}
16
  if len(transform_params) == 0:
17
- st.sidebar.text(transform_name + " transform has no parameters")
18
  else:
19
  for param in transform_params:
20
- param_values[param["param_name"]] = param2func[param["type"]](**param)
 
21
  return param_values
22
 
23
 
@@ -25,13 +30,11 @@ def show_credentials():
25
  st.text("")
26
  st.text("")
27
  st.subheader("Credentials:")
28
- st.text("Source: https://github.com/IliaLarchenko/albumentations-demo")
29
- st.text(
30
- "Albumentations library: https://github.com/albumentations-team/albumentations"
31
- )
32
- st.text("Image Source: https://www.pexels.com/royalty-free-images/")
33
 
34
 
35
- def show_docstring(object):
36
  st.subheader("Docstring:")
37
- st.text(str(object.__doc__))
 
4
  from utils import get_images_list, load_image
5
 
6
 
7
+ def show_logo():
8
+ st.image(load_image("logo.png", "../images"), format="PNG")
9
+
10
+
11
  def select_image(path_to_images: str = "images"):
12
  image_names_list = get_images_list(path_to_images)
13
  image_name = st.sidebar.selectbox("Select an image:", image_names_list)
 
18
  def show_transform_control(transform_params: dict):
19
  param_values = {}
20
  if len(transform_params) == 0:
21
+ st.sidebar.text("Transform has no parameters")
22
  else:
23
  for param in transform_params:
24
+ control_function = param2func[param["type"]]
25
+ param_values[param["param_name"]] = control_function(**param)
26
  return param_values
27
 
28
 
 
30
  st.text("")
31
  st.text("")
32
  st.subheader("Credentials:")
33
+ st.text("Source: github.com/IliaLarchenko/albumentations-demo")
34
+ st.text("Albumentations library: github.com/albumentations-team/albumentations")
35
+ st.text("Image Source: pexels.com/royalty-free-images/")
 
 
36
 
37
 
38
+ def show_docstring(obj_with_ds):
39
  st.subheader("Docstring:")
40
+ st.text(str(obj_with_ds.__doc__))