Spaces:
Sleeping
Sleeping
Commit
·
162f3fb
1
Parent(s):
a1cca87
add multi-images view and selection
Browse files
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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|