andreanascetti commited on
Commit
162f3fb
1 Parent(s): a1cca87

add multi-images view and selection

Browse files
Files changed (1) hide show
  1. app.py +79 -47
app.py CHANGED
@@ -2,12 +2,11 @@ import pandas as pd
2
  from PIL import Image
3
  import streamlit as st
4
  from streamlit_drawable_canvas import st_canvas
 
 
5
 
6
 
7
-
8
-
9
-
10
- def expand2square(imgpath, background_color = (0,0,0)):
11
  pil_img = Image.open(imgpath)
12
  width, height = pil_img.size
13
  if width == height:
@@ -21,46 +20,79 @@ def expand2square(imgpath, background_color = (0,0,0)):
21
  result.paste(pil_img, ((height - width) // 2, 0))
22
  return result.resize((700, 700))
23
 
24
- # Specify canvas parameters in application
25
- drawing_mode = st.sidebar.selectbox(
26
- "Drawing tool:", ("point", "line", "rect", "circle", "transform")
27
- )
28
-
29
- stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 3)
30
- if drawing_mode == 'point':
31
- point_display_radius = st.sidebar.slider("Point display radius: ", 1, 25, 3)
32
- stroke_color = st.sidebar.color_picker("Stroke color hex: ")
33
- bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
34
-
35
- #bg_image = "./IMG_02099.jpg"
36
- bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
37
-
38
- realtime_update = st.sidebar.checkbox("Update in realtime", True)
39
-
40
- # Create a canvas component
41
- canvas_result = st_canvas(
42
- fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity
43
- stroke_width=stroke_width,
44
- stroke_color=stroke_color,
45
- background_color=bg_color,
46
- background_image=expand2square(bg_image) if bg_image else expand2square("./IMG_02099.jpg"),
47
- update_streamlit=realtime_update,
48
- height=700,
49
- width=700,
50
- drawing_mode=drawing_mode,
51
- point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
52
- key="canvas",
53
- )
54
- test= st.sidebar.write(Image.open(bg_image).size if bg_image else None)
55
-
56
- #testt = st.image(Image.open(bg_image) if bg_image else Image.open("./IMG_02099.jpg"))
57
-
58
- test2 = st.sidebar.write(bg_color)
59
- # Do something interesting with the image data and paths
60
- # if canvas_result.image_data is not None:
61
- # st.image(canvas_result.image_data)
62
- if canvas_result.json_data is not None:
63
- objects = pd.json_normalize(canvas_result.json_data["objects"]) # need to convert obj to str because PyArrow
64
- for col in objects.select_dtypes(include=['object']).columns:
65
- objects[col] = objects[col].astype("str")
66
- st.dataframe(objects)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from PIL import Image
3
  import streamlit as st
4
  from streamlit_drawable_canvas import st_canvas
5
+ from streamlit_image_select import image_select
6
+ from streamlit_sortables import sort_items
7
 
8
 
9
+ def expand2square(imgpath, background_color=(0, 0, 0)):
 
 
 
10
  pil_img = Image.open(imgpath)
11
  width, height = pil_img.size
12
  if width == height:
 
20
  result.paste(pil_img, ((height - width) // 2, 0))
21
  return result.resize((700, 700))
22
 
23
+
24
+ @st.cache_data
25
+ def loading_data(files):
26
+ imgs = []
27
+ imgs_names = []
28
+ for file in files:
29
+ imgs.append(expand2square(file))
30
+ imgs_names.append(file.name)
31
+ return imgs, imgs_names
32
+
33
+ if 'uploaded' not in st.session_state:
34
+ st.session_state['uploaded'] = False
35
+
36
+ images = st.sidebar.file_uploader("Upload here the images (max 4 imgs for demo version):",
37
+ type=["png", "jpg"], accept_multiple_files=True)
38
+
39
+
40
+ if len(images) > 0:
41
+ st.session_state['uploaded'] = True
42
+ imgs_path = []
43
+ imgs = []
44
+ else:
45
+ st.session_state['uploaded'] = False
46
+
47
+ if st.session_state['uploaded'] is True:
48
+
49
+ # Loading uploaded images and cache the data
50
+ imgs, imgs_path = loading_data(images)
51
+
52
+ # Specify canvas parameters in application
53
+ drawing_mode = st.sidebar.selectbox(
54
+ "Drawing tool:", ("point", "line", "rect", "circle", "transform")
55
+ )
56
+
57
+ stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 3)
58
+ if drawing_mode == 'point':
59
+ point_display_radius = st.sidebar.slider("Point display radius: ", 1, 25, 3)
60
+ stroke_color = st.sidebar.color_picker("Stroke color hex: ")
61
+ bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
62
+
63
+ realtime_update = st.sidebar.checkbox("Update in realtime", False)
64
+
65
+ master_index = image_select("Uploaded images", imgs, captions=imgs_path, return_value="index")
66
+
67
+ # Create a canvas component
68
+ canvas_result = st_canvas(
69
+ fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity
70
+ stroke_width=stroke_width,
71
+ stroke_color=stroke_color,
72
+ background_color=bg_color,
73
+ background_image= imgs[master_index], #expand2square(bg_image) if bg_image else expand2square("./IMG_02099.jpg"),
74
+ update_streamlit=realtime_update,
75
+ height=700,
76
+ width=700,
77
+ drawing_mode=drawing_mode,
78
+ point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
79
+ key="canvas",
80
+ )
81
+
82
+ test = st.sidebar.write("Select the processing order of slave images")
83
+
84
+ with st.sidebar:
85
+ imgs_path2 = imgs_path.copy()
86
+ imgs_path2.pop(master_index)
87
+ sorted_items = sort_items(imgs_path2, multi_containers=False, direction='vertical')
88
+
89
+
90
+ # if canvas_result.image_data is not None:
91
+ # st.image(canvas_result.image_data)
92
+ if canvas_result.json_data is not None:
93
+ objects = pd.json_normalize(canvas_result.json_data["objects"]) # need to convert obj to str because PyArrow
94
+
95
+ for col in objects.select_dtypes(include=['object']).columns:
96
+ objects[col] = objects[col].astype("str")
97
+ st.dataframe(objects)
98
+