Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,38 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
st.title("
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
|
| 6 |
+
# Load YOLO model (Download "yolov8n.pt" first if not available)
|
| 7 |
+
model = YOLO("yolov8n.pt")
|
| 8 |
|
| 9 |
+
st.title("🎥 YOLO Object Detection Live Stream")
|
| 10 |
|
| 11 |
+
# Open Webcam
|
| 12 |
+
cap = cv2.VideoCapture(0)
|
| 13 |
+
if not cap.isOpened():
|
| 14 |
+
st.error("Error: Could not access the webcam.")
|
| 15 |
+
st.stop()
|
| 16 |
|
| 17 |
+
stframe = st.empty() # Streamlit placeholder for video frames
|
| 18 |
+
|
| 19 |
+
while True:
|
| 20 |
+
ret, frame = cap.read()
|
| 21 |
+
if not ret:
|
| 22 |
+
st.error("Error: Failed to capture frame.")
|
| 23 |
+
break
|
| 24 |
+
|
| 25 |
+
# Convert frame to RGB (Streamlit expects RGB format)
|
| 26 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 27 |
+
|
| 28 |
+
# Run YOLO object detection
|
| 29 |
+
results = model(frame)
|
| 30 |
+
img_pot = result[0].plot()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Show processed frame in Streamlit
|
| 35 |
+
stframe.image(img_pot, channels="RGB")
|
| 36 |
+
|
| 37 |
+
# Release webcam
|
| 38 |
+
cap.release()
|