phyloforfun commited on
Commit
4fd550d
·
1 Parent(s): 9e76f0c
Files changed (1) hide show
  1. app.py +91 -4
app.py CHANGED
@@ -1,10 +1,16 @@
1
- import os, math, csv
2
  import streamlit as st
3
  from streamlit_image_select import image_select
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
  import matplotlib.colors as mcolors
 
 
 
 
 
 
8
 
9
  class DirectoryManager:
10
  def __init__(self, output_dir):
@@ -220,13 +226,89 @@ def save_img(directory, base_name, mask):
220
  mask_name = os.path.join(directory, os.path.basename(base_name))
221
  cv2.imwrite(mask_name, mask)
222
 
223
- def main():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
 
225
  _, R_coverage, R_plot_area_cm2, R_save = st.columns([5,2,2,2])
226
  img_gallery, img_main, img_seg, img_green, img_warp = st.columns([1,4,2,2,2])
227
 
228
- dir_input = st.text_input("Input directory for images:", value="D:\Dropbox\GreenSight\demo")
229
- dir_output = st.text_input("Output directory:", value="D:\Dropbox\GreenSight\demo_out")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
  directory_manager = DirectoryManager(dir_output)
232
  directory_manager.create_directories()
@@ -304,6 +386,7 @@ def main():
304
  # Remove processed image from the list
305
  st.session_state.input_list.remove(selected_img)
306
  st.rerun()
 
307
  else:
308
  with R_save:
309
  if st.button('Save as Failure'):
@@ -323,5 +406,9 @@ def main():
323
  st.rerun()
324
 
325
  st.set_page_config(layout="wide", page_title='GreenSight')
 
 
 
 
326
  st.title("GreenSight")
327
  main()
 
1
+ import os, math, csv, shutil
2
  import streamlit as st
3
  from streamlit_image_select import image_select
4
  import cv2
5
  import numpy as np
6
  from PIL import Image
7
  import matplotlib.colors as mcolors
8
+ from io import BytesIO
9
+
10
+
11
+ MAX_GALLERY_IMAGES = 50
12
+ GALLERY_IMAGE_SIZE = 128
13
+
14
 
15
  class DirectoryManager:
16
  def __init__(self, output_dir):
 
226
  mask_name = os.path.join(directory, os.path.basename(base_name))
227
  cv2.imwrite(mask_name, mask)
228
 
229
+ def validate_dir(dir):
230
+ if not os.path.exists(dir):
231
+ os.makedirs(dir, exist_ok=True)
232
+
233
+ def make_zipfile(source_dir, output_filename):
234
+ shutil.make_archive(output_filename, 'zip', source_dir)
235
+ return output_filename + '.zip'
236
+
237
+ def save_uploaded_file(directory, img_file, image=None):
238
+ if not os.path.exists(directory):
239
+ os.makedirs(directory)
240
+ # Assuming the uploaded file is an image
241
+ if image is None:
242
+ with Image.open(img_file) as image:
243
+ full_path = os.path.join(directory, img_file.name)
244
+ image.save(full_path, "JPEG")
245
+ # Return the full path of the saved image
246
+ return full_path
247
+ else:
248
+ full_path = os.path.join(directory, img_file.name)
249
+ image.save(full_path, "JPEG")
250
+ return full_path
251
+
252
+ def create_download_button(dir_to_zip, zip_filename):
253
+ zip_filepath = make_zipfile(dir_to_zip, zip_filename)
254
+ with open(zip_filepath, 'rb') as f:
255
+ bytes_io = BytesIO(f.read())
256
+ st.download_button(
257
+ label=f"Download Results for{st.session_state['processing_add_on']}",type='primary',
258
+ data=bytes_io,
259
+ file_name=os.path.basename(zip_filepath),
260
+ mime='application/zip'
261
+ )
262
+
263
+ def delete_directory(dir_path):
264
+ try:
265
+ shutil.rmtree(dir_path)
266
+ st.session_state['input_list'] = []
267
+ st.session_state['input_list_small'] = []
268
+ # st.success(f"Deleted previously uploaded images, making room for new images: {dir_path}")
269
+ except OSError as e:
270
+ st.error(f"Error: {dir_path} : {e.strerror}")
271
+
272
+ def clear_image_gallery():
273
+ delete_directory(st.session_state['dir_uploaded_images'])
274
+ delete_directory(st.session_state['dir_uploaded_images_small'])
275
+ validate_dir(st.session_state['dir_uploaded_images'])
276
+ validate_dir(st.session_state['dir_uploaded_images_small'])
277
 
278
+ def main():
279
  _, R_coverage, R_plot_area_cm2, R_save = st.columns([5,2,2,2])
280
  img_gallery, img_main, img_seg, img_green, img_warp = st.columns([1,4,2,2,2])
281
 
282
+ st.session_state['dir_uploaded_images'] = os.path.join(st.session_state.dir_home,'uploads')
283
+ st.session_state['dir_uploaded_images_small'] = os.path.join(st.session_state.dir_home,'uploads_small')
284
+ uploaded_files = st.file_uploader("Upload Images", type=['jpg', 'jpeg'], accept_multiple_files=True, key=st.session_state['uploader_idk'])
285
+ if uploaded_files:
286
+ # Clear input image gallery and input list
287
+ clear_image_gallery()
288
+
289
+ # Process the new iamges
290
+ for uploaded_file in uploaded_files:
291
+ file_path = save_uploaded_file(st.session_state['dir_uploaded_images'], uploaded_file)
292
+ st.session_state['input_list'].append(file_path)
293
+
294
+ img = Image.open(file_path)
295
+ img.thumbnail((GALLERY_IMAGE_SIZE, GALLERY_IMAGE_SIZE), Image.Resampling.LANCZOS)
296
+ file_path_small = save_uploaded_file(st.session_state['dir_uploaded_images_small'], uploaded_file, img)
297
+ st.session_state['input_list_small'].append(file_path_small)
298
+ print(uploaded_file.name)
299
+
300
+ # Set the local images to the uploaded images
301
+ dir_input = st.session_state['dir_uploaded_images']
302
+
303
+ n_images = len([f for f in os.listdir(dir_input) if os.path.isfile(os.path.join(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 {dir_input}")
308
+
309
+
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()
 
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'):
 
406
  st.rerun()
407
 
408
  st.set_page_config(layout="wide", page_title='GreenSight')
409
+
410
+ if 'dir_home' not in st.session_state:
411
+ st.session_state['dir_home'] = os.path.dirname(__file__)
412
+
413
  st.title("GreenSight")
414
  main()