dev-bjoern commited on
Commit
b8dd3b4
·
1 Parent(s): 54f5ad7

Replace bpy with trimesh for GLB export (Python 3.10 compatible)

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +13 -32
  3. requirements.txt +1 -1
README.md CHANGED
@@ -9,7 +9,7 @@ python_version: "3.11"
9
  app_file: app.py
10
  pinned: false
11
  license: other
12
- hardware: a10g-small
13
  tags:
14
  - mcp-server-track
15
  - building-mcp-track-consumer
 
9
  app_file: app.py
10
  pinned: false
11
  license: other
12
+ hardware: zero-a10g
13
  tags:
14
  - mcp-server-track
15
  - building-mcp-track-consumer
app.py CHANGED
@@ -89,53 +89,34 @@ def reconstruct_body(image: np.ndarray) -> tuple:
89
 
90
  try:
91
  import torch
92
- import bpy
93
  estimator, faces = load_model()
94
-
95
  # Process image
96
  if isinstance(image, Image.Image):
97
  image = np.array(image)
98
-
99
  # BGR for OpenCV
100
  import cv2
101
  img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
102
-
103
  outputs = estimator.process_one_image(img_bgr, bbox_thr=0.5)
104
-
105
  if not outputs:
106
  return None, "⚠️ No humans detected"
107
-
108
- # Export first person as GLB via Blender
109
  person = outputs[0]
110
  vertices = person["pred_vertices"]
111
-
112
- # Reset Blender scene
113
- bpy.ops.wm.read_factory_settings(use_empty=True)
114
-
115
- # Create mesh
116
- mesh = bpy.data.meshes.new("body_mesh")
117
- mesh.from_pydata(vertices.tolist(), [], faces.tolist())
118
- mesh.update()
119
-
120
- # Create object
121
- obj = bpy.data.objects.new("body", mesh)
122
- bpy.context.collection.objects.link(obj)
123
- bpy.context.view_layer.objects.active = obj
124
- obj.select_set(True)
125
-
126
- # Smooth shading
127
- for poly in mesh.polygons:
128
- poly.use_smooth = True
129
-
130
  # Save GLB
131
  output_dir = tempfile.mkdtemp()
132
  glb_path = f"{output_dir}/body_{uuid.uuid4().hex[:8]}.glb"
133
- bpy.ops.export_scene.gltf(
134
- filepath=glb_path,
135
- export_format='GLB',
136
- use_selection=True
137
- )
138
-
139
  return glb_path, f"✓ Reconstructed {len(outputs)} person(s)"
140
 
141
  except Exception as e:
 
89
 
90
  try:
91
  import torch
92
+ import trimesh
93
  estimator, faces = load_model()
94
+
95
  # Process image
96
  if isinstance(image, Image.Image):
97
  image = np.array(image)
98
+
99
  # BGR for OpenCV
100
  import cv2
101
  img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
102
+
103
  outputs = estimator.process_one_image(img_bgr, bbox_thr=0.5)
104
+
105
  if not outputs:
106
  return None, "⚠️ No humans detected"
107
+
108
+ # Export first person as GLB via trimesh
109
  person = outputs[0]
110
  vertices = person["pred_vertices"]
111
+
112
+ # Create trimesh mesh
113
+ mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
114
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  # Save GLB
116
  output_dir = tempfile.mkdtemp()
117
  glb_path = f"{output_dir}/body_{uuid.uuid4().hex[:8]}.glb"
118
+ mesh.export(glb_path, file_type='glb')
119
+
 
 
 
 
120
  return glb_path, f"✓ Reconstructed {len(outputs)} person(s)"
121
 
122
  except Exception as e:
requirements.txt CHANGED
@@ -3,7 +3,7 @@ torchvision>=0.17.0
3
  gradio>=6.0.2
4
  huggingface_hub>=0.26.0
5
  spaces>=0.30.0
6
- bpy>=5.0.0
7
  numpy>=1.26.0
8
  opencv-python>=4.8.0
9
  Pillow>=10.0.0
 
3
  gradio>=6.0.2
4
  huggingface_hub>=0.26.0
5
  spaces>=0.30.0
6
+ trimesh>=4.0.0
7
  numpy>=1.26.0
8
  opencv-python>=4.8.0
9
  Pillow>=10.0.0