road_project / app.py
jableable's picture
Update app.py
df6ee5c verified
import streamlit as st
import keras
import numpy as np
from PIL import Image
import io
import urllib.request
import os
st.set_page_config(layout="wide")
st.markdown("""
<style>
.block-container {
padding-top: 1rem;
padding-bottom: 0rem;
padding-left: 5rem;
padding-right: 5rem;
}
</style>
""", unsafe_allow_html=True)
#title
col1, col2 = st.columns(2)
with col1:
buffer, col = st.columns(2)
with col:
st.header('Overpass Identifier')
with col2:
buffer1, col, buffer2 = st.columns(3)
with col:
st.image('overpass.png')
st.write("---")
#load model and initialize image size required by model. uploaded images are resized to indicated size
img_height = 640
img_width = 640
state = st.session_state
if "loaded_model" not in state:
with st.spinner('Loading model. This may take a few seconds...'):
state.loaded_model = keras.models.load_model("0.0008-0.92.keras")
if "lat" not in state:
state.lat = 39.11
if "lng" not in state:
state.lng = -86.56
if "coords_submitted" not in state:
state.coords_submitted = False
#if "img_submitted" not in state:
#state.img_submitted = False
if "img" not in state:
state.img = None
col1, col2, col3 = st.columns(3)
with col3:
#header
st.subheader('Enter latitude/longitude coordinates:')
coll, colr= st.columns(2)
with coll:
state.lat = st.number_input('Latitude', value=39.11, min_value=-90., max_value=90., step=.01)
st.write('The current lat/long are:')
with colr:
state.lng = st.number_input('Longitude', value=-86.56, min_value=-180., max_value=180., step=.01)
st.write(str(state.lat)+', '+str(state.lng))
with st.form("my_form"):
submit_button = st.form_submit_button(
label="Get Image and Prediction", on_click=lambda: state.update(coords_submitted=True))
#header
#st.subheader('Upload a satellite image:')
#img_buffer = st.file_uploader("Upload a satellite image file (format: .png, .jpeg, or .jpg).",type=['png', 'jpeg', 'jpg'])
#if img_buffer is not None:
#state.img = Image.open(img_buffer).convert("RGB")
with col2:
if state.coords_submitted:
state.coords_submitted = False
try:
api_key = os.getenv("goog_api")
url = "https://maps.googleapis.com/maps/api/staticmap?center="+str(state.lat)+","+str(state.lng)+"&zoom=16&size=640x640&maptype=satellite&key="+api_key
buffer = io.BytesIO(urllib.request.urlopen(url).read())
state.img = Image.open(buffer).convert("RGB")
except Exception as e:
st.write("Error! Could not access Google Static API. Error code:",e)
if state.img is not None:
st.image(state.img, use_column_width = True)
with col1:
st.subheader("Prediction")
if state.img is not None:
img_array = np.array(state.img)
batch_size = 1
img_array = np.reshape(img_array,[batch_size,img_height,img_width,3])
result = state.loaded_model.predict(img_array)
crossing_chance = result[0][1]*100
status = None
while status is None:
if crossing_chance >= 0:
status = "extremely un"
if crossing_chance >= 10:
status = "highly un"
if crossing_chance >= 20:
status = "pretty un"
if crossing_chance >= 30:
status = "slightly un"
if crossing_chance >= 40:
status = "a tiny bit more unlikely than "
if crossing_chance >= 50:
status = "a tiny bit more likely than un"
if crossing_chance >= 60:
status = "slightly "
if crossing_chance >= 70:
status = "pretty "
if crossing_chance >= 80:
status = "highly "
if crossing_chance >= 90:
status = "extremely "
st.write(f"It's {status}likely there's at least one overpass here.")
st.write("")
st.write(f"In fact, the likelihood of at least one overpass is {np.round(crossing_chance,decimals=2)}%.")