andreanascetti commited on
Commit
15a7e90
1 Parent(s): 476c719

first test

Browse files
Files changed (2) hide show
  1. app.py +65 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ def expand2square(imgpath, background_color = (0,0,0)):
10
+ pil_img = Image.open(imgpath)
11
+ width, height = pil_img.size
12
+ if width == height:
13
+ return pil_img
14
+ elif width > height:
15
+ result = Image.new(pil_img.mode, (width, width), background_color)
16
+ result.paste(pil_img, (0, (width - height) // 2))
17
+ return result.resize((700, 700))
18
+ else:
19
+ result = Image.new(pil_img.mode, (height, height), background_color)
20
+ result.paste(pil_img, ((height - width) // 2, 0))
21
+ return result.resize((700, 700))
22
+
23
+ # Specify canvas parameters in application
24
+ drawing_mode = st.sidebar.selectbox(
25
+ "Drawing tool:", ("point", "line", "rect", "circle", "transform")
26
+ )
27
+
28
+ stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 3)
29
+ if drawing_mode == 'point':
30
+ point_display_radius = st.sidebar.slider("Point display radius: ", 1, 25, 3)
31
+ stroke_color = st.sidebar.color_picker("Stroke color hex: ")
32
+ bg_color = st.sidebar.color_picker("Background color hex: ", "#eee")
33
+
34
+ #bg_image = "./IMG_02099.jpg"
35
+ bg_image = st.sidebar.file_uploader("Background image:", type=["png", "jpg"])
36
+
37
+ realtime_update = st.sidebar.checkbox("Update in realtime", True)
38
+
39
+ # Create a canvas component
40
+ canvas_result = st_canvas(
41
+ fill_color="rgba(255, 165, 0, 0.3)", # Fixed fill color with some opacity
42
+ stroke_width=stroke_width,
43
+ stroke_color=stroke_color,
44
+ background_color=bg_color,
45
+ background_image=expand2square(bg_image) if bg_image else expand2square("./IMG_02099.jpg"),
46
+ update_streamlit=realtime_update,
47
+ height=700,
48
+ width=700,
49
+ drawing_mode=drawing_mode,
50
+ point_display_radius=point_display_radius if drawing_mode == 'point' else 0,
51
+ key="canvas",
52
+ )
53
+ test= st.sidebar.write(Image.open(bg_image).size if bg_image else None)
54
+
55
+ testt = st.image(Image.open(bg_image) if bg_image else Image.open("./IMG_02099.jpg"))
56
+
57
+ test2 = st.sidebar.write(bg_color)
58
+ # Do something interesting with the image data and paths
59
+ # if canvas_result.image_data is not None:
60
+ # st.image(canvas_result.image_data)
61
+ if canvas_result.json_data is not None:
62
+ objects = pd.json_normalize(canvas_result.json_data["objects"]) # need to convert obj to str because PyArrow
63
+ for col in objects.select_dtypes(include=['object']).columns:
64
+ objects[col] = objects[col].astype("str")
65
+ st.dataframe(objects)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ pandas
2
+ numpy
3
+ streamlit>=0.88
4
+ streamlit-drawable-canvas==0.9.0