IliaLarchenko commited on
Commit
52a0611
1 Parent(s): 93cade3

added shape changing transforms support

Browse files
Files changed (4) hide show
  1. configs/augmentations.json +21 -2
  2. src/app.py +19 -9
  3. src/control.py +2 -2
  4. src/utils.py +24 -1
configs/augmentations.json CHANGED
@@ -6,6 +6,7 @@
6
  "VerticalFlip": [],
7
  "HorizontalFlip": [],
8
  "Flip": [],
 
9
  "RandomGridShuffle": [
10
  {
11
  "param_name": "grid",
@@ -421,6 +422,24 @@
421
  "param_name": "by_channels",
422
  "type": "checkbox",
423
  "defaults": 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  }
425
- ]
426
- }
 
6
  "VerticalFlip": [],
7
  "HorizontalFlip": [],
8
  "Flip": [],
9
+ "Transpose": [],
10
  "RandomGridShuffle": [
11
  {
12
  "param_name": "grid",
 
422
  "param_name": "by_channels",
423
  "type": "checkbox",
424
  "defaults": 1
425
+ }],
426
+ "CenterCrop" :
427
+ [
428
+ {
429
+ "param_name": "height",
430
+ "type": "int_interval",
431
+ "placeholder": {
432
+ "limits_list" : [1, "image_height"],
433
+ "defaults" : "image_half_height"
434
+ }
435
+ },
436
+ {
437
+ "param_name": "width",
438
+ "type": "int_interval",
439
+ "placeholder": {
440
+ "limits_list" : [1, "image_width"],
441
+ "defaults" : "image_half_width"
442
+ }
443
+ }
444
+ ]
445
  }
 
 
src/app.py CHANGED
@@ -1,10 +1,7 @@
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,
10
  select_image,
@@ -14,19 +11,28 @@ from visuals import (
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
23
- augmentations = load_augmentations_config("configs/augmentations.json")
 
 
24
 
25
  # select a transformation
26
  transform_name = st.sidebar.selectbox(
27
  "Select a transformation:", sorted(list(augmentations.keys()))
28
  )
29
 
 
30
  # select the params values
31
  param_values = show_transform_control(augmentations[transform_name])
32
 
@@ -40,10 +46,14 @@ augmented_image = transform(image=image)["image"]
40
  # st.text("Press R to update")
41
 
42
  # show the images
 
 
 
 
 
 
43
  st.image(
44
- [image, augmented_image],
45
- caption=["Original image", "Transformed image"],
46
- width=335,
47
  )
48
 
49
  # print additional info
 
1
  import streamlit as st
2
  import albumentations as A
3
 
4
+ from utils import load_augmentations_config
 
 
 
5
  from visuals import (
6
  show_transform_control,
7
  select_image,
 
11
 
12
 
13
  # show title
14
+ st.title("Demo of Albumentations")
15
 
16
  # select image
17
  image = select_image(path_to_images="images")
18
+ placeholder_params = {
19
+ "image_width": image.shape[1],
20
+ "image_height": image.shape[0],
21
+ "image_half_width": int(image.shape[1] / 2),
22
+ "image_half_height": int(image.shape[0] / 2),
23
+ }
24
 
25
  # load the config
26
+ augmentations = load_augmentations_config(
27
+ placeholder_params, "configs/augmentations.json"
28
+ )
29
 
30
  # select a transformation
31
  transform_name = st.sidebar.selectbox(
32
  "Select a transformation:", sorted(list(augmentations.keys()))
33
  )
34
 
35
+
36
  # select the params values
37
  param_values = show_transform_control(augmentations[transform_name])
38
 
 
46
  # st.text("Press R to update")
47
 
48
  # show the images
49
+ width_original = 400
50
+ width_transformed = int(width_original / image.shape[1] * augmented_image.shape[1])
51
+
52
+ st.image(
53
+ image, caption="Original image", width=width_original,
54
+ )
55
  st.image(
56
+ augmented_image, caption="Transformed image", width=width_transformed,
 
 
57
  )
58
 
59
  # print additional info
src/control.py CHANGED
@@ -26,7 +26,7 @@ def select_several_ints(
26
  return tuple(result)
27
 
28
 
29
- def select_several_RGB(param_name, **kwargs):
30
  result = select_several_ints(
31
  param_name,
32
  subparam_names=["Red", "Green", "Blue"],
@@ -53,6 +53,6 @@ param2func = {
53
  "int_interval": select_int_interval,
54
  "several_ints": select_several_ints,
55
  "radio": select_radio,
56
- "rgb": select_several_RGB,
57
  "checkbox": select_checkbox,
58
  }
 
26
  return tuple(result)
27
 
28
 
29
+ def select_RGB(param_name, **kwargs):
30
  result = select_several_ints(
31
  param_name,
32
  subparam_names=["Red", "Green", "Blue"],
 
53
  "int_interval": select_int_interval,
54
  "several_ints": select_several_ints,
55
  "radio": select_radio,
56
+ "rgb": select_RGB,
57
  "checkbox": select_checkbox,
58
  }
src/utils.py CHANGED
@@ -24,12 +24,35 @@ def get_images_list(path_to_folder: str) -> list:
24
  return image_names_list
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
 
 
24
  return image_names_list
25
 
26
 
27
+ def fill_placeholders(params, placeholder_params):
28
+ # TODO: refactor
29
+ if "placeholder" in params:
30
+ placeholder_dict = params["placeholder"]
31
+ for k, v in placeholder_dict.items():
32
+ if isinstance(v, list):
33
+ params[k] = []
34
+ for element in v:
35
+ if element in placeholder_params:
36
+ params[k].append(placeholder_params[element])
37
+ else:
38
+ params[k].append(element)
39
+ else:
40
+ if v in placeholder_params:
41
+ params[k] = placeholder_params[v]
42
+ else:
43
+ params[k] = v
44
+ params.pop("placeholder")
45
+ return params
46
+
47
+
48
  @st.cache
49
  def load_augmentations_config(
50
+ placeholder_params: dict, path_to_config: str = "configs/augmentations.json"
51
  ) -> dict:
52
  with open(path_to_config, "r") as config_file:
53
  augmentations = json.load(config_file)
54
+ for name, params in augmentations.items():
55
+ params = [fill_placeholders(param, placeholder_params) for param in params]
56
  return augmentations
57
 
58