Spaces:
Runtime error
Runtime error
Commit
·
718a957
1
Parent(s):
46fc025
update
Browse files
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 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
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 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
245 |
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
|
274 |
-
|
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
|