Download scripts/render_stacked_channels.py from ZionHao/ho3d-zero123-processed: direct link, hf CLI and curl.
- Browser
- Download file 5.55 kB
-
https://huggingface.co/datasets/ZionHao/ho3d-zero123-processed/resolve/main/scripts/render_stacked_channels.py
- Command line
-
hf download hf://datasets/ZionHao/ho3d-zero123-processed/scripts/render_stacked_channels.py
-
curl -L -o render_stacked_channels.py https://huggingface.co/datasets/ZionHao/ho3d-zero123-processed/resolve/main/scripts/render_stacked_channels.py
5.55 kB
| #!/usr/bin/env python3 | |
| """ | |
| Render 6 views and stack them as channels (HxWx18) | |
| Output: stacked PNG image (H*6, W, 3) where 6 views are vertically stacked | |
| Usage: | |
| python render/render_stacked_channels.py \ | |
| --input_dir /path/to/meshes \ | |
| --output_dir /path/to/output \ | |
| --input_coord y_up \ | |
| --device cuda:0 | |
| """ | |
| import os | |
| import sys | |
| import argparse | |
| import numpy as np | |
| from PIL import Image | |
| from pathlib import Path | |
| from tqdm import tqdm | |
| # Add project root to path | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from render.models.instantmesh_renderer_v4 import InstantMeshRendererV4 | |
| class StackedChannelRenderer: | |
| """Render 6 views and stack as image (vertical stack or channel stack)""" | |
| def __init__(self, device='cuda:0', image_size=256, input_coord='z_up'): | |
| self.image_size = image_size | |
| self.renderer = InstantMeshRendererV4( | |
| device=device, | |
| image_size=image_size, | |
| input_coord=input_coord # Support y_up/z_up | |
| ) | |
| print(f"Stacked Renderer: {image_size}x{image_size}, coord={input_coord}") | |
| def render_6views(self, obj_path): | |
| """Render 6 views, return list of (H, W, 3) arrays""" | |
| mesh = self.renderer.load_mesh(obj_path, verbose=False) | |
| return self.renderer.render_6views(mesh) | |
| def render_stacked_image(self, obj_path): | |
| """ | |
| Render and stack 6 views vertically as single image. | |
| Output: (H*6, W, 3) for vertical stack | |
| """ | |
| views = self.render_6views(obj_path) | |
| # Vertical stack: (256, 256, 3) x 6 -> (1536, 256, 3) | |
| stacked = np.concatenate(views, axis=0) | |
| return (stacked * 255).clip(0, 255).astype(np.uint8) | |
| def render_stacked_channels(self, obj_path): | |
| """ | |
| Render and stack 6 views as channels. | |
| Output: (H, W, 18) float32 | |
| """ | |
| views = self.render_6views(obj_path) | |
| stacked = np.concatenate(views, axis=-1) | |
| return stacked.astype(np.float32) | |
| def save_stacked_image(self, obj_path, output_path): | |
| """Render and save as stacked PNG image""" | |
| stacked = self.render_stacked_image(obj_path) | |
| Image.fromarray(stacked).save(output_path) | |
| return stacked | |
| def save_stacked_npy(self, obj_path, output_path, save_preview=True): | |
| """Render and save as .npy (18 channels)""" | |
| stacked = self.render_stacked_channels(obj_path) | |
| np.save(output_path, stacked) | |
| if save_preview: | |
| preview_path = output_path.replace('.npy', '_preview.png') | |
| views = [stacked[..., i*3:(i+1)*3] for i in range(6)] | |
| combined = self.renderer.create_combined_image(views) | |
| combined_uint8 = (combined * 255).clip(0, 255).astype(np.uint8) | |
| Image.fromarray(combined_uint8).save(preview_path) | |
| return stacked | |
| def find_obj_files(root_dir): | |
| """Find all .obj files""" | |
| root = Path(root_dir) | |
| obj_files = list(root.rglob('normalized_*.obj')) | |
| if not obj_files: | |
| obj_files = list(root.rglob('*.obj')) | |
| return sorted(obj_files) | |
| def main(): | |
| parser = argparse.ArgumentParser(description='Render 6 views stacked') | |
| parser.add_argument('--input_dir', type=str, required=True) | |
| parser.add_argument('--output_dir', type=str, required=True) | |
| parser.add_argument('--input_coord', type=str, default='z_up', | |
| choices=['z_up', 'y_up'], help='Input mesh coordinate system') | |
| parser.add_argument('--device', type=str, default='cuda:0') | |
| parser.add_argument('--image_size', type=int, default=256) | |
| parser.add_argument('--format', type=str, default='png', | |
| choices=['png', 'npy'], help='Output format: png (stacked image) or npy (18 channels)') | |
| parser.add_argument('--batch_size', type=int, default=8, help='Batch size for GPU rendering (default: 8)') | |
| parser.add_argument('--num_workers', type=int, default=4, help='Number of workers for data loading (default: 4)') | |
| parser.add_argument('--max_samples', type=int, default=None) | |
| args = parser.parse_args() | |
| os.makedirs(args.output_dir, exist_ok=True) | |
| obj_files = find_obj_files(args.input_dir) | |
| print(f"Found {len(obj_files)} .obj files") | |
| if args.max_samples: | |
| obj_files = obj_files[:args.max_samples] | |
| renderer = StackedChannelRenderer( | |
| device=args.device, | |
| image_size=args.image_size, | |
| input_coord=args.input_coord | |
| ) | |
| ext = '.png' if args.format == 'png' else '.npy' | |
| success, failed = 0, [] | |
| for obj_path in tqdm(obj_files, desc="Rendering"): | |
| try: | |
| rel_path = obj_path.relative_to(args.input_dir) | |
| seq = rel_path.parts[0] if len(rel_path.parts) > 1 else "default" | |
| fname = rel_path.name | |
| output_path = Path(args.output_dir) / seq / Path(fname).with_suffix(ext) | |
| output_path.parent.mkdir(parents=True, exist_ok=True) | |
| if output_path.exists(): | |
| success += 1 | |
| continue | |
| if args.format == 'png': | |
| renderer.save_stacked_image(str(obj_path), str(output_path)) | |
| else: | |
| renderer.save_stacked_npy(str(obj_path), str(output_path)) | |
| success += 1 | |
| except Exception as e: | |
| failed.append((str(obj_path), str(e))) | |
| print(f"\nDone! Success: {success}, Failed: {len(failed)}") | |
| if failed: | |
| for p, e in failed[:5]: | |
| print(f" {p}: {e}") | |
| if __name__ == '__main__': | |
| main() | |