whuang06 commited on
Commit
f35a0ee
·
verified ·
1 Parent(s): 3329dd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -16
app.py CHANGED
@@ -1,12 +1,19 @@
1
  import streamlit as st
2
  from streamlit_drawable_canvas import st_canvas
 
 
 
3
  from PIL import Image
4
  import numpy as np
5
  import cv2
6
  import hashlib
7
- import pyimgur
8
  import time
 
9
  import os
 
 
 
10
 
11
  # Function to convert the canvas drawing to a binary mask
12
  def canvas_to_mask(canvas_result, img_shape):
@@ -79,31 +86,59 @@ def delete_files_in_folder(folder_path):
79
  print(f"Error deleting files: {e}")
80
 
81
 
82
- st.title("IB Geo IA Survey")
 
83
 
84
- # Upload an image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
 
 
 
 
 
 
86
 
87
- image = Image.open("img/map.png").convert("RGB")
 
 
 
 
88
  img_array = np.array(image)
89
- width = st.slider('Stroke width', 0, 20, 5)
90
 
91
  # Create a canvas for drawing
92
- st.subheader("Highlight the central business district area:")
93
  canvas_result = st_canvas(
94
  fill_color="rgba(255, 165, 0, 0.7)", # Use an orange, semi-transparent fill
95
- stroke_width=width,
96
  stroke_color="rgba(255, 165, 0, 0.7)",
97
- background_image=Image.open("img/map.png"),
98
  update_streamlit=True,
99
  height=img_array.shape[0],
100
  width=img_array.shape[1],
101
  drawing_mode="freedraw",
102
  key="canvas",
103
  )
104
- im = pyimgur.Imgur(
105
- "253ebfef9de391c"
106
- )
107
  if st.button("Save"):
108
 
109
  mask = canvas_to_mask(canvas_result, img_array.shape)
@@ -111,9 +146,10 @@ if st.button("Save"):
111
  mask = fill_enclosed_areas(mask)
112
  cur = generate_short_hash()
113
  cv2.imwrite(f"data/{cur}.png", mask)
114
- uploaded_image = im.upload_image(f"data/{cur}.png", title="Data Backup")
115
- st.markdown(uploaded_image.title)
116
- st.markdown(uploaded_image.link)
 
117
  else:
118
  st.warning("Please draw on the image.")
119
 
@@ -122,7 +158,7 @@ if st.button("Aggregate data"):
122
  mean_mask = calculate_mean_mask("data")
123
 
124
  if mean_mask is not None:
125
- final_image = overlay_mask("img/map.png", mean_mask)
126
 
127
  st.image(final_image, caption='Where most people think the CBD is', use_column_width=True)
128
 
@@ -137,4 +173,5 @@ if st.button("Aggregate data"):
137
 
138
 
139
  if st.button("Clear data"):
140
- delete_files_in_folder("data")
 
 
1
  import streamlit as st
2
  from streamlit_drawable_canvas import st_canvas
3
+
4
+ from dropbox.files import WriteMode
5
+
6
  from PIL import Image
7
  import numpy as np
8
  import cv2
9
  import hashlib
10
+ import dropbox
11
  import time
12
+ import hmac
13
  import os
14
+ st.set_page_config(layout="wide")
15
+ dbx = dropbox.Dropbox(st.secrets["dbx"])
16
+ password = st.secrets["dbx"]
17
 
18
  # Function to convert the canvas drawing to a binary mask
19
  def canvas_to_mask(canvas_result, img_shape):
 
86
  print(f"Error deleting files: {e}")
87
 
88
 
89
+ def check_password():
90
+ """Returns `True` if the user had the correct password."""
91
 
92
+ def password_entered():
93
+ """Checks whether a password entered by the user is correct."""
94
+ if hmac.compare_digest(st.session_state["password"], password):
95
+ st.session_state["password_correct"] = True
96
+ del st.session_state["password"] # Don't store the password.
97
+ else:
98
+ st.session_state["password_correct"] = False
99
+
100
+ # Return True if the password is validated.
101
+ if st.session_state.get("password_correct", False):
102
+ return True
103
+
104
+ # Show input for password.
105
+ st.text_input(
106
+ "Password", type="password", on_change=password_entered, key="password"
107
+ )
108
+ if "password_correct" in st.session_state:
109
+ st.error("😕 Password incorrect")
110
+ return False
111
 
112
+ def backup(LOCALFILE, BACKUPPATH):
113
+ with open(LOCALFILE, 'rb') as f:
114
+ # We use WriteMode=overwrite to make sure that the settings in the file
115
+ # are changed on upload
116
+ print("Uploading " + LOCALFILE + " to Dropbox as " + BACKUPPATH + "...")
117
+ dbx.files_upload(f.read(), BACKUPPATH, mode=WriteMode('overwrite'))
118
 
119
+ st.title("IB Geography Survey")
120
+
121
+ # Upload an image
122
+
123
+ image = Image.open("img/dlmap2.png").convert("RGB")
124
  img_array = np.array(image)
 
125
 
126
  # Create a canvas for drawing
127
+ st.subheader("Highlight the central business district:")
128
  canvas_result = st_canvas(
129
  fill_color="rgba(255, 165, 0, 0.7)", # Use an orange, semi-transparent fill
130
+ stroke_width=5,
131
  stroke_color="rgba(255, 165, 0, 0.7)",
132
+ background_image=Image.open("img/dlmap2.png"),
133
  update_streamlit=True,
134
  height=img_array.shape[0],
135
  width=img_array.shape[1],
136
  drawing_mode="freedraw",
137
  key="canvas",
138
  )
139
+ #im = pyimgur.Imgur(
140
+ # "253ebfef9de391c"
141
+ #)
142
  if st.button("Save"):
143
 
144
  mask = canvas_to_mask(canvas_result, img_array.shape)
 
146
  mask = fill_enclosed_areas(mask)
147
  cur = generate_short_hash()
148
  cv2.imwrite(f"data/{cur}.png", mask)
149
+ # uploaded_image = im.upload_image(f"data/{cur}.png", title="Data Backup")
150
+ # st.markdown(uploaded_image.title)
151
+ # st.markdown(uploaded_image.link)
152
+ backup(f"data/{cur}.png", f"/geo/{cur}.png")
153
  else:
154
  st.warning("Please draw on the image.")
155
 
 
158
  mean_mask = calculate_mean_mask("data")
159
 
160
  if mean_mask is not None:
161
+ final_image = overlay_mask("img/dlmap2.png", mean_mask)
162
 
163
  st.image(final_image, caption='Where most people think the CBD is', use_column_width=True)
164
 
 
173
 
174
 
175
  if st.button("Clear data"):
176
+ if check_password():
177
+ delete_files_in_folder("data")