Spaces:
Runtime error
Runtime error
kushaal457-lang commited on
Commit Β·
298865c
1
Parent(s): b249f21
Add UniDepth package locally and update requirements
Browse files
app.py
CHANGED
|
@@ -2,14 +2,21 @@ import streamlit as st
|
|
| 2 |
import cv2
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
-
from unidepth.models import UniDepthV2
|
| 6 |
from ultralytics import YOLO
|
| 7 |
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
|
| 8 |
-
from UniDepth.unidepth.models import UniDepthV2
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
st.set_page_config(page_title="YOLO + UniDepth Streaming", layout="wide")
|
| 11 |
-
st.title("π‘ YOLO + UniDepth V2 β Real 3D Distance (
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
@st.cache_resource
|
| 14 |
def load_depth_model():
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -24,6 +31,9 @@ def load_yolo():
|
|
| 24 |
depth_model, device = load_depth_model()
|
| 25 |
yolo_model = load_yolo()
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
def get_depth_points(rgb):
|
| 28 |
img_t = torch.from_numpy(rgb).float().permute(2, 0, 1).unsqueeze(0).to(device)/255.0
|
| 29 |
with torch.no_grad():
|
|
@@ -37,7 +47,9 @@ def process_frame(frame_bgr):
|
|
| 37 |
results = yolo_model(frame_bgr, verbose=False)[0]
|
| 38 |
for box in results.boxes:
|
| 39 |
conf = box.conf.item()
|
| 40 |
-
if conf < 0.5:
|
|
|
|
|
|
|
| 41 |
cls_id = int(box.cls.item())
|
| 42 |
label = yolo_model.names[cls_id]
|
| 43 |
|
|
@@ -48,15 +60,22 @@ def process_frame(frame_bgr):
|
|
| 48 |
distance = np.sqrt(X**2 + Y**2 + Z**2)
|
| 49 |
|
| 50 |
cv2.rectangle(frame_bgr, (x1,y1),(x2,y2),(0,255,0),2)
|
| 51 |
-
cv2.putText(frame_bgr,f"{label} {distance:.2f}m",(x1,y1-10),
|
|
|
|
| 52 |
|
| 53 |
return frame_bgr
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
class YOLODepthProcessor(VideoTransformerBase):
|
| 56 |
def transform(self, frame):
|
| 57 |
img = frame.to_ndarray(format="bgr24")
|
| 58 |
return process_frame(img)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
st.warning("This app runs in the cloud β your webcam stays private on your device.")
|
| 61 |
|
| 62 |
webrtc_streamer(
|
|
@@ -75,4 +94,3 @@ webrtc_streamer(
|
|
| 75 |
]
|
| 76 |
}
|
| 77 |
)
|
| 78 |
-
|
|
|
|
| 2 |
import cv2
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
|
|
|
|
| 7 |
|
| 8 |
+
# Import local UniDepth
|
| 9 |
+
from unidepth.models import UniDepthV2
|
| 10 |
+
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# Streamlit setup
|
| 13 |
+
# -----------------------------
|
| 14 |
st.set_page_config(page_title="YOLO + UniDepth Streaming", layout="wide")
|
| 15 |
+
st.title("π‘ YOLO + UniDepth V2 β Real 3D Distance (CPU Edition)")
|
| 16 |
|
| 17 |
+
# -----------------------------
|
| 18 |
+
# Load models (cached)
|
| 19 |
+
# -----------------------------
|
| 20 |
@st.cache_resource
|
| 21 |
def load_depth_model():
|
| 22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 31 |
depth_model, device = load_depth_model()
|
| 32 |
yolo_model = load_yolo()
|
| 33 |
|
| 34 |
+
# -----------------------------
|
| 35 |
+
# Helper functions
|
| 36 |
+
# -----------------------------
|
| 37 |
def get_depth_points(rgb):
|
| 38 |
img_t = torch.from_numpy(rgb).float().permute(2, 0, 1).unsqueeze(0).to(device)/255.0
|
| 39 |
with torch.no_grad():
|
|
|
|
| 47 |
results = yolo_model(frame_bgr, verbose=False)[0]
|
| 48 |
for box in results.boxes:
|
| 49 |
conf = box.conf.item()
|
| 50 |
+
if conf < 0.5:
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
cls_id = int(box.cls.item())
|
| 54 |
label = yolo_model.names[cls_id]
|
| 55 |
|
|
|
|
| 60 |
distance = np.sqrt(X**2 + Y**2 + Z**2)
|
| 61 |
|
| 62 |
cv2.rectangle(frame_bgr, (x1,y1),(x2,y2),(0,255,0),2)
|
| 63 |
+
cv2.putText(frame_bgr, f"{label} {distance:.2f}m", (x1,y1-10),
|
| 64 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,255,255), 2)
|
| 65 |
|
| 66 |
return frame_bgr
|
| 67 |
|
| 68 |
+
# -----------------------------
|
| 69 |
+
# WebRTC Video Processor
|
| 70 |
+
# -----------------------------
|
| 71 |
class YOLODepthProcessor(VideoTransformerBase):
|
| 72 |
def transform(self, frame):
|
| 73 |
img = frame.to_ndarray(format="bgr24")
|
| 74 |
return process_frame(img)
|
| 75 |
|
| 76 |
+
# -----------------------------
|
| 77 |
+
# WebRTC Streamlit Widget
|
| 78 |
+
# -----------------------------
|
| 79 |
st.warning("This app runs in the cloud β your webcam stays private on your device.")
|
| 80 |
|
| 81 |
webrtc_streamer(
|
|
|
|
| 94 |
]
|
| 95 |
}
|
| 96 |
)
|
|
|