Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -31,29 +31,32 @@ model = torch.quantization.quantize_dynamic(
|
|
31 |
|
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 |
|
37 |
def preprocess_image(image):
|
38 |
-
|
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 |
-
|
|
|
46 |
|
47 |
predicted_depth = model(input_tensor).predicted_depth
|
48 |
depth_map = predicted_depth.squeeze()
|
49 |
|
50 |
-
#
|
51 |
-
|
|
|
|
|
|
|
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 |
|
|
|
31 |
|
32 |
processor = DPTImageProcessor.from_pretrained("Intel/dpt-swinv2-tiny-256")
|
33 |
|
34 |
+
color_map = torch.from_numpy(cv2.applyColorMap(np.arange(256, dtype=np.uint8), cv2.COLORMAP_INFERNO)).to(device)
|
35 |
+
|
36 |
+
input_tensor = torch.zeros((1, 3, 128, 128), dtype=torch.float32, device=device)
|
37 |
|
38 |
def preprocess_image(image):
|
39 |
+
return cv2.resize(image, (128, 72), interpolation=cv2.INTER_AREA).transpose(2, 0, 1).astype(np.float32) / 255.0
|
|
|
40 |
|
41 |
@torch.inference_mode()
|
42 |
def process_frame(image):
|
43 |
if image is None:
|
44 |
return None
|
45 |
+
preprocessed = preprocess_image(image)
|
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 |
+
# Discretization on GPU
|
52 |
+
num_bins = 1000
|
53 |
+
depth_min, depth_max = depth_map.min(), depth_map.max()
|
54 |
+
bins = torch.linspace(depth_min, depth_max, num_bins, device=device)
|
55 |
+
depth_map = torch.bucketize(depth_map, bins)
|
56 |
|
57 |
depth_map = (depth_map - depth_map.min()) / (depth_map.max() - depth_map.min())
|
58 |
depth_map = (depth_map * 255).byte()
|
59 |
+
depth_map_colored = color_map[depth_map]
|
|
|
60 |
|
61 |
return cv2.cvtColor(depth_map_colored.cpu().numpy(), cv2.COLOR_BGR2RGB)
|
62 |
|