srivatsavdamaraju commited on
Commit
b010a27
·
verified ·
1 Parent(s): c7c40ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -43
app.py CHANGED
@@ -1,57 +1,55 @@
1
- import cv2
2
- import streamlit as st
3
  import numpy as np
4
  from PIL import Image
5
- import time
6
-
7
- # Load the pre-trained Haar Cascade face detector
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
- # Draw rectangles around the faces
19
- for (x, y, w, h) in faces:
20
- cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
21
 
22
- return frame
 
 
23
 
24
- # Streamlit UI for the app
25
- st.title("Real-Time Face Detection")
 
26
 
27
- # Create a container for the webcam video feed
28
- video_placeholder = st.empty()
29
 
30
- # Start the webcam feed
31
- cap = cv2.VideoCapture(0) # 0 is the default webcam device
32
 
33
- if not cap.isOpened():
34
- st.error("Error: Could not access the webcam.")
35
- else:
36
- # Start capturing video frames
37
- while True:
38
- ret, frame = cap.read()
39
 
40
- if not ret:
41
- st.error("Failed to grab frame.")
42
- break
43
 
44
- # Detect faces in the current frame
45
- result_frame = detect_faces(frame)
 
 
46
 
47
- # Convert BGR (OpenCV format) to RGB (for Streamlit display)
48
- result_frame_rgb = cv2.cvtColor(result_frame, cv2.COLOR_BGR2RGB)
 
 
 
 
 
49
 
50
- # Display the frame in Streamlit (dynamically updating the image)
51
- video_placeholder.image(result_frame_rgb, channels="RGB", use_column_width=True)
52
 
53
- # Add a small delay to control the frame rate (optional)
54
- time.sleep(0.03) # This gives roughly 30 FPS
 
55
 
56
- # Release the webcam when the stream ends
57
- cap.release()
 
 
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)