IliaLarchenko commited on
Commit
e06688d
1 Parent(s): 8347477

refactoring

Browse files
Files changed (2) hide show
  1. src/app.py +69 -44
  2. src/visuals.py +15 -4
src/app.py CHANGED
@@ -10,10 +10,69 @@ from visuals import (
10
  show_docstring,
11
  )
12
 
13
- # TODO: refactor all the new code of professional mode
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # get CLI params: the path to images and image width
16
  path_to_images, width_original = get_arguments()
 
17
  if not os.path.isdir(path_to_images):
18
  st.title("There is no directory: " + path_to_images)
19
  else:
@@ -24,52 +83,24 @@ else:
24
 
25
  # select image
26
  status, image = select_image(path_to_images, interface_type)
27
- if status == 0:
28
  st.title("Can't load image")
29
  if status == 2:
30
  st.title("Please, upload the image")
31
  else:
32
- placeholder_params = {
33
- "image_width": image.shape[1],
34
- "image_height": image.shape[0],
35
- "image_half_width": int(image.shape[1] / 2),
36
- "image_half_height": int(image.shape[0] / 2),
37
- }
38
 
39
  # load the config
40
  augmentations = load_augmentations_config(
41
  placeholder_params, "configs/augmentations.json"
42
  )
43
 
44
- # select a transformation
45
- if interface_type == "Simple":
46
- transform_names = [
47
- st.sidebar.selectbox(
48
- "Select a transformation:", sorted(list(augmentations.keys()))
49
- )
50
- ]
51
- # in the professional mode you can choose several transforms
52
- elif interface_type == "Professional":
53
- transform_names = [
54
- st.sidebar.selectbox(
55
- "Select transformation №1:", sorted(list(augmentations.keys()))
56
- )
57
- ]
58
- while transform_names[-1] != "None":
59
- transform_names.append(
60
- st.sidebar.selectbox(
61
- f"Select transformation №{len(transform_names) + 1}:",
62
- ["None"] + sorted(list(augmentations.keys())),
63
- )
64
- )
65
- transform_names = transform_names[:-1]
66
 
67
- transforms = []
68
- for i, transform_name in enumerate(transform_names):
69
- # select the params values
70
- st.sidebar.subheader("Params of the " + transform_name)
71
- param_values = show_transform_control(augmentations[transform_name], i)
72
- transforms.append(getattr(A, transform_name)(**param_values))
73
 
74
  try:
75
  # apply the transformation to the image
@@ -82,6 +113,7 @@ else:
82
  Check transforms that change the shape of image."
83
  )
84
 
 
85
  if error == 0:
86
  augmented_image = data["image"]
87
  # show title
@@ -98,14 +130,7 @@ else:
98
  )
99
 
100
  # random values used to get transformations
101
- if interface_type == "Professional":
102
- st.subheader("Random params used")
103
- random_values = {}
104
- for applied_params in data["replay"]["transforms"]:
105
- random_values[
106
- applied_params["__class_fullname__"].split(".")[-1]
107
- ] = applied_params["params"]
108
- st.write(random_values)
109
 
110
  # print additional info
111
  for transform in transforms:
 
10
  show_docstring,
11
  )
12
 
