Spaces:
Sleeping
Sleeping
Delete pages/5_Live_Webcam.py
Browse files- pages/5_Live_Webcam.py +0 -71
pages/5_Live_Webcam.py
DELETED
|
@@ -1,71 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import cv2
|
| 3 |
-
import time
|
| 4 |
-
from ultralytics import YOLO
|
| 5 |
-
|
| 6 |
-
st.title("Live Webcam Object Detection")
|
| 7 |
-
|
| 8 |
-
# -----------------------------
|
| 9 |
-
# Load YOLO Model
|
| 10 |
-
# -----------------------------
|
| 11 |
-
@st.cache_resource
|
| 12 |
-
def load_model():
|
| 13 |
-
return YOLO("models/best.pt")
|
| 14 |
-
|
| 15 |
-
model = load_model()
|
| 16 |
-
|
| 17 |
-
# -----------------------------
|
| 18 |
-
# Sidebar Settings
|
| 19 |
-
# -----------------------------
|
| 20 |
-
confidence = st.sidebar.slider("Confidence Threshold", 0.1, 1.0, 0.4)
|
| 21 |
-
|
| 22 |
-
start = st.button("Start Webcam")
|
| 23 |
-
stop = st.button("Stop Webcam")
|
| 24 |
-
|
| 25 |
-
frame_placeholder = st.empty()
|
| 26 |
-
fps_placeholder = st.empty()
|
| 27 |
-
|
| 28 |
-
# -----------------------------
|
| 29 |
-
# Webcam Logic
|
| 30 |
-
# -----------------------------
|
| 31 |
-
if start:
|
| 32 |
-
|
| 33 |
-
cap = cv2.VideoCapture(0)
|
| 34 |
-
|
| 35 |
-
if not cap.isOpened():
|
| 36 |
-
st.error("Webcam not detected.")
|
| 37 |
-
else:
|
| 38 |
-
st.success("Webcam started. Press Stop to end.")
|
| 39 |
-
|
| 40 |
-
prev_time = 0
|
| 41 |
-
|
| 42 |
-
while cap.isOpened():
|
| 43 |
-
|
| 44 |
-
ret, frame = cap.read()
|
| 45 |
-
if not ret:
|
| 46 |
-
st.warning("Failed to read frame.")
|
| 47 |
-
break
|
| 48 |
-
|
| 49 |
-
# YOLO Prediction
|
| 50 |
-
results = model.predict(frame, conf=confidence, verbose=False)
|
| 51 |
-
annotated_frame = results[0].plot()
|
| 52 |
-
|
| 53 |
-
# FPS Calculation
|
| 54 |
-
current_time = time.time()
|
| 55 |
-
fps = 1 / (current_time - prev_time) if prev_time != 0 else 0
|
| 56 |
-
prev_time = current_time
|
| 57 |
-
|
| 58 |
-
# Display Frame
|
| 59 |
-
frame_placeholder.image(
|
| 60 |
-
annotated_frame,
|
| 61 |
-
channels="BGR",
|
| 62 |
-
width="stretch"
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
fps_placeholder.write(f"FPS: {fps:.2f}")
|
| 66 |
-
|
| 67 |
-
if stop:
|
| 68 |
-
break
|
| 69 |
-
|
| 70 |
-
cap.release()
|
| 71 |
-
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|