phyloforfun commited on
Commit
8f9d85f
·
1 Parent(s): 21dbcfe
Files changed (1) hide show
  1. app.py +96 -94
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
- # dir_input = st.text_input("Input directory for images:", value=os.path.join(st.session_state['dir_home'],"demo"))
311
- 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"))
312
-
313
- directory_manager = DirectoryManager(dir_output)
314
- directory_manager.create_directories()
 
 
315
 
316
- run_name = st.text_input("Run name:", value="test")
317
- file_name = os.path.join(directory_manager.data, f"{run_name}.csv")
318
- headers = ['image',"plant_coverage_uncorrected_percen", "plant_coverage_corrected_percent", "plant_area_corrected_cm2"]
319
- file_exists = os.path.isfile(file_name)
320
 
321
- if 'input_list' not in st.session_state:
322
- 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'))]
323
- st.session_state.input_list = input_images
324
 
325
- if os.path.exists(st.session_state['dir_input']):
 
 
 
 
 
 
 
326
 
327
- if len(st.session_state.input_list) == 0 or st.session_state.input_list is None:
328
- st.balloons()
329
- else:
330
- with img_gallery:
331
- selected_img = image_select("Select an image", st.session_state.input_list, use_container_width=False)
332
- base_name = os.path.basename(selected_img)
333
-
334
- if selected_img:
335
-
336
- selected_img_view = Image.open(selected_img)
337
- with img_main:
338
- st.image(selected_img_view, caption="Selected Image", use_column_width='auto')
339
-
340
- flag_lower_bound, flag_upper_bound, plant_lower_bound, plant_upper_bound = get_color_parameters()
341
-
342
- 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)
343
-
344
- if plant_mask_warp is not None:
345
- plot_coverage, warp_coverage, plot_area_cm2 = calculate_coverage(mask_plant_plot_bi, plant_mask_warp, black_pixels_in_quad)
346
-
347
- with R_coverage:
348
- st.markdown(f"Uncorrected Plant Coverage: {plot_coverage}%")
349
- with R_plot_area_cm2:
350
- st.markdown(f"Corrected Plant Coverage: {warp_coverage}%")
351
- st.markdown(f"Corrected Plant Area: {plot_area_cm2}cm2")
352
-
353
- # Display masks in galleries
354
- with img_seg:
355
- st.image(plant_mask, caption="Plant Mask", use_column_width=True)
356
- st.image(flag_mask, caption="Flag Mask", use_column_width=True)
357
- with img_green:
358
- st.image(mask_plant_plot, caption="Plant Mask Inside Plot", use_column_width=True)
359
- st.image(plant_rgb, caption="Plant Material", use_column_width=True)
360
- with img_warp:
361
- st.image(plot_rgb, caption="Plant Material Inside Plot", use_column_width=True)
362
- st.image(plant_rgb_warp, caption="Plant Mask Inside Plot Warped to Square", use_column_width=True)
363
- # st.image(plot_rgb_warp, caption="Flag Mask", use_column_width=True)
364
- with R_save:
365
- if st.button('Save'):
366
- # Save the masks to their respective folders
367
- save_img(directory_manager.mask_flag, base_name, flag_mask)
368
- save_img(directory_manager.mask_plant, base_name, plant_mask)
369
- save_img(directory_manager.mask_plant_plot, base_name, mask_plant_plot)
370
- save_img(directory_manager.plant_rgb, base_name, plant_rgb)
371
- save_img(directory_manager.plot_rgb, base_name, plot_rgb)
372
- save_img(directory_manager.plant_rgb_warp, base_name, plant_rgb_warp)
373
- save_img(directory_manager.plant_mask_warp, base_name, plant_mask_warp)
374
-
375
- # Append the data to the CSV file
376
- with open(file_name, mode='a', newline='') as file:
377
- writer = csv.writer(file)
378
-
379
- # If the file doesn't exist, write the headers
380
- if not file_exists:
381
- writer.writerow(headers)
382
-
383
- # Write the data
384
- writer.writerow([f"{base_name}",f"{plot_coverage}", f"{warp_coverage}", f"{plot_area_cm2}"])
385
-
386
- # Remove processed image from the list
387
- st.session_state.input_list.remove(selected_img)
388
- st.rerun()
389
- create_download_button(dir_output, run_name)
390
- else:
391
- with R_save:
392
- if st.button('Save as Failure'):
393
- # Append the data to the CSV file
394
- with open(file_name, mode='a', newline='') as file:
395
- writer = csv.writer(file)
396
-
397
- # If the file doesn't exist, write the headers
398
- if not file_exists:
399
- writer.writerow(headers)
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