Spaces:
Sleeping
Sleeping
Fix trimesh index out of bounds error: Resolve dimension mismatch between image and depth map - Resize image to match depth map dimensions exactly - Add bounds checking for array access - Improve face generation with validation - Add detailed dimension logging for debugging
Browse files- models/depth_processor.py +27 -8
models/depth_processor.py
CHANGED
|
@@ -324,9 +324,15 @@ class DepthProcessor:
|
|
| 324 |
try:
|
| 325 |
import trimesh
|
| 326 |
|
| 327 |
-
|
| 328 |
h, w = depth_map.shape
|
| 329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
vertices = []
|
| 331 |
faces = []
|
| 332 |
colors = []
|
|
@@ -343,6 +349,10 @@ class DepthProcessor:
|
|
| 343 |
|
| 344 |
for y in range(0, h, step):
|
| 345 |
for x in range(0, w, step):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
depth_val = depth_map[y, x]
|
| 347 |
Z = (1.0 - depth_val) * 0.3 # Reduced depth scale
|
| 348 |
|
|
@@ -356,15 +366,22 @@ class DepthProcessor:
|
|
| 356 |
vertex_map[(y, x)] = vertex_idx
|
| 357 |
vertex_idx += 1
|
| 358 |
|
| 359 |
-
# Add color
|
| 360 |
-
if
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
| 362 |
else:
|
| 363 |
-
colors.append([0.7, 0.7, 0.7])
|
| 364 |
|
| 365 |
-
# Generate faces from grid
|
| 366 |
for y in range(0, h - step, step):
|
| 367 |
for x in range(0, w - step, step):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
corners = [
|
| 369 |
(y, x), (y, x + step),
|
| 370 |
(y + step, x), (y + step, x + step)
|
|
@@ -372,8 +389,10 @@ class DepthProcessor:
|
|
| 372 |
|
| 373 |
if all(corner in vertex_map for corner in corners):
|
| 374 |
indices = [vertex_map[corner] for corner in corners]
|
| 375 |
-
faces
|
| 376 |
-
|
|
|
|
|
|
|
| 377 |
|
| 378 |
if not vertices:
|
| 379 |
raise ValueError("No valid vertices generated")
|
|
|
|
| 324 |
try:
|
| 325 |
import trimesh
|
| 326 |
|
| 327 |
+
# Ensure image and depth map have same dimensions
|
| 328 |
h, w = depth_map.shape
|
| 329 |
|
| 330 |
+
# Resize image to match depth map dimensions exactly
|
| 331 |
+
image_resized = image.resize((w, h), Image.Resampling.LANCZOS)
|
| 332 |
+
img_array = np.array(image_resized)
|
| 333 |
+
|
| 334 |
+
logger.info(f"📏 Image dimensions: {img_array.shape}, Depth map: {depth_map.shape}")
|
| 335 |
+
|
| 336 |
vertices = []
|
| 337 |
faces = []
|
| 338 |
colors = []
|
|
|
|
| 349 |
|
| 350 |
for y in range(0, h, step):
|
| 351 |
for x in range(0, w, step):
|
| 352 |
+
# Ensure we don't go out of bounds
|
| 353 |
+
if y >= h or x >= w:
|
| 354 |
+
continue
|
| 355 |
+
|
| 356 |
depth_val = depth_map[y, x]
|
| 357 |
Z = (1.0 - depth_val) * 0.3 # Reduced depth scale
|
| 358 |
|
|
|
|
| 366 |
vertex_map[(y, x)] = vertex_idx
|
| 367 |
vertex_idx += 1
|
| 368 |
|
| 369 |
+
# Add color - ensure coordinates are within bounds
|
| 370 |
+
if y < img_array.shape[0] and x < img_array.shape[1]:
|
| 371 |
+
if len(img_array.shape) == 3:
|
| 372 |
+
colors.append(img_array[y, x] / 255.0)
|
| 373 |
+
else:
|
| 374 |
+
colors.append([0.7, 0.7, 0.7])
|
| 375 |
else:
|
| 376 |
+
colors.append([0.7, 0.7, 0.7]) # Default gray color
|
| 377 |
|
| 378 |
+
# Generate faces from grid with bounds checking
|
| 379 |
for y in range(0, h - step, step):
|
| 380 |
for x in range(0, w - step, step):
|
| 381 |
+
# Ensure all corners are within bounds
|
| 382 |
+
if y + step >= h or x + step >= w:
|
| 383 |
+
continue
|
| 384 |
+
|
| 385 |
corners = [
|
| 386 |
(y, x), (y, x + step),
|
| 387 |
(y + step, x), (y + step, x + step)
|
|
|
|
| 389 |
|
| 390 |
if all(corner in vertex_map for corner in corners):
|
| 391 |
indices = [vertex_map[corner] for corner in corners]
|
| 392 |
+
# Only create faces if we have valid indices
|
| 393 |
+
if len(indices) == 4 and all(idx is not None for idx in indices):
|
| 394 |
+
faces.append([indices[0], indices[1], indices[2]])
|
| 395 |
+
faces.append([indices[1], indices[3], indices[2]])
|
| 396 |
|
| 397 |
if not vertices:
|
| 398 |
raise ValueError("No valid vertices generated")
|