Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,33 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
-
import cv2
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from ultralytics import YOLO
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# Load YOLO model
|
| 12 |
-
# @st.cache_resource
|
| 13 |
-
def load_model():
|
| 14 |
-
# Dynamically select device (GPU if available, otherwise CPU)
|
| 15 |
-
# device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 16 |
-
model = YOLO('./best .pt').to(device) # Replace with your model path
|
| 17 |
-
return model
|
| 18 |
-
|
| 19 |
-
model = load_model()
|
| 20 |
|
| 21 |
-
# Function to make predictions and draw bounding boxes
|
| 22 |
def predict_and_draw(image):
|
| 23 |
-
# Convert PIL image to OpenCV format
|
| 24 |
img = np.array(image)
|
| 25 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 26 |
-
|
| 27 |
-
# Resize image to YOLO input size (640x640)
|
| 28 |
img_resized = cv2.resize(img, (640, 640))
|
| 29 |
-
|
| 30 |
-
# Perform prediction
|
| 31 |
results = model(img_resized)
|
| 32 |
|
| 33 |
-
# Access the first result
|
| 34 |
result = results[0]
|
| 35 |
-
boxes = result.boxes
|
| 36 |
-
class_names = model.names
|
| 37 |
-
img_with_boxes = img_resized.copy()
|
| 38 |
-
defect_list = []
|
| 39 |
|
| 40 |
-
# Draw bounding boxes and labels on the image
|
| 41 |
for box in boxes:
|
| 42 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 43 |
-
conf = box.conf[0].item()
|
| 44 |
-
cls = int(box.cls[0].item())
|
| 45 |
-
label = f"{class_names[cls]} ({conf:.2f})"
|
| 46 |
-
|
| 47 |
-
defect_list.append(f"{class_names[cls]} - Confidence: {conf:.2f}") # Add to list
|
| 48 |
-
|
| 49 |
-
# Draw rectangle and label
|
| 50 |
cv2.rectangle(img_with_boxes, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 51 |
cv2.putText(
|
| 52 |
img_with_boxes,
|
|
@@ -57,57 +38,26 @@ def predict_and_draw(image):
|
|
| 57 |
(255, 0, 0),
|
| 58 |
2,
|
| 59 |
)
|
| 60 |
-
|
| 61 |
-
# Convert back to PIL for Streamlit display
|
| 62 |
img_with_boxes = cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 63 |
return Image.fromarray(img_with_boxes), defect_list
|
| 64 |
|
| 65 |
-
# Streamlit app
|
| 66 |
-
st.title("
|
| 67 |
-
st.markdown("Upload an image of a road to detect defects
|
| 68 |
|
| 69 |
-
|
| 70 |
-
uploaded_file = st.file_uploader("Upload an Image (JPG/PNG)", type=["jpg", "jpeg", "png"])
|
| 71 |
|
| 72 |
if uploaded_file:
|
| 73 |
-
|
| 74 |
-
col1, col2 = st.columns([1, 1]) # Equal width for input and output columns
|
| 75 |
-
|
| 76 |
with col1:
|
| 77 |
-
st.subheader("Uploaded Image")
|
| 78 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
| 79 |
|
| 80 |
-
# Show a button for detection
|
| 81 |
if st.button("Detect Defects"):
|
| 82 |
-
with st.spinner("Detecting defects...
|
| 83 |
-
# Show progress bar
|
| 84 |
-
progress_bar = st.progress(0)
|
| 85 |
result_image, defect_list = predict_and_draw(Image.open(uploaded_file))
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
# Display result image with bounding boxes
|
| 91 |
-
with col2:
|
| 92 |
-
st.subheader("Detected Defects")
|
| 93 |
-
st.image(result_image, caption="Detected Defects", use_container_width=True)
|
| 94 |
-
|
| 95 |
-
# Display detected defects with confidence scores
|
| 96 |
-
st.subheader("Detected Defects Details:")
|
| 97 |
-
if defect_list:
|
| 98 |
-
for defect in defect_list:
|
| 99 |
-
st.write(f"- {defect}")
|
| 100 |
-
else:
|
| 101 |
-
st.write("No defects detected.")
|
| 102 |
-
|
| 103 |
-
else:
|
| 104 |
-
st.warning("Click on 'Detect Defects' to analyze the image.")
|
| 105 |
-
else:
|
| 106 |
-
st.info("Please upload an image to begin detection.")
|
| 107 |
-
|
| 108 |
-
# Add some footer information
|
| 109 |
-
st.markdown("""
|
| 110 |
-
---
|
| 111 |
-
🛠️ This app helps detect road defects using YOLO model.
|
| 112 |
-
📩 For feedback, contact us at: vaman2425@gmail.com
|
| 113 |
-
""")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
from ultralytics import YOLO
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
|
| 9 |
+
# Dynamically load model from Hugging Face Hub
|
| 10 |
+
model_path = hf_hub_download(repo_id="your-username/your-model-repo", filename="best .pt")
|
| 11 |
+
model = YOLO(model_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
|
|
|
| 13 |
def predict_and_draw(image):
|
|
|
|
| 14 |
img = np.array(image)
|
| 15 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
| 16 |
img_resized = cv2.resize(img, (640, 640))
|
|
|
|
|
|
|
| 17 |
results = model(img_resized)
|
| 18 |
|
|
|
|
| 19 |
result = results[0]
|
| 20 |
+
boxes = result.boxes
|
| 21 |
+
class_names = model.names
|
| 22 |
+
img_with_boxes = img_resized.copy()
|
| 23 |
+
defect_list = []
|
| 24 |
|
|
|
|
| 25 |
for box in boxes:
|
| 26 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())
|
| 27 |
+
conf = box.conf[0].item()
|
| 28 |
+
cls = int(box.cls[0].item())
|
| 29 |
+
label = f"{class_names[cls]} ({conf:.2f})"
|
| 30 |
+
defect_list.append(f"{class_names[cls]} - Confidence: {conf:.2f}")
|
|
|
|
|
|
|
|
|
|
| 31 |
cv2.rectangle(img_with_boxes, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 32 |
cv2.putText(
|
| 33 |
img_with_boxes,
|
|
|
|
| 38 |
(255, 0, 0),
|
| 39 |
2,
|
| 40 |
)
|
|
|
|
|
|
|
| 41 |
img_with_boxes = cv2.cvtColor(img_with_boxes, cv2.COLOR_BGR2RGB)
|
| 42 |
return Image.fromarray(img_with_boxes), defect_list
|
| 43 |
|
| 44 |
+
# Streamlit app logic
|
| 45 |
+
st.title("Road Defect Detection App")
|
| 46 |
+
st.markdown("Upload an image of a road to detect defects.")
|
| 47 |
|
| 48 |
+
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
|
|
|
|
| 49 |
|
| 50 |
if uploaded_file:
|
| 51 |
+
col1, col2 = st.columns([1, 1])
|
|
|
|
|
|
|
| 52 |
with col1:
|
|
|
|
| 53 |
st.image(uploaded_file, caption="Uploaded Image", use_container_width=True)
|
| 54 |
|
|
|
|
| 55 |
if st.button("Detect Defects"):
|
| 56 |
+
with st.spinner("Detecting defects..."):
|
|
|
|
|
|
|
| 57 |
result_image, defect_list = predict_and_draw(Image.open(uploaded_file))
|
| 58 |
+
with col2:
|
| 59 |
+
st.image(result_image, caption="Detected Defects", use_container_width=True)
|
| 60 |
|
| 61 |
+
st.subheader("Detected Defects:")
|
| 62 |
+
for defect in defect_list:
|
| 63 |
+
st.write(f"- {defect}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|