Update app.py
Browse files
app.py
CHANGED
@@ -32,30 +32,30 @@ model = torch.quantization.quantize_dynamic(
|
|
32 |
processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
33 |
|
34 |
color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)
|
35 |
-
|
36 |
-
input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float32, device=device)
|
37 |
|
38 |
def preprocess_image(image):
|
39 |
-
|
|
|
40 |
|
41 |
@torch.inference_mode()
|
42 |
def process_frame(image):
|
43 |
if image is None:
|
44 |
return None
|
45 |
-
|
46 |
-
input_tensor = torch.from_numpy(preprocessed).unsqueeze(0).to(device)
|
47 |
|
48 |
predicted_depth = model(input_tensor).predicted_depth
|
49 |
-
depth_map = predicted_depth.squeeze()
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
55 |
-
depth_map = (depth_map * 255).
|
56 |
-
|
|
|
57 |
|
58 |
-
return cv2.cvtColor(depth_map_colored, cv2.COLOR_BGR2RGB)
|
59 |
|
60 |
interface = gr.Interface(
|
61 |
fn=process_frame,
|
|
|
32 |
processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
33 |
|
34 |
color_map = cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)
|
35 |
+
color_map_gpu = torch.from_numpy(color_map).to(device)
|
|
|
36 |
|
37 |
def preprocess_image(image):
|
38 |
+
resized = cv2.resize(image, (128, 72), interpolation=cv2.INTER_AREA)
|
39 |
+
return torch.from_numpy(resized.transpose(2, 0, 1)).float().div(255).unsqueeze(0).to(device)
|
40 |
|
41 |
@torch.inference_mode()
|
42 |
def process_frame(image):
|
43 |
if image is None:
|
44 |
return None
|
45 |
+
input_tensor = preprocess_image(image)
|
|
|
46 |
|
47 |
predicted_depth = model(input_tensor).predicted_depth
|
48 |
+
depth_map = predicted_depth.squeeze()
|
49 |
+
|
50 |
+
# Rounding instead of discretization
|
51 |
+
depth_map = torch.round(depth_map * 100) / 100
|
52 |
+
|
53 |
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
54 |
+
depth_map = (depth_map * 255).byte()
|
55 |
+
|
56 |
+
depth_map_colored = color_map_gpu[depth_map]
|
57 |
|
58 |
+
return cv2.cvtColor(depth_map_colored.cpu().numpy(), cv2.COLOR_BGR2RGB)
|
59 |
|
60 |
interface = gr.Interface(
|
61 |
fn=process_frame,
|