-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
233 lines (186 loc) · 6.67 KB
/
Copy pathmain.py
File metadata and controls
233 lines (186 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import argparse
import os
import sys
from typing import Optional
import cv2
import matplotlib.pyplot as plt
import numpy as np
import torch
from classifier import classify_severity
from features import extract_depth_features
from segmentation import get_pothole_mask
_DEPTH_MODEL: Optional[object] = None
def _resolve_depth_anything_root() -> str:
script_dir = os.path.dirname(os.path.abspath(__file__))
candidates = [
os.path.join(script_dir, "Depth-Anything-V2"),
os.path.abspath(os.path.join(script_dir, "..", "Depth-Anything-V2")),
]
for path in candidates:
if os.path.isdir(path):
return path
raise FileNotFoundError(
"Could not find Depth-Anything-V2. Expected one of: " + ", ".join(candidates)
)
def _load_depth_model():
global _DEPTH_MODEL
if _DEPTH_MODEL is not None:
return _DEPTH_MODEL
depth_anything_root = _resolve_depth_anything_root()
if depth_anything_root not in sys.path:
sys.path.append(depth_anything_root)
# pyrefly: ignore [missing-import]
from depth_anything_v2.dpt import DepthAnythingV2
model_path = os.path.join(
depth_anything_root, "checkpoints", "depth_anything_v2_vits.pth"
)
if not os.path.isfile(model_path):
raise FileNotFoundError(f"Depth checkpoint not found: {model_path}")
device = "cuda" if torch.cuda.is_available() else "cpu"
model = DepthAnythingV2(
encoder="vits", features=64, out_channels=[48, 96, 192, 384]
)
model.load_state_dict(torch.load(model_path, map_location=device))
model.to(device).eval()
_DEPTH_MODEL = model
return _DEPTH_MODEL
def get_depth_map(image: np.ndarray) -> np.ndarray:
"""Infer an HxW float32 depth map from a BGR image."""
if image is None:
raise ValueError("image cannot be None")
model = _load_depth_model()
depth_map = model.infer_image(image).astype(np.float32)
if depth_map.shape != image.shape[:2]:
depth_map = cv2.resize(
depth_map,
(image.shape[1], image.shape[0]),
interpolation=cv2.INTER_LINEAR,
)
return depth_map
def run_pipeline(
image_path: str, output_dir: Optional[str] = None, show: bool = True
) -> None:
mask, original_image = get_pothole_mask(image_path)
depth_map = get_depth_map(original_image)
if mask.shape != depth_map.shape:
depth_map = cv2.resize(
depth_map,
(mask.shape[1], mask.shape[0]),
interpolation=cv2.INTER_LINEAR,
)
features = extract_depth_features(mask, depth_map)
severity = classify_severity(features)
if features is None:
print("mean_depth: None")
print("max_depth: None")
print("area: 0")
else:
print(f"mean_depth: {features['mean_depth']:.6f}")
print(f"max_depth: {features['max_depth']:.6f}")
print(f"area: {features['area']}")
print(f"severity: {severity}")
text_overlay = original_image.copy()
cv2.putText(
text_overlay,
f"Severity: {severity}",
(20, 40),
cv2.FONT_HERSHEY_SIMPLEX,
1.0,
(0, 0, 255),
2,
cv2.LINE_AA,
)
overlay_mask = original_image.copy()
if np.any(mask):
red_layer = np.zeros_like(original_image)
red_layer[:, :, 2] = 255
blended = cv2.addWeighted(original_image, 0.4, red_layer, 0.6, 0)
overlay_mask[mask == 1] = blended[mask == 1]
ys, xs = np.where(mask == 1)
x1, y1, x2, y2 = int(xs.min()), int(ys.min()), int(xs.max()), int(ys.max())
cv2.rectangle(overlay_mask, (x1, y1), (x2, y2), (0, 0, 255), 2)
local_contrast = 0.0 if features is None else float(features.get("local_depth_contrast", 0.0))
depth_std = 0.0 if features is None else float(features.get("depth_std", 0.0))
label_lines = [
f"Class: {severity}",
f"Drop: {local_contrast:.3f}",
f"Roughness: {depth_std:.3f}",
]
line_h = 20
pad = 8
label_w = max(
cv2.getTextSize(t, cv2.FONT_HERSHEY_SIMPLEX, 0.55, 2)[0][0]
for t in label_lines
)
label_h = 2 * pad + line_h * len(label_lines)
box_w = label_w + 2 * pad
label_x = max(5, min(x1, overlay_mask.shape[1] - box_w - 5))
label_y = y1 - label_h - 8
if label_y < 5:
label_y = min(overlay_mask.shape[0] - label_h - 5, y2 + 8)
dark_bg = overlay_mask.copy()
cv2.rectangle(
dark_bg,
(label_x, label_y),
(label_x + box_w, label_y + label_h),
(0, 0, 0),
-1,
)
cv2.addWeighted(dark_bg, 0.55, overlay_mask, 0.45, 0, overlay_mask)
for idx, text in enumerate(label_lines):
y = label_y + pad + 14 + idx * line_h
cv2.putText(
overlay_mask,
text,
(label_x + pad, y),
cv2.FONT_HERSHEY_SIMPLEX,
0.55,
(255, 255, 255),
2,
cv2.LINE_AA,
)
depth_norm = cv2.normalize(depth_map, None, 0, 255, cv2.NORM_MINMAX).astype(
np.uint8
)
depth_heatmap = cv2.applyColorMap(depth_norm, cv2.COLORMAP_JET)
plt.figure(figsize=(15, 5))
plt.subplot(1, 3, 1)
plt.title("Original Image + Severity")
plt.imshow(cv2.cvtColor(text_overlay, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.subplot(1, 3, 2)
plt.title("Pothole BBox + Depth + Severity")
plt.imshow(cv2.cvtColor(overlay_mask, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.subplot(1, 3, 3)
plt.title("Depth Map Heatmap")
plt.imshow(cv2.cvtColor(depth_heatmap, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.tight_layout()
if output_dir:
os.makedirs(output_dir, exist_ok=True)
image_name = os.path.splitext(os.path.basename(image_path))[0]
output_path = os.path.join(output_dir, f"{image_name}_result.png")
plt.savefig(output_path, dpi=150, bbox_inches="tight")
print(f"saved_visualization: {output_path}")
if show:
plt.show()
else:
plt.close()
def main() -> None:
parser = argparse.ArgumentParser(description="Pothole severity pipeline")
parser.add_argument("image_path", help="Path to input image")
parser.add_argument(
"--output_dir",
default=None,
help="Optional directory to save visualization image",
)
parser.add_argument(
"--no_show",
action="store_true",
help="Disable interactive plot display",
)
args = parser.parse_args()
run_pipeline(args.image_path, output_dir=args.output_dir, show=not args.no_show)
if __name__ == "__main__":
main()