Spaces:
Runtime error
Runtime error
Commit
·
8f9d85f
1
Parent(s):
21dbcfe
update
Browse files
app.py
CHANGED
@@ -306,104 +306,106 @@ def main():
|
|
306 |
st.session_state['uploader_idk'] += 1
|
307 |
st.info(f"Processing **{n_images}** images from {st.session_state['dir_input']}")
|
308 |
|
309 |
-
if st.session_state['dir_input']:
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
|
|
|
|
315 |
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
-
if
|
328 |
-
|
329 |
-
|
330 |
-
with
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
|
382 |
-
|
383 |
-
|
384 |
-
|
385 |
-
|
386 |
-
|
387 |
-
|
388 |
-
|
389 |
-
|
390 |
-
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
-
|
395 |
-
|
396 |
-
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
# Write the data
|
402 |
-
writer.writerow([f"{base_name}",f"NA", f"NA", f"NA"])
|
403 |
-
|
404 |
-
# Remove processed image from the list
|
405 |
-
st.session_state.input_list.remove(selected_img)
|
406 |
-
st.rerun()
|
407 |
|
408 |
st.set_page_config(layout="wide", page_title='GreenSight')
|
409 |
|
|
|
306 |
st.session_state['uploader_idk'] += 1
|
307 |
st.info(f"Processing **{n_images}** images from {st.session_state['dir_input']}")
|
308 |
|
309 |
+
if not st.session_state['dir_input']:
|
310 |
+
st.session_state['dir_input'] = os.path.join(st.session_state['dir_home'],"demo")
|
311 |
+
|
312 |
+
# dir_input = st.text_input("Input directory for images:", value=os.path.join(st.session_state['dir_home'],"demo"))
|
313 |
+
dir_output = os.path.join(st.session_state['dir_home'],"demo_out") # st.text_input("Output directory:", value=os.path.join(st.session_state['dir_home'],"demo_out"))
|
314 |
+
|
315 |
+
directory_manager = DirectoryManager(dir_output)
|
316 |
+
directory_manager.create_directories()
|
317 |
|
318 |
+
run_name = st.text_input("Run name:", value="test")
|
319 |
+
file_name = os.path.join(directory_manager.data, f"{run_name}.csv")
|
320 |
+
headers = ['image',"plant_coverage_uncorrected_percen", "plant_coverage_corrected_percent", "plant_area_corrected_cm2"]
|
321 |
+
file_exists = os.path.isfile(file_name)
|
322 |
|
323 |
+
if 'input_list' not in st.session_state:
|
324 |
+
input_images = [os.path.join(st.session_state['dir_input'], fname) for fname in os.listdir(st.session_state['dir_input']) if fname.endswith(('.jpg', '.jpeg', '.png'))]
|
325 |
+
st.session_state.input_list = input_images
|
326 |
|
327 |
+
if os.path.exists(st.session_state['dir_input']):
|
328 |
+
|
329 |
+
if len(st.session_state.input_list) == 0 or st.session_state.input_list is None:
|
330 |
+
st.balloons()
|
331 |
+
else:
|
332 |
+
with img_gallery:
|
333 |
+
selected_img = image_select("Select an image", st.session_state.input_list, use_container_width=False)
|
334 |
+
base_name = os.path.basename(selected_img)
|
335 |
|
336 |
+
if selected_img:
|
337 |
+
|
338 |
+
selected_img_view = Image.open(selected_img)
|
339 |
+
with img_main:
|
340 |
+
st.image(selected_img_view, caption="Selected Image", use_column_width='auto')
|
341 |
+
|
342 |
+
flag_lower_bound, flag_upper_bound, plant_lower_bound, plant_upper_bound = get_color_parameters()
|
343 |
+
|
344 |
+
flag_mask, plant_mask, mask_plant_plot, plant_rgb, plot_rgb, plant_rgb_warp, plant_mask_warp, plant_mask_bi, mask_plant_plot_bi, black_pixels_in_quad = process_image(selected_img, flag_lower_bound, flag_upper_bound, plant_lower_bound, plant_upper_bound)
|
345 |
+
|
346 |
+
if plant_mask_warp is not None:
|
347 |
+
plot_coverage, warp_coverage, plot_area_cm2 = calculate_coverage(mask_plant_plot_bi, plant_mask_warp, black_pixels_in_quad)
|
348 |
+
|
349 |
+
with R_coverage:
|
350 |
+
st.markdown(f"Uncorrected Plant Coverage: {plot_coverage}%")
|
351 |
+
with R_plot_area_cm2:
|
352 |
+
st.markdown(f"Corrected Plant Coverage: {warp_coverage}%")
|
353 |
+
st.markdown(f"Corrected Plant Area: {plot_area_cm2}cm2")
|
354 |
+
|
355 |
+
# Display masks in galleries
|
356 |
+
with img_seg:
|
357 |
+
st.image(plant_mask, caption="Plant Mask", use_column_width=True)
|
358 |
+
st.image(flag_mask, caption="Flag Mask", use_column_width=True)
|
359 |
+
with img_green:
|
360 |
+
st.image(mask_plant_plot, caption="Plant Mask Inside Plot", use_column_width=True)
|
361 |
+
st.image(plant_rgb, caption="Plant Material", use_column_width=True)
|
362 |
+
with img_warp:
|
363 |
+
st.image(plot_rgb, caption="Plant Material Inside Plot", use_column_width=True)
|
364 |
+
st.image(plant_rgb_warp, caption="Plant Mask Inside Plot Warped to Square", use_column_width=True)
|
365 |
+
# st.image(plot_rgb_warp, caption="Flag Mask", use_column_width=True)
|
366 |
+
with R_save:
|
367 |
+
if st.button('Save'):
|
368 |
+
# Save the masks to their respective folders
|
369 |
+
save_img(directory_manager.mask_flag, base_name, flag_mask)
|
370 |
+
save_img(directory_manager.mask_plant, base_name, plant_mask)
|
371 |
+
save_img(directory_manager.mask_plant_plot, base_name, mask_plant_plot)
|
372 |
+
save_img(directory_manager.plant_rgb, base_name, plant_rgb)
|
373 |
+
save_img(directory_manager.plot_rgb, base_name, plot_rgb)
|
374 |
+
save_img(directory_manager.plant_rgb_warp, base_name, plant_rgb_warp)
|
375 |
+
save_img(directory_manager.plant_mask_warp, base_name, plant_mask_warp)
|
376 |
+
|
377 |
+
# Append the data to the CSV file
|
378 |
+
with open(file_name, mode='a', newline='') as file:
|
379 |
+
writer = csv.writer(file)
|
380 |
+
|
381 |
+
# If the file doesn't exist, write the headers
|
382 |
+
if not file_exists:
|
383 |
+
writer.writerow(headers)
|
384 |
+
|
385 |
+
# Write the data
|
386 |
+
writer.writerow([f"{base_name}",f"{plot_coverage}", f"{warp_coverage}", f"{plot_area_cm2}"])
|
387 |
+
|
388 |
+
# Remove processed image from the list
|
389 |
+
st.session_state.input_list.remove(selected_img)
|
390 |
+
st.rerun()
|
391 |
+
create_download_button(dir_output, run_name)
|
392 |
+
else:
|
393 |
+
with R_save:
|
394 |
+
if st.button('Save as Failure'):
|
395 |
+
# Append the data to the CSV file
|
396 |
+
with open(file_name, mode='a', newline='') as file:
|
397 |
+
writer = csv.writer(file)
|
398 |
+
|
399 |
+
# If the file doesn't exist, write the headers
|
400 |
+
if not file_exists:
|
401 |
+
writer.writerow(headers)
|
402 |
+
|
403 |
+
# Write the data
|
404 |
+
writer.writerow([f"{base_name}",f"NA", f"NA", f"NA"])
|
405 |
+
|
406 |
+
# Remove processed image from the list
|
407 |
+
st.session_state.input_list.remove(selected_img)
|
408 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
409 |
|
410 |
st.set_page_config(layout="wide", page_title='GreenSight')
|
411 |
|