Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# ---------------- CONFIG ----------------
|
| 9 |
+
st.set_page_config(page_title="Pothole Detection", layout="wide")
|
| 10 |
+
st.title("🕳️ Pothole Detection using YOLO")
|
| 11 |
+
st.write("Upload an image — the model will detect potholes and mark them.")
|
| 12 |
+
|
| 13 |
+
# -------- Load YOLO Model --------------
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_model():
|
| 16 |
+
try:
|
| 17 |
+
model = YOLO("best.pt") # your model file
|
| 18 |
+
return model
|
| 19 |
+
except Exception as e:
|
| 20 |
+
st.error(f"Failed to load model: {e}")
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
model = load_model()
|
| 24 |
+
|
| 25 |
+
if model is None:
|
| 26 |
+
st.stop()
|
| 27 |
+
|
| 28 |
+
# -------- File Upload ------------------
|
| 29 |
+
uploaded_file = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
|
| 30 |
+
|
| 31 |
+
if uploaded_file:
|
| 32 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 33 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 34 |
+
|
| 35 |
+
with st.spinner("Detecting potholes... ⏳"):
|
| 36 |
+
# Save temp file
|
| 37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
|
| 38 |
+
image.save(tmp.name)
|
| 39 |
+
results = model(tmp.name)
|
| 40 |
+
|
| 41 |
+
# Render result image
|
| 42 |
+
result_img = results[0].plot() # numpy array (BGR)
|
| 43 |
+
|
| 44 |
+
# Convert BGR to RGB
|
| 45 |
+
result_img_rgb = Image.fromarray(result_img[..., ::-1])
|
| 46 |
+
|
| 47 |
+
st.image(result_img_rgb, caption="Detected Potholes ✅", use_container_width=True)
|
| 48 |
+
|
| 49 |
+
# Download button
|
| 50 |
+
result_path = "output_pothole.jpg"
|
| 51 |
+
result_img_rgb.save(result_path)
|
| 52 |
+
|
| 53 |
+
with open(result_path, "rb") as f:
|
| 54 |
+
st.download_button("📥 Download Result", f, file_name="pothole_detected.jpg")
|