jableable commited on
Commit
096e4b4
·
verified ·
1 Parent(s): 3dee844

Update new_app.py

Browse files
Files changed (1) hide show
  1. 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
- @st.cache_data(show_spinner=False, ttl=3600)
10
- def fetch_satellite_tile(lat, lng, zoom=16, size=(640, 640), api_key=""):
11
- if not api_key:
12
- raise RuntimeError("Google Static Maps API key is missing.")
13
- # Build URL
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
- # Fetch with timeout
20
- req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
21
- with urllib.request.urlopen(req, timeout=10) as resp:
22
- buffer = io.BytesIO(resp.read())
23
- return Image.open(buffer).convert("RGB")
24
-
25
- @st.cache_resource
26
- def get_model():
27
- # compile=False speeds loading; prefer model(...) over .predict for TF
28
- return keras.models.load_model("0.0008-0.92.keras", compile=False)
29
-
30
- def preprocess(img: Image.Image, size=(640, 640)) -> np.ndarray:
31
- img = img.resize(size, Image.BILINEAR)
32
- arr = np.asarray(img, dtype=np.float32)
33
- # adjust this normalization to match training
34
- arr = arr / 255.0
35
- return np.expand_dims(arr, axis=0)
36
-
37
- def status_text(pct: float) -> str:
38
- if pct >= 90:
39
- return "extremely likely"
40
- elif pct >= 60:
41
- return "likely"
42
- elif pct >= 40:
43
- return "a coin toss whether"
44
- elif pct >= 10:
45
- return "unlikely"
46
- else:
47
- return "extremely unlikely"
48
-
49
- st.markdown("""
50
- <style>
51
- .block-container { padding-top: 1rem; padding-bottom: 0rem; padding-left: 5rem; padding-right: 5rem; }
52
- </style>
53
- """, unsafe_allow_html=True)
54
-
55
- img_size = (640, 640)
56
- model = get_model()
57
-
58
- col1, col2 = st.columns(2)
59
- with col1:
60
- st.header("Overpass Identifier")
61
- with col2:
62
- st.image("overpass.png")
63
-
64
- st.write("---")
65
-
66
- # Defaults
67
- lat = st.session_state.get("lat", 39.11)
68
- lng = st.session_state.get("lng", -86.56)
69
-
70
- with st.form("coords"):
71
- st.subheader("Enter latitude/longitude coordinates:")
72
- c1, c2 = st.columns(2)
73
- with c1:
74
- lat = st.number_input("Latitude", value=float(lat), min_value=-90.0, max_value=90.0, step=0.01, format="%.4f")
75
- with c2:
76
- lng = st.number_input("Longitude", value=float(lng), min_value=-180.0, max_value=180.0, step=0.01, format="%.4f")
77
- submitted = st.form_submit_button("Get Image and Prediction")
78
-
79
- if submitted:
80
- st.session_state.lat, st.session_state.lng = lat, lng
81
- api_key = os.getenv("goog_api", "")
82
- try:
83
- with st.spinner("Fetching satellite tile..."):
84
- img = fetch_satellite_tile(lat, lng, size=img_size, api_key=api_key)
85
- st.image(img, caption=f"{lat:.4f}, {lng:.4f}", use_column_width=True)
86
-
87
- with st.spinner("Running inference..."):
88
- x = preprocess(img, size=img_size)
89
- # Prefer model(x, training=False) for speed; adjust index if your model output differs
90
- y = model(x, training=False).numpy()
91
- crossing_chance = float(y[0][1] * 100.0)
92
-
93
- status = status_text(crossing_chance)
94
- st.markdown(f"**It’s {status} that there’s an overpass here.**")
95
- st.write(f"In fact, the likelihood of at least one overpass is **{crossing_chance:.2f}%**.")
96
-
97
- except Exception as e:
98
- st.error(f"Error fetching image or running prediction: {e}")
 
 
 
 
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.")