Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -505,7 +505,33 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
505 |
faces.append([p1, p2, p3])
|
506 |
faces.append([p2, p4, p3])
|
507 |
|
|
|
|
|
|
|
|
|
|
|
508 |
faces = np.array(faces)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
509 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
510 |
|
511 |
# Apply vertex colors from image
|
@@ -524,6 +550,10 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
524 |
|
525 |
vertex_idx = i * resolution + j
|
526 |
|
|
|
|
|
|
|
|
|
527 |
# Handle RGBA images
|
528 |
if len(img_array.shape) == 3 and img_array.shape[2] == 4:
|
529 |
# Direct copy of RGBA values with bilinear interpolation
|
@@ -557,13 +587,31 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
557 |
|
558 |
mesh.visual.vertex_colors = vertex_colors
|
559 |
|
560 |
-
|
561 |
-
|
562 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
563 |
|
564 |
-
# Fix normals for better rendering
|
565 |
-
mesh.fix_normals()
|
566 |
return mesh
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
567 |
|
568 |
@app.route('/health', methods=['GET'])
|
569 |
def health_check():
|
|
|
505 |
faces.append([p1, p2, p3])
|
506 |
faces.append([p2, p4, p3])
|
507 |
|
508 |
+
# Check if we have valid faces before creating the mesh
|
509 |
+
if len(faces) == 0:
|
510 |
+
# Create a simple square mesh as fallback
|
511 |
+
faces = [[0, 1, 2], [1, 3, 2]]
|
512 |
+
|
513 |
faces = np.array(faces)
|
514 |
+
|
515 |
+
# Ensure we have at least one vertex and face
|
516 |
+
if len(vertices) == 0 or len(faces) == 0:
|
517 |
+
# Create a minimal mesh to avoid errors
|
518 |
+
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]])
|
519 |
+
faces = np.array([[0, 1, 2], [1, 3, 2]])
|
520 |
+
|
521 |
+
# Check for out-of-bounds indexes
|
522 |
+
max_vertex_idx = len(vertices) - 1
|
523 |
+
valid_faces = []
|
524 |
+
for face in faces:
|
525 |
+
if np.all(face <= max_vertex_idx):
|
526 |
+
valid_faces.append(face)
|
527 |
+
|
528 |
+
if len(valid_faces) == 0:
|
529 |
+
# Create a minimal mesh to avoid errors
|
530 |
+
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]])
|
531 |
+
faces = np.array([[0, 1, 2], [1, 3, 2]])
|
532 |
+
else:
|
533 |
+
faces = np.array(valid_faces)
|
534 |
+
|
535 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
536 |
|
537 |
# Apply vertex colors from image
|
|
|
550 |
|
551 |
vertex_idx = i * resolution + j
|
552 |
|
553 |
+
# Skip if vertex index is out of range
|
554 |
+
if vertex_idx >= len(vertices):
|
555 |
+
continue
|
556 |
+
|
557 |
# Handle RGBA images
|
558 |
if len(img_array.shape) == 3 and img_array.shape[2] == 4:
|
559 |
# Direct copy of RGBA values with bilinear interpolation
|
|
|
587 |
|
588 |
mesh.visual.vertex_colors = vertex_colors
|
589 |
|
590 |
+
try:
|
591 |
+
# Apply smoothing for non-high detail levels
|
592 |
+
if detail_level != 'high':
|
593 |
+
mesh = mesh.smoothed(method='laplacian', iterations=1)
|
594 |
+
|
595 |
+
# Try to fix normals but catch any errors
|
596 |
+
try:
|
597 |
+
mesh.fix_normals()
|
598 |
+
except Exception as e:
|
599 |
+
print(f"Warning: Could not fix normals: {str(e)}")
|
600 |
+
# Compute face normals manually if fix_normals fails
|
601 |
+
mesh.face_normals = trimesh.geometry.triangles_normals(
|
602 |
+
mesh.triangles
|
603 |
+
)
|
604 |
+
except Exception as e:
|
605 |
+
print(f"Warning: Error in mesh post-processing: {str(e)}")
|
606 |
|
|
|
|
|
607 |
return mesh
|
608 |
+
|
609 |
+
|
610 |
+
|
611 |
+
|
612 |
+
|
613 |
+
|
614 |
+
|
615 |
|
616 |
@app.route('/health', methods=['GET'])
|
617 |
def health_check():
|