Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,158 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
if
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import cv2
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import supervision as sv
|
| 9 |
+
from streamlit_webrtc import webrtc_streamer, WebRtcMode
|
| 10 |
+
import functools
|
| 11 |
+
|
| 12 |
+
# --- Page Configuration ---
|
| 13 |
+
st.set_page_config(
|
| 14 |
+
page_title="Traffic Lane and Object Detection",
|
| 15 |
+
page_icon=":camera_video:",
|
| 16 |
+
layout="wide",
|
| 17 |
+
initial_sidebar_state="expanded",
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# --- Sidebar ---
|
| 21 |
+
st.sidebar.header("Traffic Lane and Object Detection Options")
|
| 22 |
+
source_type = st.sidebar.radio("Select Input Source:", ("Image", "Video", "Live Camera Feed")) # Added Live Camera Feed
|
| 23 |
+
confidence_threshold = st.sidebar.slider(
|
| 24 |
+
"Confidence Threshold", min_value=0.0, max_value=1.0, value=0.25, step=0.05
|
| 25 |
+
)
|
| 26 |
+
iou_threshold = st.sidebar.slider(
|
| 27 |
+
"IoU Threshold", min_value=0.0, max_value=1.0, value=0.45, step=0.05, help="Intersection over Union threshold for NMS"
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# --- Load YOLO Model ---
|
| 32 |
+
@st.cache_resource
|
| 33 |
+
def load_model():
|
| 34 |
+
model = YOLO("yolov8n.pt") # Load a pretrained YOLOv8n model
|
| 35 |
+
return model
|
| 36 |
+
|
| 37 |
+
model = load_model()
|
| 38 |
+
|
| 39 |
+
# --- Functions ---
|
| 40 |
+
def detect_lanes_and_objects_image(image, model, confidence_threshold, iou_threshold):
|
| 41 |
+
"""Runs YOLO on a single image with Supervision."""
|
| 42 |
+
img_np = np.array(image)
|
| 43 |
+
results = model(img_np, conf=confidence_threshold, iou=iou_threshold)
|
| 44 |
+
|
| 45 |
+
detections = sv.Detections.from_ultralytics(results[0])
|
| 46 |
+
annotator = sv.BoxAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
|
| 47 |
+
annotated_frame = annotator.annotate(scene=img_np, detections=detections)
|
| 48 |
+
|
| 49 |
+
return annotated_frame
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def detect_lanes_and_objects_video(video_path, model, confidence_threshold, iou_threshold):
|
| 53 |
+
"""Runs YOLO on a video file with Supervision."""
|
| 54 |
+
video = cv2.VideoCapture(video_path)
|
| 55 |
+
frame_width = int(video.get(3))
|
| 56 |
+
frame_height = int(video.get(4))
|
| 57 |
+
fps = int(video.get(cv2.CAP_PROP_FPS))
|
| 58 |
+
codec = cv2.VideoWriter_fourcc(*"mp4v")
|
| 59 |
+
output_path = "output.mp4"
|
| 60 |
+
out = cv2.VideoWriter(output_path, codec, fps, (frame_width, frame_height))
|
| 61 |
+
stframe = st.empty()
|
| 62 |
+
|
| 63 |
+
while video.isOpened():
|
| 64 |
+
ret, frame = video.read()
|
| 65 |
+
if not ret:
|
| 66 |
+
break
|
| 67 |
+
|
| 68 |
+
results = model(frame, conf=confidence_threshold, iou=iou_threshold)
|
| 69 |
+
|
| 70 |
+
detections = sv.Detections.from_ultralytics(results[0])
|
| 71 |
+
annotator = sv.BoxAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
|
| 72 |
+
annotated_frame = annotator.annotate(scene=frame, detections=detections)
|
| 73 |
+
|
| 74 |
+
out.write(annotated_frame)
|
| 75 |
+
stframe.image(annotated_frame, channels="BGR", use_column_width=True)
|
| 76 |
+
|
| 77 |
+
video.release()
|
| 78 |
+
out.release()
|
| 79 |
+
cv2.destroyAllWindows()
|
| 80 |
+
|
| 81 |
+
return output_path
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def process_frame(frame, model, confidence_threshold, iou_threshold):
|
| 85 |
+
"""Process each frame from the webcam with Supervision."""
|
| 86 |
+
img = frame.to_ndarray(format="bgr24")
|
| 87 |
+
results = model(img, conf=confidence_threshold, iou=iou_threshold)
|
| 88 |
+
|
| 89 |
+
detections = sv.Detections.from_ultralytics(results[0])
|
| 90 |
+
annotator = sv.BoxAnnotator(thickness=2, text_thickness=1, text_scale=0.5)
|
| 91 |
+
annotated_frame = annotator.annotate(scene=img, detections=detections)
|
| 92 |
+
|
| 93 |
+
return annotated_frame
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# --- Main Application ---
|
| 97 |
+
st.title("Traffic Lane and Object Detection")
|
| 98 |
+
|
| 99 |
+
if source_type == "Image":
|
| 100 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 101 |
+
|
| 102 |
+
if uploaded_file is not None:
|
| 103 |
+
image = Image.open(uploaded_file)
|
| 104 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 105 |
+
|
| 106 |
+
if st.button("Run Detection"):
|
| 107 |
+
with st.spinner("Running YOLOv8..."):
|
| 108 |
+
detected_image = detect_lanes_and_objects_image(
|
| 109 |
+
image, model, confidence_threshold, iou_threshold
|
| 110 |
+
)
|
| 111 |
+
st.image(detected_image, caption="Detected Image", use_column_width=True)
|
| 112 |
+
|
| 113 |
+
elif source_type == "Video":
|
| 114 |
+
uploaded_video = st.file_uploader("Upload a video", type=["mp4", "avi", "mov"])
|
| 115 |
+
|
| 116 |
+
if uploaded_video is not None:
|
| 117 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
| 118 |
+
tfile.write(uploaded_video.read())
|
| 119 |
+
video_path = tfile.name
|
| 120 |
+
|
| 121 |
+
if st.button("Run Detection"):
|
| 122 |
+
with st.spinner("Running YOLOv8 on video..."):
|
| 123 |
+
output_video_path = detect_lanes_and_objects_video(
|
| 124 |
+
video_path, model, confidence_threshold, iou_threshold
|
| 125 |
+
)
|
| 126 |
+
st.video(output_video_path)
|
| 127 |
+
tfile.close()
|
| 128 |
+
os.unlink(video_path)
|
| 129 |
+
os.remove(output_video_path)
|
| 130 |
+
|
| 131 |
+
elif source_type == "Live Camera Feed":
|
| 132 |
+
st.subheader("Live Camera Feed")
|
| 133 |
+
|
| 134 |
+
if model is None:
|
| 135 |
+
st.error("YOLO model is not loaded. Cannot run live feed.")
|
| 136 |
+
st.stop()
|
| 137 |
+
|
| 138 |
+
custom_process_frame = functools.partial(
|
| 139 |
+
process_frame,
|
| 140 |
+
model=model,
|
| 141 |
+
confidence_threshold=confidence_threshold,
|
| 142 |
+
iou_threshold=iou_threshold,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
webrtc_streamer(
|
| 146 |
+
key="live-feed",
|
| 147 |
+
video_frame_callback=custom_process_frame,
|
| 148 |
+
mode=WebRtcMode.SENDRECV,
|
| 149 |
+
media_stream_constraints={"video": True, "audio": False},
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
st.markdown("---")
|
| 154 |
+
st.markdown(
|
| 155 |
+
"""
|
| 156 |
+
**Note:** This example uses YOLOv8 for object detection. Lane detection is a more complex task and requires additional image processing techniques. This is a simplified demo and will likely not perform well on complex or noisy video.
|
| 157 |
+
"""
|
| 158 |
+
)
|