Skip to content

Commit 9f358fb

Browse files
committed
fix: imports fix and added rembg
1 parent 3d55cc8 commit 9f358fb

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

Dockerfile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ RUN git clone https://github.com/VAST-AI-Research/TripoSR.git /app/TripoSR
1111

1212
WORKDIR /app/TripoSR
1313

14-
# Upgrade pip
15-
RUN pip install --upgrade pip
14+
# Upgrade pip and setuptools (needed for torchmcubes build)
15+
RUN pip install --upgrade pip "setuptools>=49.6.0"
16+
17+
# Pin NumPy <2 to avoid incompatibility with PyTorch compiled against NumPy 1.x
18+
RUN pip install "numpy<2"
1619

1720
# Install TripoSR dependencies (much lighter than TRELLIS)
18-
RUN pip install pillow transformers trimesh rembg onnxruntime numpy einops omegaconf pytorch-lightning huggingface_hub PyMCubes
21+
RUN pip install pillow transformers trimesh rembg onnxruntime einops omegaconf pytorch-lightning huggingface_hub
22+
23+
# Install torchmcubes from source (TripoSR's marching cubes dependency)
24+
RUN pip install git+https://github.com/tatsy/torchmcubes.git
1925

2026
# Install RunPod SDK
2127
RUN pip install runpod requests

handler.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import torch
33
import base64
44
import requests
5+
import rembg
56
from io import BytesIO
67
from PIL import Image
78

@@ -40,23 +41,31 @@ def generate_3d(job):
4041
# 1. Fetch the 2D image from the URL
4142
response = requests.get(image_url)
4243
response.raise_for_status()
43-
image = Image.open(BytesIO(response.content)).convert("RGB")
44+
raw_image = Image.open(BytesIO(response.content))
4445

45-
print("Image downloaded. Running 3D synthesis...")
46+
# 2. CRITICAL: Remove the background so TripoSR doesn't generate a cube!
47+
print("Removing background...")
48+
transparent_image = rembg.remove(raw_image)
4649

47-
# 2. Run TripoSR inference
50+
# 3. Composite onto a clean white background (TripoSR's preferred format)
51+
image = Image.new("RGB", transparent_image.size, (255, 255, 255))
52+
image.paste(transparent_image, mask=transparent_image.split()[3]) # Use alpha channel as mask
53+
54+
print("Image pre-processed. Running 3D synthesis...")
55+
56+
# 4. Run TripoSR inference
4857
with torch.no_grad():
4958
scene_codes = model([image], device="cuda")
5059

51-
# 3. Extract the mesh and export as GLB
60+
# 5. Extract the mesh and export as GLB
5261
meshes = model.extract_mesh(scene_codes, resolution=256)
5362
mesh = meshes[0]
5463

55-
# 4. Save to a temporary GLB file
64+
# 6. Save to a temporary GLB file
5665
temp_path = "/tmp/output.glb"
5766
mesh.export(temp_path)
5867

59-
# 5. Encode the GLB to base64 to send back to Express
68+
# 7. Encode the GLB to base64 to send back to Express
6069
with open(temp_path, "rb") as f:
6170
obj_base64 = base64.b64encode(f.read()).decode('utf-8')
6271

0 commit comments

Comments
 (0)