Spaces:
Running
Running
Update new_app.py
Browse files- new_app.py +101 -98
new_app.py
CHANGED
|
@@ -1,98 +1,101 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import keras
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
import io, os, urllib.request
|
| 6 |
-
|
| 7 |
-
st.set_page_config(layout="wide")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
size_str = f"{size[0]}x{size[1]}"
|
| 15 |
-
url = (
|
| 16 |
-
"https://maps.googleapis.com/maps/api/staticmap?"
|
| 17 |
-
f"center={lat},{lng}&zoom={zoom}&size={size_str}&maptype=satellite&scale=2&key={api_key}"
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return
|
| 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 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
with
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
st.
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import keras
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io, os, urllib.request
|
| 6 |
+
|
| 7 |
+
st.set_page_config(layout="wide")
|
| 8 |
+
|
| 9 |
+
# ---------- Caching & helpers ----------
|
| 10 |
+
@st.cache_data(show_spinner=False, ttl=3600)
|
| 11 |
+
def fetch_satellite_tile(lat, lng, zoom=16, size=(640, 640), api_key=""):
|
| 12 |
+
if not api_key:
|
| 13 |
+
raise RuntimeError("Google Static Maps API key is missing.")
|
| 14 |
+
size_str = f"{size[0]}x{size[1]}"
|
| 15 |
+
url = (
|
| 16 |
+
"https://maps.googleapis.com/maps/api/staticmap?"
|
| 17 |
+
f"center={lat},{lng}&zoom={zoom}&size={size_str}&maptype=satellite&scale=2&key={api_key}"
|
| 18 |
+
)
|
| 19 |
+
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
| 20 |
+
with urllib.request.urlopen(req, timeout=10) as resp:
|
| 21 |
+
buffer = io.BytesIO(resp.read())
|
| 22 |
+
return Image.open(buffer).convert("RGB")
|
| 23 |
+
|
| 24 |
+
@st.cache_resource
|
| 25 |
+
def get_model():
|
| 26 |
+
return keras.models.load_model("0.0008-0.92.keras", compile=False)
|
| 27 |
+
|
| 28 |
+
def preprocess(img: Image.Image, size=(640, 640)) -> np.ndarray:
|
| 29 |
+
img = img.resize(size, Image.BILINEAR)
|
| 30 |
+
arr = np.asarray(img, dtype=np.float32) / 255.0
|
| 31 |
+
return np.expand_dims(arr, axis=0)
|
| 32 |
+
|
| 33 |
+
def status_text(pct: float) -> str:
|
| 34 |
+
if pct >= 90: return "extremely likely"
|
| 35 |
+
if pct >= 60: return "likely"
|
| 36 |
+
if pct >= 40: return "a coin toss whether"
|
| 37 |
+
if pct >= 10: return "unlikely"
|
| 38 |
+
return "extremely unlikely"
|
| 39 |
+
|
| 40 |
+
# ---------- Styling ----------
|
| 41 |
+
st.markdown("""
|
| 42 |
+
<style>
|
| 43 |
+
.block-container { padding-top: 1rem; padding-bottom: 0rem; padding-left: 5rem; padding-right: 5rem; }
|
| 44 |
+
</style>
|
| 45 |
+
""", unsafe_allow_html=True)
|
| 46 |
+
|
| 47 |
+
# ---------- Header row (two columns) ----------
|
| 48 |
+
h1, h2 = st.columns([1,1], gap="large")
|
| 49 |
+
with h1:
|
| 50 |
+
st.header("Overpass Identifier")
|
| 51 |
+
with h2:
|
| 52 |
+
st.image("overpass.png")
|
| 53 |
+
|
| 54 |
+
st.write("---")
|
| 55 |
+
|
| 56 |
+
# Defaults
|
| 57 |
+
img_size = (640, 640)
|
| 58 |
+
model = get_model()
|
| 59 |
+
lat = st.session_state.get("lat", 39.11)
|
| 60 |
+
lng = st.session_state.get("lng", -86.56)
|
| 61 |
+
|
| 62 |
+
# ---------- Main content row (three columns: Pred | Image | Inputs) ----------
|
| 63 |
+
pred_col, img_col, form_col = st.columns([1,1,1], gap="large")
|
| 64 |
+
|
| 65 |
+
# Right: inputs (form to avoid mid-typing reruns)
|
| 66 |
+
with form_col:
|
| 67 |
+
st.subheader("Enter latitude/longitude coordinates:")
|
| 68 |
+
with st.form("coords"):
|
| 69 |
+
c1, c2 = st.columns(2)
|
| 70 |
+
with c1:
|
| 71 |
+
lat = st.number_input("Latitude", value=float(lat), min_value=-90.0, max_value=90.0, step=0.01, format="%.4f")
|
| 72 |
+
with c2:
|
| 73 |
+
lng = st.number_input("Longitude", value=float(lng), min_value=-180.0, max_value=180.0, step=0.01, format="%.4f")
|
| 74 |
+
submitted = st.form_submit_button("Get Image and Prediction")
|
| 75 |
+
|
| 76 |
+
# Center: image
|
| 77 |
+
img = None
|
| 78 |
+
if submitted:
|
| 79 |
+
st.session_state.lat, st.session_state.lng = lat, lng
|
| 80 |
+
try:
|
| 81 |
+
with st.spinner("Fetching satellite tile..."):
|
| 82 |
+
img = fetch_satellite_tile(lat, lng, size=img_size, api_key=os.getenv("goog_api", ""))
|
| 83 |
+
except Exception as e:
|
| 84 |
+
st.error(f"Error fetching image: {e}")
|
| 85 |
+
|
| 86 |
+
with img_col:
|
| 87 |
+
if img is not None:
|
| 88 |
+
st.image(img, caption=f"{lat:.4f}, {lng:.4f}", use_column_width=True)
|
| 89 |
+
|
| 90 |
+
# Left: prediction
|
| 91 |
+
with pred_col:
|
| 92 |
+
st.subheader("Prediction")
|
| 93 |
+
if img is not None:
|
| 94 |
+
with st.spinner("Running inference..."):
|
| 95 |
+
x = preprocess(img, size=img_size)
|
| 96 |
+
y = model(x, training=False).numpy() # adjust index if your model differs
|
| 97 |
+
crossing_chance = float(y[0][1] * 100.0)
|
| 98 |
+
st.markdown(f"**It’s {status_text(crossing_chance)} that there’s an overpass here.**")
|
| 99 |
+
st.write(f"Estimated probability: **{crossing_chance:.2f}%**.")
|
| 100 |
+
else:
|
| 101 |
+
st.caption("Submit coordinates to see the satellite image and prediction.")
|