Spaces:
Running
Running
Upload newstreamlit_app.py
Browse files- newstreamlit_app.py +127 -0
newstreamlit_app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import keras
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import urllib.request
|
7 |
+
st.set_page_config(layout="wide")
|
8 |
+
|
9 |
+
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
.block-container {
|
13 |
+
padding-top: 1rem;
|
14 |
+
padding-bottom: 0rem;
|
15 |
+
padding-left: 5rem;
|
16 |
+
padding-right: 5rem;
|
17 |
+
}
|
18 |
+
</style>
|
19 |
+
""", unsafe_allow_html=True)
|
20 |
+
|
21 |
+
#title
|
22 |
+
col1, col2 = st.columns(2)
|
23 |
+
with col1:
|
24 |
+
buffer, col = st.columns(2)
|
25 |
+
with col:
|
26 |
+
st.header('Overpass Identifier')
|
27 |
+
with col2:
|
28 |
+
buffer1, col, buffer2 = st.columns(3)
|
29 |
+
with col:
|
30 |
+
st.image('overpass.jpg', width = 190)
|
31 |
+
|
32 |
+
st.write("---")
|
33 |
+
|
34 |
+
#load model and initialize image size required by model. uploaded images are resized to indicated size
|
35 |
+
img_height = 640
|
36 |
+
img_width = 640
|
37 |
+
|
38 |
+
state = st.session_state
|
39 |
+
|
40 |
+
if "loaded_model" not in state:
|
41 |
+
state.loaded_model = keras.models.load_model("0.0008-0.92.keras")
|
42 |
+
|
43 |
+
if "lat" not in state:
|
44 |
+
state.lat = 39.11
|
45 |
+
|
46 |
+
if "lng" not in state:
|
47 |
+
state.lng = -86.56
|
48 |
+
|
49 |
+
if "coords_submitted" not in state:
|
50 |
+
state.coords_submitted = False
|
51 |
+
|
52 |
+
#if "img_submitted" not in state:
|
53 |
+
#state.img_submitted = False
|
54 |
+
|
55 |
+
if "img" not in state:
|
56 |
+
state.img = None
|
57 |
+
|
58 |
+
col1, col2, col3 = st.columns(3)
|
59 |
+
with col3:
|
60 |
+
#header
|
61 |
+
st.subheader('Enter latitude/longitude coordinates:')
|
62 |
+
|
63 |
+
coll, colr= st.columns(2)
|
64 |
+
with coll:
|
65 |
+
state.lat = st.number_input('Latitude', value=39.11, min_value=-90., max_value=90., step=.01)
|
66 |
+
st.write('The current lat/long are:')
|
67 |
+
with colr:
|
68 |
+
state.lng = st.number_input('Longitude', value=-86.56, min_value=-180., max_value=180., step=.01)
|
69 |
+
st.write(str(state.lat)+', '+str(state.lng))
|
70 |
+
with st.form("my_form"):
|
71 |
+
submit_button = st.form_submit_button(
|
72 |
+
label="Get Image and Prediction", on_click=lambda: state.update(coords_submitted=True))
|
73 |
+
#header
|
74 |
+
#st.subheader('Upload a satellite image:')
|
75 |
+
#img_buffer = st.file_uploader("Upload a satellite image file (format: .png, .jpeg, or .jpg).",type=['png', 'jpeg', 'jpg'])
|
76 |
+
#if img_buffer is not None:
|
77 |
+
#state.img = Image.open(img_buffer).convert("RGB")
|
78 |
+
|
79 |
+
|
80 |
+
with col2:
|
81 |
+
if state.coords_submitted:
|
82 |
+
state.coords_submitted = False
|
83 |
+
try:
|
84 |
+
url = "https://maps.googleapis.com/maps/api/staticmap?center="+str(state.lat)+","+str(state.lng)+"&zoom=16&size=640x640&maptype=satellite&key=AIzaSyCzzVb_qf0TQgLw3K2y5EE6geyzE6KzQuA"
|
85 |
+
buffer = io.BytesIO(urllib.request.urlopen(url).read())
|
86 |
+
state.img = Image.open(buffer).convert("RGB")
|
87 |
+
except Exception as e:
|
88 |
+
st.write("Error! Could not access Google Static API. Error code:",e)
|
89 |
+
if state.img is not None:
|
90 |
+
st.image(state.img, use_column_width = True)
|
91 |
+
|
92 |
+
with col1:
|
93 |
+
st.subheader("Prediction")
|
94 |
+
if state.img is not None:
|
95 |
+
img_array = np.array(state.img)
|
96 |
+
batch_size = 1
|
97 |
+
img_array = np.reshape(img_array,[batch_size,img_height,img_width,3])
|
98 |
+
result = state.loaded_model.predict(img_array)
|
99 |
+
crossing_chance = result[0][1]*100
|
100 |
+
status = None
|
101 |
+
while status is None:
|
102 |
+
if crossing_chance >= 0:
|
103 |
+
status = "extremely un"
|
104 |
+
if crossing_chance >= 10:
|
105 |
+
status = "highly un"
|
106 |
+
if crossing_chance >= 20:
|
107 |
+
status = "pretty un"
|
108 |
+
if crossing_chance >= 30:
|
109 |
+
status = "slightly un"
|
110 |
+
if crossing_chance >= 40:
|
111 |
+
status = "a tiny bit more unlikely than "
|
112 |
+
if crossing_chance >= 50:
|
113 |
+
status = "a tiny bit more likely than un"
|
114 |
+
if crossing_chance >= 60:
|
115 |
+
status = "slightly "
|
116 |
+
if crossing_chance >= 70:
|
117 |
+
status = "pretty "
|
118 |
+
if crossing_chance >= 80:
|
119 |
+
status = "highly "
|
120 |
+
if crossing_chance >= 90:
|
121 |
+
status = "extremely "
|
122 |
+
|
123 |
+
|
124 |
+
st.write(f"It's {status}likely there's at least one overpass here.")
|
125 |
+
st.write("")
|
126 |
+
st.write(f"In fact, the likelihood of at least one overpass is {np.round(crossing_chance,decimals=2)}%.")
|
127 |
+
|