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