phyloforfun commited on
Commit
718a957
·
1 Parent(s): 46fc025
Files changed (1) hide show
  1. app.py +84 -83
app.py CHANGED
@@ -163,55 +163,55 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
163
  return None, None, None, None, None, None, None, None, None, None
164
  elif len(significant_contours) > 4:
165
  st.session_state['keep_quad'] = False
166
- while not st.session_state['keep_quad']:
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
182
- centroids = update_displayed_quadrilateral(selected_quad_index)
183
-
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:
197
- if st.button('Keep Plot Bounds'):
198
- st.session_state['keep_quad'] = True
199
- if st.button('Save as Failure'):
200
- st.session_state['keep_quad'] = True
201
- # Append the data to the CSV file
202
- with open(file_name, mode='a', newline='') as file:
203
- writer = csv.writer(file)
204
-
205
- # If the file doesn't exist, write the headers
206
- if not file_exists:
207
- writer.writerow(headers)
208
-
209
- # Write the data
210
- writer.writerow([f"{base_name}",f"NA", f"NA", f"NA"])
211
-
212
- # Remove processed image from the list
213
- st.session_state['input_list'].remove(selected_img)
214
- st.rerun()
215
 
216
  # If there are exactly 4 largest contours, proceed with existing logic
217
  elif len(significant_contours) == 4:
