Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -33,17 +33,29 @@ model = model.to(device)
|
|
33 |
processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
34 |
|
35 |
def preprocess_image(image):
|
36 |
-
image = cv2.resize(image, (
|
37 |
image = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float().to(device)
|
38 |
return image / 255.0
|
39 |
|
40 |
-
def plot_depth_map(depth_map,
|
41 |
fig = plt.figure(figsize=(16, 9))
|
42 |
ax = fig.add_subplot(111, projection='3d')
|
43 |
x, y = np.meshgrid(range(depth_map.shape[1]), range(depth_map.shape[0]))
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
ax.set_zlim(0, 1)
|
|
|
47 |
plt.close(fig)
|
48 |
|
49 |
fig.canvas.draw()
|
@@ -53,7 +65,7 @@ def plot_depth_map(depth_map, azimuth):
|
|
53 |
return img
|
54 |
|
55 |
@torch.inference_mode()
|
56 |
-
def process_frame(image
|
57 |
if image is None:
|
58 |
return None
|
59 |
preprocessed = preprocess_image(image)
|
@@ -62,11 +74,11 @@ def process_frame(image, azimuth):
|
|
62 |
|
63 |
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
64 |
|
65 |
-
return plot_depth_map(depth_map,
|
66 |
|
67 |
interface = gr.Interface(
|
68 |
fn=process_frame,
|
69 |
-
inputs=
|
70 |
outputs="image",
|
71 |
live=True
|
72 |
)
|
|
|
33 |
processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
34 |
|
35 |
def preprocess_image(image):
|
36 |
+
image = cv2.resize(image, (64, 36)) # 16:9 aspect ratio
|
37 |
image = torch.from_numpy(image).permute(2, 0, 1).unsqueeze(0).float().to(device)
|
38 |
return image / 255.0
|
39 |
|
40 |
+
def plot_depth_map(depth_map, original_image):
|
41 |
fig = plt.figure(figsize=(16, 9))
|
42 |
ax = fig.add_subplot(111, projection='3d')
|
43 |
x, y = np.meshgrid(range(depth_map.shape[1]), range(depth_map.shape[0]))
|
44 |
+
|
45 |
+
# Flip x and y to mirror the plot
|
46 |
+
x = np.flip(x, axis=1)
|
47 |
+
y = np.flip(y, axis=0)
|
48 |
+
|
49 |
+
# Rotate by 90 degrees
|
50 |
+
x, y = y, x
|
51 |
+
|
52 |
+
# Use original image colors
|
53 |
+
colors = original_image.reshape(-1, 3) / 255.0
|
54 |
+
|
55 |
+
ax.plot_surface(x, y, depth_map, facecolors=colors, shade=False)
|
56 |
+
ax.view_init(elev=70, azim=0) # Tilt by 20 degrees (90 - 20 = 70)
|
57 |
ax.set_zlim(0, 1)
|
58 |
+
plt.axis('off')
|
59 |
plt.close(fig)
|
60 |
|
61 |
fig.canvas.draw()
|
|
|
65 |
return img
|
66 |
|
67 |
@torch.inference_mode()
|
68 |
+
def process_frame(image):
|
69 |
if image is None:
|
70 |
return None
|
71 |
preprocessed = preprocess_image(image)
|
|
|
74 |
|
75 |
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
76 |
|
77 |
+
return plot_depth_map(depth_map, cv2.resize(image, (64, 36)))
|
78 |
|
79 |
interface = gr.Interface(
|
80 |
fn=process_frame,
|
81 |
+
inputs=gr.Image(sources="webcam", streaming=True),
|
82 |
outputs="image",
|
83 |
live=True
|
84 |
)
|