IliaLarchenko commited on
Commit
d5465e6
1 Parent(s): 6e61d26

refactoring

Browse files
Files changed (3) hide show
  1. configs/augmentations.json +4 -4
  2. src/main.py +50 -49
  3. src/utils.py +36 -0
configs/augmentations.json CHANGED
@@ -11,8 +11,8 @@
11
  "param_name": "grid",
12
  "type": "several_ints",
13
  "subparam_names": [
14
- "y",
15
- "x"
16
  ],
17
  "limits_list": [
18
  [
@@ -264,8 +264,8 @@
264
  "param_name": "tile_grid_size",
265
  "type": "several_ints",
266
  "subparam_names": [
267
- "y",
268
- "x"
269
  ],
270
  "limits_list": [
271
  [
 
11
  "param_name": "grid",
12
  "type": "several_ints",
13
  "subparam_names": [
14
+ "height",
15
+ "width"
16
  ],
17
  "limits_list": [
18
  [
 
264
  "param_name": "tile_grid_size",
265
  "type": "several_ints",
266
  "subparam_names": [
267
+ "height",
268
+ "width"
269
  ],
270
  "limits_list": [
271
  [
src/main.py CHANGED
@@ -1,73 +1,74 @@
1
- import numpy as np
2
- import os
3
- import cv2
4
- import json
5
-
6
- import albumentations as A
7
  import streamlit as st
 
8
 
9
  from control import *
 
 
 
 
 
 
10
 
11
 
12
- def load_image(image_name, path_to_folder="../images"):
13
- path_to_image = os.path.join(path_to_folder, image_name)
14
- image = cv2.imread(path_to_image)
15
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
16
  return image
17
 
18
 
19
- st.title("Demo of Albumentations transforms")
 
 
 
 
 
 
 
20
 
21
 
22
- # selecting the image
23
- path_to_images = "images"
24
- image_names_list = [
25
- x for x in os.listdir(path_to_images) if x[-3:] in ["jpg", "peg", "png"]
26
- ]
27
- image_name = st.sidebar.selectbox("Select an image:", image_names_list)
28
- image = load_image(image_name, path_to_images)
 
 
29
 
30
 
31
- # selecting the transformation
32
- path_to_config = "configs/augmentations.json"
33
- with open(path_to_config, "r") as config_file:
34
- augmentations = json.load(config_file)
35
- transform_name = st.sidebar.selectbox(
36
- "Select a transformation:", sorted(list(augmentations.keys()))
37
- )
38
- transform_params = augmentations[transform_name]
39
 
40
 
41
- # show the transform options
42
- if len(transform_params) == 0:
43
- st.sidebar.text(transform_name + " transform has no parameters")
44
- else:
45
- for param in transform_params:
46
- param["value"] = param2func[param["type"]](**param)
47
 
 
 
48
 
49
- params_string = ", ".join(
50
- [param["param_name"] + "=" + str(param["value"]) for param in transform_params]
51
- + ["p=1.0"]
 
52
  )
53
- params_string = "(" + params_string + ")"
54
 
55
- st.text(transform_name + params_string)
 
 
 
 
56
  st.text("Press R to update")
57
- exec("transform = A." + transform_name + params_string)
 
 
58
  st.image(
59
- [image, transform(image=image)["image"]],
60
  caption=["Original image", "Transformed image"],
61
  width=320,
62
  )
63
 
64
- st.subheader("Docstring:")
65
- st.text(str(transform.__doc__))
66
-
67
-
68
- st.text("")
69
- st.text("")
70
- st.subheader("Credentials:")
71
- st.text("Source: https://github.com/IliaLarchenko/albumentations-demo")
72
- st.text("Albumentations library: https://github.com/albumentations-team/albumentations")
73
- st.text("Image Source: https://www.pexels.com/royalty-free-images/")
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import albumentations as A
3
 
4
  from control import *
5
+ from utils import (
6
+ load_image,
7
+ get_images_list,
8
+ load_augmentations_config,
9
+ generate_executable_string,
10
+ )
11
 
12
 
13
+ def select_image(path_to_images: str = "images"):
14
+ image_names_list = get_images_list(path_to_images)
15
+ image_name = st.sidebar.selectbox("Select an image:", image_names_list)
16
+ image = load_image(image_name, path_to_images)
17
  return image
18
 
19
 
20
+ def show_transform_control(transform_params: dict):
21
+ param_values = {}
22
+ if len(transform_params) == 0:
23
+ st.sidebar.text(transform_name + " transform has no parameters")
24
+ else:
25
+ for param in transform_params:
26
+ param_values[param["param_name"]] = param2func[param["type"]](**param)
27
+ return param_values
28
 
29
 
30
+ def show_credentials():
31
+ st.text("")
32
+ st.text("")
33
+ st.subheader("Credentials:")
34
+ st.text("Source: https://github.com/IliaLarchenko/albumentations-demo")
35
+ st.text(
36
+ "Albumentations library: https://github.com/albumentations-team/albumentations"
37
+ )
38
+ st.text("Image Source: https://www.pexels.com/royalty-free-images/")
39
 
40
 
41
+ def show_docstring(object):
42
+ st.subheader("Docstring:")
43
+ st.text(str(object.__doc__))
 
 
 
 
 
44
 
45
 
46
+ # main
47
+ st.title("Demo of Albumentations transforms")
 
 
 
 
48
 
49
+ # selecting an image
50
+ image = select_image(path_to_images="images")
51
 
52
+ # load the config
53
+ augmentations = load_augmentations_config("configs/augmentations.json")
54
+ transform_name = st.sidebar.selectbox(
55
+ "Select a transformation:", sorted(list(augmentations.keys()))
56
  )
 
57
 
58
+ # select the params values
59
+ param_values = show_transform_control(augmentations[transform_name])
60
+ executable_string = generate_executable_string(transform_name, param_values)
61
+
62
+ st.text(executable_string)
63
  st.text("Press R to update")
64
+ exec("transform = A." + executable_string)
65
+ augmented_image = transform(image=image)["image"]
66
+
67
  st.image(
68
+ [image, augmented_image],
69
  caption=["Original image", "Transformed image"],
70
  width=320,
71
  )
72
 
73
+ show_docstring(transform)
74
+ show_credentials()
 
 
 
 
 
 
 
 
src/utils.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import json
4
+
5
+ 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
+ ]
21
+ return image_names_list
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