@@ -236,42 +236,43 @@ def process_image(image_path, flag_lower, flag_upper, plant_lower, plant_upper,
236
  # Sort the centroids
237
  centroids.sort(key=lambda point: (-math.atan2(point[1] - centroid_y, point[0] - centroid_x)) % (2 * np.pi))
238
 
239
- # Create a polygon mask using the sorted centroids
240
- poly_mask = np.zeros_like(flag_mask)
241
- cv2.fillPoly(poly_mask, [np.array(centroids)], 255)
242
-
243
- # Mask the plant_mask with poly_mask
244
- mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
 
245
 
246
- # Count the number of black pixels inside the quadrilateral
247
- total_pixels_in_quad = np.prod(poly_mask.shape)
248
- white_pixels_in_quad = np.sum(poly_mask == 255)
249
- black_pixels_in_quad = total_pixels_in_quad - white_pixels_in_quad
250
-
251
- # Extract the RGB pixels from the original image using the mask_plant_plot
252
- plant_rgb = cv2.bitwise_and(img, img, mask=mask_plant_plot)
253
-
254
- # Draw the bounding quadrilateral
255
- plot_rgb = plant_rgb.copy()
256
- for i in range(4):
257
- cv2.line(plot_rgb, centroids[i], centroids[(i+1)%4], (0, 0, 255), 3)
258
-
259
- # Convert the masks to RGB for visualization
260
- flag_mask_rgb = cv2.cvtColor(flag_mask, cv2.COLOR_GRAY2RGB)
261
- orange_color = [255, 165, 0] # RGB value for orange
262
- flag_mask_rgb[np.any(flag_mask_rgb != [0, 0, 0], axis=-1)] = orange_color
263
-
264
- plant_mask_rgb = cv2.cvtColor(plant_mask, cv2.COLOR_GRAY2RGB)
265
- mask_plant_plot_rgb = cv2.cvtColor(mask_plant_plot, cv2.COLOR_GRAY2RGB)
266
- bright_green_color = [0, 255, 0]
267
- plant_mask_rgb[np.any(plant_mask_rgb != [0, 0, 0], axis=-1)] = bright_green_color
268
- mask_plant_plot_rgb[np.any(mask_plant_plot_rgb != [0, 0, 0], axis=-1)] = bright_green_color
269
-
270
- # Warp the images
271
- plant_rgb_warp = warp_image(plant_rgb, centroids)
272
- plant_mask_warp = warp_image(mask_plant_plot_rgb, centroids)
273
 
274
- return flag_mask_rgb, plant_mask_rgb, mask_plant_plot_rgb, plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp, plant_mask, mask_plant_plot, black_pixels_in_quad
275
 
276
  def calculate_coverage(mask_plant_plot, plant_mask_warp, black_pixels_in_quad):
277
  # Calculate the percentage of white pixels for mask_plant_plot
 
163
  return None, None, None, None, None, None, None, None, None, None
164
  elif len(significant_contours) > 4:
165
  st.session_state['keep_quad'] = False
166
+ # while not st.session_state['keep_quad']:
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
182
+ centroids = update_displayed_quadrilateral(selected_quad_index)
183
+
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:
197
+ if st.button('Keep Plot Bounds'):
198
+ st.session_state['keep_quad'] = True
199
+ if st.button('Save as Failure'):
200
+ st.session_state['keep_quad'] = True
201
+ # Append the data to the CSV file
202
+ with open(file_name, mode='a', newline='') as file:
203
+ writer = csv.writer(file)
204
+
205
+ # If the file doesn't exist, write the headers
206
+ if not file_exists:
207
+ writer.writerow(headers)
208
+
209
+ # Write the data
210
+ writer.writerow([f"{base_name}",f"NA", f"NA", f"NA"])
211
+
212
+ # Remove processed image from the list
213
+ st.session_state['input_list'].remove(selected_img)
214
+ st.rerun()
215
 
216
  # If there are exactly 4 largest contours, proceed with existing logic
217
  elif len(significant_contours) == 4:
 
236
  # Sort the centroids
237
  centroids.sort(key=lambda point: (-math.atan2(point[1] - centroid_y, point[0] - centroid_x)) % (2 * np.pi))
238
 
239
+ if len(centroids) == 4:
240
+ # Create a polygon mask using the sorted centroids
241
+ poly_mask = np.zeros_like(flag_mask)
242
+ cv2.fillPoly(poly_mask, [np.array(centroids)], 255)
243
+
244
+ # Mask the plant_mask with poly_mask
245
+ mask_plant_plot = cv2.bitwise_and(plant_mask, plant_mask, mask=poly_mask)
246
 
247
+ # Count the number of black pixels inside the quadrilateral
248
+ total_pixels_in_quad = np.prod(poly_mask.shape)
249
+ white_pixels_in_quad = np.sum(poly_mask == 255)
250
+ black_pixels_in_quad = total_pixels_in_quad - white_pixels_in_quad
251
+
252
+ # Extract the RGB pixels from the original image using the mask_plant_plot
253
+ plant_rgb = cv2.bitwise_and(img, img, mask=mask_plant_plot)
254
+
255
+ # Draw the bounding quadrilateral
256
+ plot_rgb = plant_rgb.copy()
257
+ for i in range(4):
258
+ cv2.line(plot_rgb, centroids[i], centroids[(i+1)%4], (0, 0, 255), 3)
259
+
260
+ # Convert the masks to RGB for visualization
261
+ flag_mask_rgb = cv2.cvtColor(flag_mask, cv2.COLOR_GRAY2RGB)
262
+ orange_color = [255, 165, 0] # RGB value for orange
263
+ flag_mask_rgb[np.any(flag_mask_rgb != [0, 0, 0], axis=-1)] = orange_color
264
+
265
+ plant_mask_rgb = cv2.cvtColor(plant_mask, cv2.COLOR_GRAY2RGB)
266
+ mask_plant_plot_rgb = cv2.cvtColor(mask_plant_plot, cv2.COLOR_GRAY2RGB)
267
+ bright_green_color = [0, 255, 0]
268
+ plant_mask_rgb[np.any(plant_mask_rgb != [0, 0, 0], axis=-1)] = bright_green_color
269
+ mask_plant_plot_rgb[np.any(mask_plant_plot_rgb != [0, 0, 0], axis=-1)] = bright_green_color
270
+
271
+ # Warp the images
272
+ plant_rgb_warp = warp_image(plant_rgb, centroids)
273
+ plant_mask_warp = warp_image(mask_plant_plot_rgb, centroids)
274
 
275
+ return flag_mask_rgb, plant_mask_rgb, mask_plant_plot_rgb, plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp, plant_mask, mask_plant_plot, black_pixels_in_quad
276
 
277
  def calculate_coverage(mask_plant_plot, plant_mask_warp, black_pixels_in_quad):
278
  # Calculate the percentage of white pixels for mask_plant_plot