Download OVOW/third_party/Motion324/scripts/trellis2_gen.py from AIGeeksGroup/PhysicalWM: direct link, hf CLI and curl.
- Browser
- Download file 4.77 kB
-
https://huggingface.co/datasets/AIGeeksGroup/PhysicalWM/resolve/main/OVOW/third_party/Motion324/scripts/trellis2_gen.py
- Command line
-
hf download hf://datasets/AIGeeksGroup/PhysicalWM/OVOW/third_party/Motion324/scripts/trellis2_gen.py
-
curl -L -o trellis2_gen.py https://huggingface.co/datasets/AIGeeksGroup/PhysicalWM/resolve/main/OVOW/third_party/Motion324/scripts/trellis2_gen.py
4.77 kB
| #!/usr/bin/env python3 | |
| import argparse | |
| import os | |
| import sys | |
| from datetime import datetime | |
| os.environ.setdefault("OPENCV_IO_ENABLE_OPENEXR", "1") | |
| os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True") | |
| os.environ["HF_ENDPOINT"] = "https://huggingface.co" | |
| trellis2_root = os.environ.get( | |
| "TRELLIS2_ROOT", | |
| "/mnt/einsia/aws01-nvme/einsia-shared/homes/gaomingju/jiayin/OVOW/engine/oneposeviagen/TRELLIS2", | |
| ) | |
| if os.path.isdir(trellis2_root) and trellis2_root not in sys.path: | |
| sys.path.insert(0, trellis2_root) | |
| import numpy as np | |
| import torch | |
| from PIL import Image | |
| def _load_image_with_alpha(path: str) -> Image.Image: | |
| img = Image.open(path) | |
| if img.mode == "RGBA": | |
| arr = np.array(img).astype(np.float32) / 255.0 | |
| rgb = arr[..., :3] * arr[..., 3:4] | |
| return Image.fromarray((rgb * 255).astype(np.uint8), mode="RGB") | |
| return img.convert("RGB") | |
| def parse_args() -> argparse.Namespace: | |
| parser = argparse.ArgumentParser(description="Trellis2 GLB generator (single image).") | |
| parser.add_argument("--input", required=True, help="Input image path (RGB or RGBA).") | |
| parser.add_argument("--output_glb", required=True, help="Output GLB path.") | |
| parser.add_argument("--ckpt", required=True, help="Checkpoint path or repo id.") | |
| parser.add_argument("--seed", type=int, default=42, help="Random seed.") | |
| parser.add_argument("--pipeline_type", default="1024", help="Pipeline type (e.g., 1024).") | |
| parser.add_argument("--max_num_tokens", type=int, default=49152, help="Max tokens.") | |
| parser.add_argument("--simplify_target", type=int, default=1000000, help="Decimation target.") | |
| parser.add_argument("--texture_size", type=int, default=1024, help="Texture size.") | |
| parser.add_argument("--preprocess_image", action="store_true", help="Enable pipeline preprocessing.") | |
| parser.add_argument("--disable_rembg", action="store_true", help="Disable Trellis2 rembg model download.") | |
| return parser.parse_args() | |
| def log(msg: str) -> None: | |
| ts = datetime.now().strftime("%H:%M:%S") | |
| print(f"[{ts}] {msg}", flush=True) | |
| def main() -> None: | |
| args = parse_args() | |
| os.makedirs(os.path.dirname(args.output_glb) or ".", exist_ok=True) | |
| if args.seed >= 0: | |
| torch.manual_seed(args.seed) | |
| torch.cuda.manual_seed_all(args.seed) | |
| log("Importing Trellis2 and o_voxel") | |
| from trellis2.pipelines import Trellis2ImageTo3DPipeline | |
| from trellis2.pipelines import rembg as rembg_mod | |
| try: | |
| import o_voxel | |
| except Exception as exc: # noqa: BLE001 | |
| raise RuntimeError( | |
| "Failed to import o_voxel. Please build/install it in the Trellis2 env.\n" | |
| "Example:\n" | |
| " conda run -n opg pip install -e " | |
| "/mnt/einsia/aws01-nvme/einsia-shared/homes/gaomingju/jiayin/OVOW/engine/oneposeviagen/TRELLIS2/o-voxel" | |
| ) from exc | |
| log(f"Loading image: {args.input}") | |
| image = _load_image_with_alpha(args.input) | |
| disable_rembg = args.disable_rembg or os.getenv("TRELLIS2_DISABLE_REMBG", "0") == "1" | |
| if disable_rembg: | |
| class _NoRembg: | |
| def __init__(self, *_, **__): | |
| pass | |
| def to(self, *_): | |
| return self | |
| def cuda(self): | |
| return self | |
| def cpu(self): | |
| return self | |
| def __call__(self, image): | |
| return image | |
| rembg_mod.BiRefNet = _NoRembg | |
| log("RMBG disabled (using no-op rembg model)") | |
| log(f"Loading Trellis2 pipeline from: {args.ckpt}") | |
| pipeline = Trellis2ImageTo3DPipeline.from_pretrained(args.ckpt) | |
| pipeline.cuda() | |
| log("Running Trellis2 inference") | |
| run_kwargs = { | |
| "num_samples": 1, | |
| "seed": int(args.seed), | |
| "preprocess_image": bool(args.preprocess_image), | |
| "pipeline_type": str(args.pipeline_type), | |
| "max_num_tokens": int(args.max_num_tokens), | |
| } | |
| try: | |
| outputs = pipeline.run(image, **run_kwargs) | |
| except TypeError: | |
| outputs = pipeline.run(image) | |
| log("Converting to GLB") | |
| mesh = outputs[0] | |
| mesh.simplify(int(args.simplify_target)) | |
| glb_obj = o_voxel.postprocess.to_glb( | |
| vertices=mesh.vertices, | |
| faces=mesh.faces, | |
| attr_volume=mesh.attrs, | |
| coords=mesh.coords, | |
| attr_layout=mesh.layout, | |
| voxel_size=mesh.voxel_size, | |
| aabb=[[-0.5, -0.5, -0.5], [0.5, 0.5, 0.5]], | |
| decimation_target=int(args.simplify_target), | |
| texture_size=int(args.texture_size), | |
| remesh=True, | |
| remesh_band=1, | |
| remesh_project=0, | |
| verbose=False, | |
| ) | |
| glb_obj.export(args.output_glb, extension_webp=True) | |
| log(f"GLB saved: {args.output_glb}") | |
| if __name__ == "__main__": | |
| main() | |