phyloforfun commited on
Commit
7fe3476
·
1 Parent(s): 718a957
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -115,6 +115,19 @@ def update_displayed_quadrilateral(index, point_combinations, base_image_path):
115
  # Display the image with the selected quadrilateral
116
  display_image_with_quadrilateral(base_image, quad_points)
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper, loc, file_name, file_exists, selected_img, headers, base_name):
119
  with loc:
120
  btn_back, btn_next = st.columns([2,2])
@@ -167,15 +180,24 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
167
  with loc:
168
  st.warning("Cycle until correct plot bounds are found")
169
  # Create all possible combinations of four points
170
- point_combinations = list(itertools.combinations(significant_contours, 4))
171
-
 
 
 
 
 
 
 
 
 
172
  # Placeholder for quadrilateral indices
173
  selected_quad_index = 0
174
 
175
  # Function to update displayed quadrilateral based on selected index
176
  def update_displayed_quadrilateral(index):
177
  # Extract the four points of the current quadrilateral
178
- centroids = get_points_from_contours(point_combinations[index])
179
  return centroids
180
 
181
  # Show initial quadrilateral
@@ -184,13 +206,17 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
184
  with btn_back:
185
  # Button to go to the previous quadrilateral
186
  if st.button('Previous'):
187
- selected_quad_index = max(selected_quad_index - 1, 0)
188
- centroids = update_displayed_quadrilateral(selected_quad_index)
 
 
 
 
189
 
190
  with btn_next:
191
  # Button to go to the next quadrilateral
192
  if st.button('Next'):
193
- selected_quad_index = min(selected_quad_index + 1, len(point_combinations) - 1)
194
  centroids = update_displayed_quadrilateral(selected_quad_index)
195
 
196
  with loc:
@@ -560,6 +586,9 @@ if 'dir_uploaded_images_small' not in st.session_state:
560
  if 'keep_quad' not in st.session_state:
561
  st.session_state['keep_quad'] = False
562
 
 
 
 
563
  st.title("GreenSight")
564
  st.write("Simple color segmentation app to estimate the vegetation coverage in a plot. Corners of the plot need to be marked with solid, uniforly colored flags.")
565
  st.write("If you exit the session before completing the segmentation of all images, all progress will be lost!")
 
115
  # Display the image with the selected quadrilateral
116
  display_image_with_quadrilateral(base_image, quad_points)
117
 
118
+ def is_valid_quadrilateral(centroids):
119
+ if len(centroids) != 4:
120
+ return False
121
+
122
+ def ccw(A, B, C):
123
+ return (C[1] - A[1]) * (B[0] - A[0]) > (B[1] - A[1]) * (C[0] - A[0])
124
+
125
+ def intersect(A, B, C, D):
126
+ return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D)
127
+
128
+ A, B, C, D = centroids
129
+ return not (intersect(A, B, C, D) or intersect(A, D, B, C))
130
+
131
  def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper, loc, file_name, file_exists, selected_img, headers, base_name):
132
  with loc:
133
  btn_back, btn_next = st.columns([2,2])
 
180
  with loc:
181
  st.warning("Cycle until correct plot bounds are found")
182
  # Create all possible combinations of four points
183
+ if len(significant_contours) >= 4:
184
+ # Generate all permutations of four points from the significant contours
185
+ permutations_of_four = list(itertools.permutations(significant_contours, 4))
186
+
187
+ # Filter out invalid quadrilaterals
188
+ valid_permutations = [perm for perm in permutations_of_four if is_valid_quadrilateral(get_points_from_contours(perm))]
189
+
190
+ if not valid_permutations:
191
+ st.error("No valid quadrilaterals found.")
192
+ return None, None, None, None, None, None, None, None, None, None
193
+
194
  # Placeholder for quadrilateral indices
195
  selected_quad_index = 0
196
 
197
  # Function to update displayed quadrilateral based on selected index
198
  def update_displayed_quadrilateral(index):
199
  # Extract the four points of the current quadrilateral
200
+ centroids = get_points_from_contours(valid_permutations[index])
201
  return centroids
202
 
203
  # Show initial quadrilateral
 
206
  with btn_back:
207
  # Button to go to the previous quadrilateral
208
  if st.button('Previous'):
209
+ st.session_state.quad_index = (st.session_state.quad_index - 1) % len(valid_permutations)
210
+ centroids = update_displayed_quadrilateral(st.session_state.quad_index)
211
+
212
+ if st.button('Previous'):
213
+ st.session_state.quad_index = (st.session_state.quad_index + 1) % len(valid_permutations)
214
+ centroids = update_displayed_quadrilateral(st.session_state.quad_index)
215
 
216
  with btn_next:
217
  # Button to go to the next quadrilateral
218
  if st.button('Next'):
219
+ selected_quad_index = min(selected_quad_index + 1, len(valid_permutations) - 1)
220
  centroids = update_displayed_quadrilateral(selected_quad_index)
221
 
222
  with loc:
 
586
  if 'keep_quad' not in st.session_state:
587
  st.session_state['keep_quad'] = False
588
 
589
+ if 'quad_index' not in st.session_state:
590
+ st.session_state['quad_index'] = 0
591
+
592
  st.title("GreenSight")
593
  st.write("Simple color segmentation app to estimate the vegetation coverage in a plot. Corners of the plot need to be marked with solid, uniforly colored flags.")
594
  st.write("If you exit the session before completing the segmentation of all images, all progress will be lost!")