-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnerf_generator.py
More file actions
executable file
·215 lines (177 loc) · 7.38 KB
/
nerf_generator.py
File metadata and controls
executable file
·215 lines (177 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
"""
NeRF (Neural Radiance Fields) Integration Module
Reconstructs 3D scenes from multiple images with camera poses
"""
import os
import sys
import argparse
from pathlib import Path
try:
import numpy as np
from PIL import Image
import imageio
# Add NeRF to path
nerf_path = os.path.join(os.path.dirname(__file__), 'nerf_repo')
if os.path.exists(nerf_path):
sys.path.insert(0, nerf_path)
except ImportError as e:
print(f"Error importing required libraries: {e}", file=sys.stderr)
print("Please install requirements: pip install -r requirements.txt", file=sys.stderr)
sys.exit(1)
def generate_nerf(
images_dir: str,
output_dir: str = "output",
dataset_type: str = "llff",
config_file: str = None
) -> dict:
"""
Generate 3D model from multiple images using NeRF
Args:
images_dir: Directory containing input images
output_dir: Directory to save NeRF output and extracted mesh
dataset_type: Type of dataset ('llff', 'blender', 'deepvoxels')
config_file: Path to NeRF config file (optional)
Returns:
Dictionary with paths to generated outputs:
{
'mesh': path,
'video': path,
'success': bool
}
"""
try:
# Check if NeRF repository exists
nerf_repo_path = os.path.join(os.path.dirname(__file__), 'nerf_repo')
if not os.path.exists(nerf_repo_path):
return {
'success': False,
'error': f"NeRF repository not found. Please ensure nerf_repo/ directory exists."
}
# Check for TensorFlow
try:
import tensorflow as tf
print(f"TensorFlow version: {tf.__version__}", file=sys.stderr)
sys.stderr.flush()
except ImportError:
return {
'success': False,
'error': "TensorFlow not found. NeRF requires TensorFlow 1.15. Note: TensorFlow 1.15 may not work on Apple Silicon. Consider using a modern NeRF implementation."
}
# Check if images directory exists and has images
if not os.path.exists(images_dir):
return {
'success': False,
'error': f"Images directory not found: {images_dir}"
}
image_files = [f for f in os.listdir(images_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
if len(image_files) < 3:
return {
'success': False,
'error': f"NeRF requires at least 3 images. Found {len(image_files)} images in {images_dir}"
}
print(f"Found {len(image_files)} images in {images_dir}", file=sys.stderr)
sys.stderr.flush()
# Create output directory
os.makedirs(output_dir, exist_ok=True)
# Check if we have camera poses
# NeRF requires camera poses - either from COLMAP/LLFF or provided
poses_file = os.path.join(images_dir, 'poses_bounds.npy')
if not os.path.exists(poses_file):
print("⚠️ Camera poses not found. NeRF requires camera poses.", file=sys.stderr)
print(" You can generate poses using COLMAP or LLFF tools.", file=sys.stderr)
print(" For now, using simplified mode (may not work correctly).", file=sys.stderr)
sys.stderr.flush()
# Create config file if not provided
if config_file is None:
config_file = os.path.join(output_dir, 'nerf_config.txt')
create_nerf_config(config_file, images_dir, output_dir, dataset_type)
print(f"Starting NeRF training...", file=sys.stderr)
print(f" This may take several hours depending on your hardware.", file=sys.stderr)
print(f" Note: TensorFlow 1.15 may not work on Apple Silicon.", file=sys.stderr)
sys.stderr.flush()
# Note: Full NeRF training requires TensorFlow 1.15 and CUDA
# This is a simplified wrapper - full implementation would call run_nerf.py
# For now, we'll provide a structure that can be enhanced
mesh_path = os.path.join(output_dir, "nerf_mesh.ply")
video_path = os.path.join(output_dir, "nerf_video.mp4")
# Placeholder - full implementation would:
# 1. Run NeRF training: python run_nerf.py --config config_file
# 2. Extract mesh from trained NeRF
# 3. Generate render video
print("⚠️ NeRF training is not fully implemented yet.", file=sys.stderr)
print(" Full NeRF integration requires:", file=sys.stderr)
print(" - TensorFlow 1.15 (may not work on Apple Silicon)", file=sys.stderr)
print(" - CUDA for GPU acceleration", file=sys.stderr)
print(" - Camera poses (COLMAP/LLFF)", file=sys.stderr)
print(" - Several hours of training time", file=sys.stderr)
sys.stderr.flush()
return {
'success': False,
'error': "NeRF training not fully implemented. Requires TensorFlow 1.15 and CUDA. Consider using a modern NeRF implementation like nerfstudio for Apple Silicon compatibility."
}
except Exception as e:
import traceback
error_msg = f"Error generating NeRF: {str(e)}"
print(error_msg, file=sys.stderr)
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
return {
'success': False,
'error': error_msg
}
def create_nerf_config(config_path: str, images_dir: str, output_dir: str, dataset_type: str):
"""Create a NeRF config file"""
config_content = f"""# NeRF Configuration
# Generated automatically for Prometheus
# Dataset settings
--datadir={images_dir}
--dataset_type={dataset_type}
--expname=prometheus_nerf
--basedir={output_dir}/logs
# Training settings
--N_iters=200000
--N_samples=64
--N_rand=1024
--lrate=5e-4
--lrate_decay=250
# Network settings
--netdepth=8
--netwidth=256
--netdepth_fine=8
--netwidth_fine=256
# Rendering settings
--chunk=1024*32
--no_batching
--no_reload
"""
with open(config_path, 'w') as f:
f.write(config_content)
print(f"Created NeRF config: {config_path}", file=sys.stderr)
sys.stderr.flush()
def main():
parser = argparse.ArgumentParser(description='Generate 3D model from images using NeRF')
parser.add_argument('--images', type=str, required=True, help='Directory containing input images')
parser.add_argument('--output', type=str, default='output', help='Output directory')
parser.add_argument('--dataset-type', type=str, default='llff', choices=['llff', 'blender', 'deepvoxels'],
help='Type of dataset')
parser.add_argument('--config', type=str, default=None, help='Path to NeRF config file')
args = parser.parse_args()
result = generate_nerf(
images_dir=args.images,
output_dir=args.output,
dataset_type=args.dataset_type,
config_file=args.config
)
if result['success']:
if 'mesh' in result:
print(f"MESH: {result['mesh']}", file=sys.stdout)
if 'video' in result:
print(f"VIDEO: {result['video']}", file=sys.stdout)
sys.stdout.flush()
else:
print(f"ERROR: {result.get('error', 'Unknown error')}", file=sys.stderr)
sys.stderr.flush()
sys.exit(1)
if __name__ == '__main__':
main()