Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,55 @@
|
|
| 1 |
-
import
|
| 2 |
-
import streamlit as st
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
| 9 |
-
|
| 10 |
-
# Define a function to detect faces in a frame
|
| 11 |
-
def detect_faces(frame):
|
| 12 |
-
# Convert the frame to grayscale (Haar Cascade works on grayscale images)
|
| 13 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 14 |
-
|
| 15 |
-
# Detect faces in the image
|
| 16 |
-
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
|
| 33 |
-
if not
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Start capturing video frames
|
| 37 |
-
while True:
|
| 38 |
-
ret, frame = cap.read()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
break
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
| 1 |
+
import torch
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
from PIL import Image
|
| 4 |
+
from transformers import AutoImageProcessor, AutoModelForDepthEstimation
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Load model and image processor
|
| 9 |
+
image_processor = AutoImageProcessor.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
| 10 |
+
model = AutoModelForDepthEstimation.from_pretrained("depth-anything/Depth-Anything-V2-Small-hf")
|
| 11 |
|
| 12 |
+
# Set the device for model (CUDA if available)
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
model.to(device)
|
| 15 |
|
| 16 |
+
# Use FP16 if available (half precision for speed)
|
| 17 |
+
if torch.cuda.is_available():
|
| 18 |
+
model = model.half()
|
| 19 |
|
| 20 |
+
# Streamlit App
|
| 21 |
+
st.title("Depth Estimation from Webcam")
|
| 22 |
|
| 23 |
+
# Capture image from webcam
|
| 24 |
+
image_data = st.camera_input("Capture an image")
|
| 25 |
|
| 26 |
+
if image_data is not None:
|
| 27 |
+
# Convert the captured image data to a PIL image
|
| 28 |
+
image = Image.open(image_data)
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Prepare the image for the model
|
| 31 |
+
inputs = image_processor(images=image, return_tensors="pt").to(device)
|
|
|
|
| 32 |
|
| 33 |
+
# Model inference (no gradients needed)
|
| 34 |
+
with torch.no_grad():
|
| 35 |
+
outputs = model(**inputs)
|
| 36 |
+
predicted_depth = outputs.predicted_depth
|
| 37 |
|
| 38 |
+
# Interpolate depth map to match the image's dimensions
|
| 39 |
+
prediction = torch.nn.functional.interpolate(
|
| 40 |
+
predicted_depth.unsqueeze(1),
|
| 41 |
+
size=(image.height, image.width), # Match the image's dimensions
|
| 42 |
+
mode="bicubic",
|
| 43 |
+
align_corners=False,
|
| 44 |
+
)
|
| 45 |
|
| 46 |
+
# Convert depth map to numpy for visualization
|
| 47 |
+
depth_map = prediction.squeeze().cpu().numpy()
|
| 48 |
|
| 49 |
+
# Normalize depth map for display (visualization purposes)
|
| 50 |
+
depth_map_normalized = np.uint8(depth_map / np.max(depth_map) * 255)
|
| 51 |
+
depth_map_colored = cv2.applyColorMap(depth_map_normalized, cv2.COLORMAP_JET)
|
| 52 |
|
| 53 |
+
# Display the original image and the depth map in Streamlit
|
| 54 |
+
st.image(image, caption="Captured Image", use_column_width=True)
|
| 55 |
+
st.image(depth_map_colored, caption="Depth Map", use_column_width=True)
|