13
+
14
+ def get_placeholder_params(image):
15
+ return {
16
+ "image_width": image.shape[1],
17
+ "image_height": image.shape[0],
18
+ "image_half_width": int(image.shape[1] / 2),
19
+ "image_half_height": int(image.shape[0] / 2),
20
+ }
21
+
22
+
23
+ def select_transformations(augmentations: dict, interface_type: str) -> list:
24
+ # in the Simple mode you can choose only one transform
25
+ if interface_type == "Simple":
26
+ transform_names = [
27
+ st.sidebar.selectbox(
28
+ "Select a transformation:", sorted(list(augmentations.keys()))
29
+ )
30
+ ]
31
+ # in the professional mode you can choose several transforms
32
+ elif interface_type == "Professional":
33
+ transform_names = [
34
+ st.sidebar.selectbox(
35
+ "Select transformation №1:", sorted(list(augmentations.keys()))
36
+ )
37
+ ]
38
+ while transform_names[-1] != "None":
39
+ transform_names.append(
40
+ st.sidebar.selectbox(
41
+ f"Select transformation №{len(transform_names) + 1}:",
42
+ ["None"] + sorted(list(augmentations.keys())),
43
+ )
44
+ )
45
+ transform_names = transform_names[:-1]
46
+ return transform_names
47
+
48
+
49
+ def get_transormations_params(transform_names: list) -> list:
50
+ transforms = []
51
+ for i, transform_name in enumerate(transform_names):
52
+ # select the params values
53
+ st.sidebar.subheader("Params of the " + transform_name)
54
+ param_values = show_transform_control(augmentations[transform_name], i)
55
+ transforms.append(getattr(A, transform_name)(**param_values))
56
+ return transforms
57
+
58
+
59
+ def show_random_params(data: dict, interface_type: str = "Professional"):
60
+ """Shows random params used for transformation (from A.ReplayCompose)"""
61
+ if interface_type == "Professional":
62
+ st.subheader("Random params used")
63
+ random_values = {}
64
+ for applied_params in data["replay"]["transforms"]:
65
+ random_values[
66
+ applied_params["__class_fullname__"].split(".")[-1]
67
+ ] = applied_params["params"]
68
+ st.write(random_values)
69
+
70
+
71
+ # TODO: refactor all the new code
72
 
73
  # get CLI params: the path to images and image width
74
  path_to_images, width_original = get_arguments()
75
+
76
  if not os.path.isdir(path_to_images):
77
  st.title("There is no directory: " + path_to_images)
78
  else:
 
83
 
84
  # select image
85
  status, image = select_image(path_to_images, interface_type)
86
+ if status == 1:
87
  st.title("Can't load image")
88
  if status == 2:
89
  st.title("Please, upload the image")
90
  else:
91
+ # image was loaded successfully
92
+ placeholder_params = get_placeholder_params(image)
 
 
 
 
93
 
94
  # load the config
95
  augmentations = load_augmentations_config(
96
  placeholder_params, "configs/augmentations.json"
97
  )
98
 
99
+ # get list of transformations names
100
+ transform_names = select_transformations(augmentations, interface_type)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ # get parameters for each transform
103
+ transforms = get_transormations_params(transform_names)
 
 
 
 
104
 
105
  try:
106
  # apply the transformation to the image
 
113
  Check transforms that change the shape of image."
114
  )
115
 
116
+ # proced only if everything is ok
117
  if error == 0:
118
  augmented_image = data["image"]
119
  # show title
 
130
  )
131
 
132
  # random values used to get transformations
133
+ show_random_params(data, interface_type)
 
 
 
 
 
 
 
134
 
135
  # print additional info
136
  for transform in transforms:
src/visuals.py CHANGED
@@ -10,9 +10,20 @@ def show_logo():
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(
@@ -26,13 +37,13 @@ def select_image(path_to_images: str, interface_type: str = "Simple"):
26
  image = load_image(image_name, path_to_images)
27
  return 1, image
28
  except cv2.error:
29
- return 0, 0
30
  else:
31
  try:
32
  image = upload_image()
33
- return 1, image
34
  except cv2.error:
35
- return 0, 0
36
  except AttributeError:
37
  return 2, 0
38
 
 
10
 
11
 
12
  def select_image(path_to_images: str, interface_type: str = "Simple"):
13
+ """ Show interface to choose the image, and load it
14
+ Args:
15
+ path_to_images (dict): path ot folder with images
16
+ interface_type (dict): mode of the interface used
17
+ Returns:
18
+ (status, image)
19
+ status (int):
20
+ 0 - if everything is ok
21
+ 1 - if there is error during loading of image file
22
+ 2 - if user hasn't uploaded photo yet
23
+ """
24
  image_names_list = get_images_list(path_to_images)
25
  if len(image_names_list) < 1:
26
+ return 1, 0
27
  else:
28
  if interface_type == "Professional":
29
  image_name = st.sidebar.selectbox(
 
37
  image = load_image(image_name, path_to_images)
38
  return 1, image
39
  except cv2.error:
40
+ return 1, 0
41
  else:
42
  try:
43
  image = upload_image()
44
+ return 0, image
45
  except cv2.error:
46
+ return 1, 0
47
  except AttributeError:
48
  return 2, 0
49