Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,11 +14,6 @@ import traceback
|
|
14 |
from huggingface_hub import snapshot_download
|
15 |
from flask_cors import CORS
|
16 |
import numpy as np
|
17 |
-
import trimesh
|
18 |
-
from transformers import pipeline
|
19 |
-
from scipy.ndimage import gaussian_filter, uniform_filter, median_filter
|
20 |
-
from scipy import interpolate
|
21 |
-
import cv2
|
22 |
|
23 |
app = Flask(__name__)
|
24 |
CORS(app) # Enable CORS for all routes
|
@@ -38,6 +33,7 @@ os.makedirs(CACHE_DIR, exist_ok=True)
|
|
38 |
os.environ['HF_HOME'] = CACHE_DIR
|
39 |
os.environ['TRANSFORMERS_CACHE'] = os.path.join(CACHE_DIR, 'transformers')
|
40 |
os.environ['HF_DATASETS_CACHE'] = os.path.join(CACHE_DIR, 'datasets')
|
|
|
41 |
|
42 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
43 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max
|
@@ -46,7 +42,8 @@ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max
|
|
46 |
processing_jobs = {}
|
47 |
|
48 |
# Global model variables
|
49 |
-
|
|
|
50 |
model_loaded = False
|
51 |
model_loading = False
|
52 |
|
@@ -58,6 +55,42 @@ MAX_DIMENSION = 512 # Max image dimension to process
|
|
58 |
class TimeoutError(Exception):
|
59 |
pass
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
# Thread-safe timeout implementation
|
62 |
def process_with_timeout(function, args, timeout):
|
63 |
result = [None]
|
@@ -91,335 +124,139 @@ def process_with_timeout(function, args, timeout):
|
|
91 |
def allowed_file(filename):
|
92 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
93 |
|
94 |
-
#
|
95 |
def preprocess_image(image_path):
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
#
|
102 |
-
if img.width > img.height:
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
img_array = np.array(img)
|
114 |
-
|
115 |
-
# Optional: Apply adaptive histogram equalization for better contrast
|
116 |
-
# This helps the depth model detect more details
|
117 |
-
if len(img_array.shape) == 3 and img_array.shape[2] == 3:
|
118 |
-
# Convert to LAB color space
|
119 |
-
lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2LAB)
|
120 |
-
l, a, b = cv2.split(lab)
|
121 |
-
|
122 |
-
# Apply CLAHE to L channel
|
123 |
-
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
124 |
-
cl = clahe.apply(l)
|
125 |
|
126 |
-
#
|
127 |
-
|
128 |
|
129 |
-
#
|
130 |
-
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
136 |
|
|
|
137 |
def load_model():
|
138 |
-
global
|
139 |
|
140 |
if model_loaded:
|
141 |
-
return
|
142 |
|
143 |
if model_loading:
|
144 |
# Wait for model to load if it's already in progress
|
145 |
while model_loading and not model_loaded:
|
146 |
time.sleep(0.5)
|
147 |
-
return
|
148 |
|
149 |
try:
|
150 |
model_loading = True
|
151 |
-
print("Starting model loading...")
|
152 |
-
|
153 |
-
# Using DPT-Large which provides better detail than DPT-Hybrid
|
154 |
-
# Alternatively, consider "vinvino02/glpn-nyu" for different detail characteristics
|
155 |
-
model_name = "Intel/dpt-large"
|
156 |
-
|
157 |
-
# Download model with retry mechanism
|
158 |
-
max_retries = 3
|
159 |
-
retry_delay = 5
|
160 |
-
|
161 |
-
for attempt in range(max_retries):
|
162 |
-
try:
|
163 |
-
snapshot_download(
|
164 |
-
repo_id=model_name,
|
165 |
-
cache_dir=CACHE_DIR,
|
166 |
-
resume_download=True,
|
167 |
-
)
|
168 |
-
break
|
169 |
-
except Exception as e:
|
170 |
-
if attempt < max_retries - 1:
|
171 |
-
print(f"Download attempt {attempt+1} failed: {str(e)}. Retrying in {retry_delay} seconds...")
|
172 |
-
time.sleep(retry_delay)
|
173 |
-
retry_delay *= 2
|
174 |
-
else:
|
175 |
-
raise
|
176 |
-
|
177 |
-
# Initialize model with appropriate precision
|
178 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
179 |
|
180 |
-
#
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
except Exception as e:
|
197 |
-
print(f"Error loading model: {str(e)}")
|
198 |
print(traceback.format_exc())
|
199 |
raise
|
200 |
finally:
|
201 |
model_loading = False
|
202 |
|
203 |
-
#
|
204 |
-
def
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
enhanced_depth = depth_map.copy().astype(np.float32)
|
216 |
-
|
217 |
-
# Remove outliers using percentile clipping (more stable than min/max)
|
218 |
-
p_low, p_high = np.percentile(enhanced_depth, [1, 99])
|
219 |
-
enhanced_depth = np.clip(enhanced_depth, p_low, p_high)
|
220 |
-
|
221 |
-
# Normalize to 0-1 range for processing
|
222 |
-
enhanced_depth = (enhanced_depth - p_low) / (p_high - p_low) if p_high > p_low else enhanced_depth
|
223 |
-
|
224 |
-
# Apply different enhancement methods based on detail level
|
225 |
-
if detail_level == 'high':
|
226 |
-
# Apply unsharp masking for edge enhancement - simulating Hunyuan's detail technique
|
227 |
-
# First apply gaussian blur
|
228 |
-
blurred = gaussian_filter(enhanced_depth, sigma=1.5)
|
229 |
-
# Create the unsharp mask
|
230 |
-
mask = enhanced_depth - blurred
|
231 |
-
# Apply the mask with strength factor
|
232 |
-
enhanced_depth = enhanced_depth + 1.5 * mask
|
233 |
-
|
234 |
-
# Apply bilateral filter to preserve edges while smoothing noise
|
235 |
-
# Simulate using gaussian combinations
|
236 |
-
smooth1 = gaussian_filter(enhanced_depth, sigma=0.5)
|
237 |
-
smooth2 = gaussian_filter(enhanced_depth, sigma=2.0)
|
238 |
-
edge_mask = enhanced_depth - smooth2
|
239 |
-
enhanced_depth = smooth1 + 1.2 * edge_mask
|
240 |
-
|
241 |
-
elif detail_level == 'medium':
|
242 |
-
# Less aggressive but still effective enhancement
|
243 |
-
# Apply mild unsharp masking
|
244 |
-
blurred = gaussian_filter(enhanced_depth, sigma=1.0)
|
245 |
-
mask = enhanced_depth - blurred
|
246 |
-
enhanced_depth = enhanced_depth + 0.8 * mask
|
247 |
-
|
248 |
-
# Apply mild smoothing to reduce noise but preserve edges
|
249 |
-
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.5)
|
250 |
-
|
251 |
-
else: # low
|
252 |
-
# Just apply noise reduction without too much detail enhancement
|
253 |
-
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.7)
|
254 |
-
|
255 |
-
# Normalize again after processing
|
256 |
-
enhanced_depth = np.clip(enhanced_depth, 0, 1)
|
257 |
-
|
258 |
-
return enhanced_depth
|
259 |
-
|
260 |
-
# Convert depth map to 3D mesh with significantly enhanced detail
|
261 |
-
def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
262 |
-
"""Convert depth map to 3D mesh with highly improved detail preservation"""
|
263 |
-
# First, enhance the depth map for better details
|
264 |
-
enhanced_depth = enhance_depth_map(depth_map, detail_level)
|
265 |
-
|
266 |
-
# Get dimensions of depth map
|
267 |
-
h, w = enhanced_depth.shape
|
268 |
-
|
269 |
-
# Create a higher resolution grid for better detail
|
270 |
-
x = np.linspace(0, w-1, resolution)
|
271 |
-
y = np.linspace(0, h-1, resolution)
|
272 |
-
x_grid, y_grid = np.meshgrid(x, y)
|
273 |
-
|
274 |
-
# Use bicubic interpolation for smoother surface with better details
|
275 |
-
# Create interpolation function
|
276 |
-
interp_func = interpolate.RectBivariateSpline(
|
277 |
-
np.arange(h), np.arange(w), enhanced_depth, kx=3, ky=3
|
278 |
-
)
|
279 |
-
|
280 |
-
# Sample depth at grid points with the interpolation function
|
281 |
-
z_values = interp_func(y, x, grid=True)
|
282 |
-
|
283 |
-
# Apply a post-processing step to enhance small details even further
|
284 |
-
if detail_level == 'high':
|
285 |
-
# Calculate local gradients to detect edges
|
286 |
-
dx = np.gradient(z_values, axis=1)
|
287 |
-
dy = np.gradient(z_values, axis=0)
|
288 |
-
|
289 |
-
# Enhance edges by increasing depth differences at high gradient areas
|
290 |
-
gradient_magnitude = np.sqrt(dx**2 + dy**2)
|
291 |
-
edge_mask = np.clip(gradient_magnitude * 5, 0, 0.2) # Scale and limit effect
|
292 |
-
|
293 |
-
# Apply edge enhancement
|
294 |
-
z_values = z_values + edge_mask * (z_values - gaussian_filter(z_values, sigma=1.0))
|
295 |
-
|
296 |
-
# Normalize z-values with advanced scaling for better depth impression
|
297 |
-
z_min, z_max = np.percentile(z_values, [2, 98]) # Remove outliers
|
298 |
-
z_values = (z_values - z_min) / (z_max - z_min) if z_max > z_min else z_values
|
299 |
-
|
300 |
-
# Apply depth scaling appropriate to the detail level
|
301 |
-
if detail_level == 'high':
|
302 |
-
z_scaling = 2.5 # More pronounced depth variations
|
303 |
-
elif detail_level == 'medium':
|
304 |
-
z_scaling = 2.0 # Standard depth
|
305 |
-
else:
|
306 |
-
z_scaling = 1.5 # More subtle depth variations
|
307 |
-
|
308 |
-
z_values = z_values * z_scaling
|
309 |
-
|
310 |
-
# Normalize x and y coordinates
|
311 |
-
x_grid = (x_grid / w - 0.5) * 2.0 # Map to -1 to 1
|
312 |
-
y_grid = (y_grid / h - 0.5) * 2.0 # Map to -1 to 1
|
313 |
-
|
314 |
-
# Create vertices
|
315 |
-
vertices = np.vstack([x_grid.flatten(), -y_grid.flatten(), -z_values.flatten()]).T
|
316 |
-
|
317 |
-
# Create faces (triangles) with optimized winding for better normals
|
318 |
-
faces = []
|
319 |
-
for i in range(resolution-1):
|
320 |
-
for j in range(resolution-1):
|
321 |
-
p1 = i * resolution + j
|
322 |
-
p2 = i * resolution + (j + 1)
|
323 |
-
p3 = (i + 1) * resolution + j
|
324 |
-
p4 = (i + 1) * resolution + (j + 1)
|
325 |
-
|
326 |
-
# Calculate normals to ensure consistent orientation
|
327 |
-
v1 = vertices[p1]
|
328 |
-
v2 = vertices[p2]
|
329 |
-
v3 = vertices[p3]
|
330 |
-
v4 = vertices[p4]
|
331 |
-
|
332 |
-
# Calculate normals for both possible triangulations
|
333 |
-
# and choose the one that's more consistent
|
334 |
-
norm1 = np.cross(v2-v1, v4-v1)
|
335 |
-
norm2 = np.cross(v4-v3, v1-v3)
|
336 |
-
|
337 |
-
if np.dot(norm1, norm2) >= 0:
|
338 |
-
# Standard triangulation
|
339 |
-
faces.append([p1, p2, p4])
|
340 |
-
faces.append([p1, p4, p3])
|
341 |
-
else:
|
342 |
-
# Alternative triangulation for smoother surface
|
343 |
-
faces.append([p1, p2, p3])
|
344 |
-
faces.append([p2, p4, p3])
|
345 |
-
|
346 |
-
faces = np.array(faces)
|
347 |
-
|
348 |
-
# Create mesh
|
349 |
-
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
350 |
-
|
351 |
-
# Apply advanced texturing if image is provided
|
352 |
-
if image:
|
353 |
-
# Convert to numpy array if needed
|
354 |
-
if isinstance(image, Image.Image):
|
355 |
-
img_array = np.array(image)
|
356 |
-
else:
|
357 |
-
img_array = image
|
358 |
-
|
359 |
-
# Create vertex colors with improved sampling
|
360 |
-
if resolution <= img_array.shape[0] and resolution <= img_array.shape[1]:
|
361 |
-
# Create vertex colors by sampling the image with bilinear interpolation
|
362 |
-
vertex_colors = np.zeros((vertices.shape[0], 4), dtype=np.uint8)
|
363 |
-
|
364 |
-
# Get normalized coordinates for sampling
|
365 |
-
for i in range(resolution):
|
366 |
-
for j in range(resolution):
|
367 |
-
# Calculate exact image coordinates with proper scaling
|
368 |
-
img_x = j * (img_array.shape[1] - 1) / (resolution - 1)
|
369 |
-
img_y = i * (img_array.shape[0] - 1) / (resolution - 1)
|
370 |
-
|
371 |
-
# Bilinear interpolation for smooth color transitions
|
372 |
-
x0, y0 = int(img_x), int(img_y)
|
373 |
-
x1, y1 = min(x0 + 1, img_array.shape[1] - 1), min(y0 + 1, img_array.shape[0] - 1)
|
374 |
-
|
375 |
-
# Calculate interpolation weights
|
376 |
-
wx = img_x - x0
|
377 |
-
wy = img_y - y0
|
378 |
-
|
379 |
-
vertex_idx = i * resolution + j
|
380 |
-
|
381 |
-
if len(img_array.shape) == 3 and img_array.shape[2] == 3: # RGB
|
382 |
-
# Perform bilinear interpolation for each color channel
|
383 |
-
r = int((1-wx)*(1-wy)*img_array[y0, x0, 0] + wx*(1-wy)*img_array[y0, x1, 0] +
|
384 |
-
(1-wx)*wy*img_array[y1, x0, 0] + wx*wy*img_array[y1, x1, 0])
|
385 |
-
g = int((1-wx)*(1-wy)*img_array[y0, x0, 1] + wx*(1-wy)*img_array[y0, x1, 1] +
|
386 |
-
(1-wx)*wy*img_array[y1, x0, 1] + wx*wy*img_array[y1, x1, 1])
|
387 |
-
b = int((1-wx)*(1-wy)*img_array[y0, x0, 2] + wx*(1-wy)*img_array[y0, x1, 2] +
|
388 |
-
(1-wx)*wy*img_array[y1, x0, 2] + wx*wy*img_array[y1, x1, 2])
|
389 |
-
|
390 |
-
vertex_colors[vertex_idx, :3] = [r, g, b]
|
391 |
-
vertex_colors[vertex_idx, 3] = 255 # Alpha
|
392 |
-
elif len(img_array.shape) == 3 and img_array.shape[2] == 4: # RGBA
|
393 |
-
for c in range(4): # For each RGBA channel
|
394 |
-
vertex_colors[vertex_idx, c] = int((1-wx)*(1-wy)*img_array[y0, x0, c] +
|
395 |
-
wx*(1-wy)*img_array[y0, x1, c] +
|
396 |
-
(1-wx)*wy*img_array[y1, x0, c] +
|
397 |
-
wx*wy*img_array[y1, x1, c])
|
398 |
-
else:
|
399 |
-
# Handle grayscale with bilinear interpolation
|
400 |
-
gray = int((1-wx)*(1-wy)*img_array[y0, x0] + wx*(1-wy)*img_array[y0, x1] +
|
401 |
-
(1-wx)*wy*img_array[y1, x0] + wx*wy*img_array[y1, x1])
|
402 |
-
vertex_colors[vertex_idx, :3] = [gray, gray, gray]
|
403 |
-
vertex_colors[vertex_idx, 3] = 255
|
404 |
-
|
405 |
-
mesh.visual.vertex_colors = vertex_colors
|
406 |
-
|
407 |
-
# Apply smoothing to get rid of staircase artifacts
|
408 |
-
if detail_level != 'high':
|
409 |
-
# For medium and low detail, apply Laplacian smoothing
|
410 |
-
# but preserve the overall shape
|
411 |
-
mesh = mesh.smoothed(method='laplacian', iterations=1)
|
412 |
-
|
413 |
-
# Calculate and fix normals for better rendering
|
414 |
-
mesh.fix_normals()
|
415 |
-
|
416 |
-
return mesh
|
417 |
|
418 |
@app.route('/health', methods=['GET'])
|
419 |
def health_check():
|
420 |
return jsonify({
|
421 |
"status": "healthy",
|
422 |
-
"model": "
|
423 |
"device": "cuda" if torch.cuda.is_available() else "cpu"
|
424 |
}), 200
|
425 |
|
@@ -477,22 +314,16 @@ def convert_image_to_3d():
|
|
477 |
|
478 |
# Get optional parameters with defaults
|
479 |
try:
|
480 |
-
mesh_resolution = min(int(request.form.get('mesh_resolution', 100)), 200) # Limit max resolution
|
481 |
output_format = request.form.get('output_format', 'obj').lower()
|
482 |
-
detail_level = request.form.get('detail_level', 'medium').lower()
|
483 |
-
|
|
|
484 |
except ValueError:
|
485 |
return jsonify({"error": "Invalid parameter values"}), 400
|
486 |
|
487 |
# Validate output format
|
488 |
-
if output_format not in ['obj', 'glb']:
|
489 |
-
return jsonify({"error": "Unsupported output format. Use 'obj' or '
|
490 |
-
|
491 |
-
# Adjust mesh resolution based on detail level
|
492 |
-
if detail_level == 'high':
|
493 |
-
mesh_resolution = min(int(mesh_resolution * 1.5), 200)
|
494 |
-
elif detail_level == 'low':
|
495 |
-
mesh_resolution = max(int(mesh_resolution * 0.7), 50)
|
496 |
|
497 |
# Create a job ID
|
498 |
job_id = str(uuid.uuid4())
|
@@ -521,14 +352,14 @@ def convert_image_to_3d():
|
|
521 |
processing_jobs[job_id]['thread_alive'] = lambda: thread.is_alive()
|
522 |
|
523 |
try:
|
524 |
-
# Preprocess image
|
525 |
processing_jobs[job_id]['progress'] = 5
|
526 |
-
|
527 |
processing_jobs[job_id]['progress'] = 10
|
528 |
|
529 |
# Load model
|
530 |
try:
|
531 |
-
|
532 |
processing_jobs[job_id]['progress'] = 30
|
533 |
except Exception as e:
|
534 |
processing_jobs[job_id]['status'] = 'error'
|
@@ -537,22 +368,31 @@ def convert_image_to_3d():
|
|
537 |
|
538 |
# Process image with thread-safe timeout
|
539 |
try:
|
540 |
-
def
|
541 |
-
#
|
542 |
-
|
543 |
-
|
|
|
|
|
|
|
544 |
|
545 |
-
#
|
546 |
-
|
547 |
-
|
548 |
-
elif hasattr(depth_map, 'numpy'):
|
549 |
-
depth_map = depth_map.numpy()
|
550 |
-
elif isinstance(depth_map, Image.Image):
|
551 |
-
depth_map = np.array(depth_map)
|
552 |
|
553 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
554 |
|
555 |
-
|
556 |
|
557 |
if error:
|
558 |
if isinstance(error, TimeoutError):
|
@@ -560,73 +400,86 @@ def convert_image_to_3d():
|
|
560 |
processing_jobs[job_id]['error'] = f"Processing timed out after {TIMEOUT_SECONDS} seconds"
|
561 |
return
|
562 |
else:
|
563 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
564 |
|
565 |
processing_jobs[job_id]['progress'] = 60
|
566 |
|
567 |
-
#
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
print(f"Error processing job {job_id}: {str(e)}")
|
577 |
-
print(error_details)
|
578 |
-
return
|
579 |
-
|
580 |
-
# Export based on requested format with enhanced quality settings
|
581 |
-
try:
|
582 |
-
if output_format == 'obj':
|
583 |
-
obj_path = os.path.join(output_dir, "model.obj")
|
584 |
-
|
585 |
-
# Export with normal and texture coordinates
|
586 |
-
mesh.export(
|
587 |
-
obj_path,
|
588 |
-
file_type='obj',
|
589 |
-
include_normals=True,
|
590 |
-
include_texture=True
|
591 |
-
)
|
592 |
-
|
593 |
-
# Create a zip file with OBJ and MTL
|
594 |
-
zip_path = os.path.join(output_dir, "model.zip")
|
595 |
-
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
596 |
-
zipf.write(obj_path, arcname="model.obj")
|
597 |
-
mtl_path = os.path.join(output_dir, "model.mtl")
|
598 |
-
if os.path.exists(mtl_path):
|
599 |
-
zipf.write(mtl_path, arcname="model.mtl")
|
600 |
|
601 |
-
#
|
602 |
-
|
603 |
-
|
604 |
-
zipf.write(
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
616 |
|
617 |
-
|
618 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
619 |
|
620 |
# Update job status
|
621 |
processing_jobs[job_id]['status'] = 'completed'
|
622 |
processing_jobs[job_id]['progress'] = 100
|
|
|
623 |
print(f"Job {job_id} completed successfully")
|
|
|
624 |
except Exception as e:
|
625 |
error_details = traceback.format_exc()
|
626 |
processing_jobs[job_id]['status'] = 'error'
|
627 |
-
processing_jobs[job_id]['error'] = f"Error
|
628 |
-
print(f"Error
|
629 |
print(error_details)
|
|
|
630 |
|
631 |
# Clean up temporary file
|
632 |
if os.path.exists(filepath):
|
@@ -672,10 +525,14 @@ def download_model(job_id):
|
|
672 |
zip_path = os.path.join(output_dir, "model.zip")
|
673 |
if os.path.exists(zip_path):
|
674 |
return send_file(zip_path, as_attachment=True, download_name="model.zip")
|
675 |
-
|
676 |
glb_path = os.path.join(output_dir, "model.glb")
|
677 |
if os.path.exists(glb_path):
|
678 |
return send_file(glb_path, as_attachment=True, download_name="model.glb")
|
|
|
|
|
|
|
|
|
679 |
|
680 |
return jsonify({"error": "File not found"}), 404
|
681 |
|
@@ -692,13 +549,31 @@ def preview_model(job_id):
|
|
692 |
obj_path = os.path.join(output_dir, "model.obj")
|
693 |
if os.path.exists(obj_path):
|
694 |
return send_file(obj_path, mimetype='model/obj')
|
695 |
-
|
696 |
glb_path = os.path.join(output_dir, "model.glb")
|
697 |
if os.path.exists(glb_path):
|
698 |
return send_file(glb_path, mimetype='model/gltf-binary')
|
|
|
|
|
|
|
|
|
699 |
|
700 |
return jsonify({"error": "Model file not found"}), 404
|
701 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
702 |
# Cleanup old jobs periodically
|
703 |
def cleanup_old_jobs():
|
704 |
current_time = time.time()
|
@@ -759,17 +634,23 @@ def model_info(job_id):
|
|
759 |
if os.path.exists(zip_path):
|
760 |
model_stats['package_size'] = os.path.getsize(zip_path)
|
761 |
|
762 |
-
|
763 |
glb_path = os.path.join(output_dir, "model.glb")
|
764 |
if os.path.exists(glb_path):
|
765 |
model_stats['model_size'] = os.path.getsize(glb_path)
|
766 |
|
|
|
|
|
|
|
|
|
|
|
767 |
# Return detailed info
|
768 |
return jsonify({
|
769 |
"status": job['status'],
|
770 |
"model_format": job['output_format'],
|
771 |
"download_url": job['result_url'],
|
772 |
"preview_url": job['preview_url'],
|
|
|
773 |
"model_stats": model_stats,
|
774 |
"created_at": job.get('created_at'),
|
775 |
"completed_at": job.get('completed_at')
|
@@ -778,185 +659,28 @@ def model_info(job_id):
|
|
778 |
@app.route('/', methods=['GET'])
|
779 |
def index():
|
780 |
return jsonify({
|
781 |
-
"message": "
|
782 |
"endpoints": [
|
783 |
"/convert",
|
784 |
"/progress/<job_id>",
|
785 |
"/download/<job_id>",
|
786 |
"/preview/<job_id>",
|
|
|
787 |
"/model-info/<job_id>"
|
788 |
],
|
789 |
"parameters": {
|
790 |
-
"
|
791 |
-
"output_format": "obj or glb",
|
792 |
"detail_level": "low, medium, or high - controls the level of detail in the final model",
|
793 |
-
"
|
|
|
794 |
},
|
795 |
-
"description": "This API creates high-quality 3D models from 2D images with
|
796 |
}), 200
|
797 |
|
798 |
-
# Example endpoint showing how to compare different detail levels
|
799 |
-
@app.route('/detail-comparison', methods=['POST'])
|
800 |
-
def compare_detail_levels():
|
801 |
-
# Check if image is in the request
|
802 |
-
if 'image' not in request.files:
|
803 |
-
return jsonify({"error": "No image provided"}), 400
|
804 |
-
|
805 |
-
file = request.files['image']
|
806 |
-
if file.filename == '':
|
807 |
-
return jsonify({"error": "No image selected"}), 400
|
808 |
-
|
809 |
-
if not allowed_file(file.filename):
|
810 |
-
return jsonify({"error": f"File type not allowed. Supported types: {', '.join(ALLOWED_EXTENSIONS)}"}), 400
|
811 |
-
|
812 |
-
# Create a job ID
|
813 |
-
job_id = str(uuid.uuid4())
|
814 |
-
output_dir = os.path.join(RESULTS_FOLDER, job_id)
|
815 |
-
os.makedirs(output_dir, exist_ok=True)
|
816 |
-
|
817 |
-
# Save the uploaded file
|
818 |
-
filename = secure_filename(file.filename)
|
819 |
-
filepath = os.path.join(app.config['UPLOAD_FOLDER'], f"{job_id}_{filename}")
|
820 |
-
file.save(filepath)
|
821 |
-
|
822 |
-
# Initialize job tracking
|
823 |
-
processing_jobs[job_id] = {
|
824 |
-
'status': 'processing',
|
825 |
-
'progress': 0,
|
826 |
-
'result_url': None,
|
827 |
-
'preview_url': None,
|
828 |
-
'error': None,
|
829 |
-
'output_format': 'glb', # Use GLB for comparison
|
830 |
-
'created_at': time.time(),
|
831 |
-
'comparison': True
|
832 |
-
}
|
833 |
-
|
834 |
-
# Process in separate thread to create 3 different detail levels
|
835 |
-
def process_comparison():
|
836 |
-
thread = threading.current_thread()
|
837 |
-
processing_jobs[job_id]['thread_alive'] = lambda: thread.is_alive()
|
838 |
-
|
839 |
-
try:
|
840 |
-
# Preprocess image
|
841 |
-
image = preprocess_image(filepath)
|
842 |
-
processing_jobs[job_id]['progress'] = 10
|
843 |
-
|
844 |
-
# Load model
|
845 |
-
try:
|
846 |
-
model = load_model()
|
847 |
-
processing_jobs[job_id]['progress'] = 20
|
848 |
-
except Exception as e:
|
849 |
-
processing_jobs[job_id]['status'] = 'error'
|
850 |
-
processing_jobs[job_id]['error'] = f"Error loading model: {str(e)}"
|
851 |
-
return
|
852 |
-
|
853 |
-
# Process image to get depth map
|
854 |
-
try:
|
855 |
-
depth_map = model(image)["depth"]
|
856 |
-
if isinstance(depth_map, torch.Tensor):
|
857 |
-
depth_map = depth_map.cpu().numpy()
|
858 |
-
elif hasattr(depth_map, 'numpy'):
|
859 |
-
depth_map = depth_map.numpy()
|
860 |
-
elif isinstance(depth_map, Image.Image):
|
861 |
-
depth_map = np.array(depth_map)
|
862 |
-
|
863 |
-
processing_jobs[job_id]['progress'] = 40
|
864 |
-
except Exception as e:
|
865 |
-
processing_jobs[job_id]['status'] = 'error'
|
866 |
-
processing_jobs[job_id]['error'] = f"Error estimating depth: {str(e)}"
|
867 |
-
return
|
868 |
-
|
869 |
-
# Create meshes at different detail levels
|
870 |
-
result_urls = {}
|
871 |
-
|
872 |
-
for detail_level in ['low', 'medium', 'high']:
|
873 |
-
try:
|
874 |
-
# Update progress
|
875 |
-
if detail_level == 'low':
|
876 |
-
processing_jobs[job_id]['progress'] = 50
|
877 |
-
elif detail_level == 'medium':
|
878 |
-
processing_jobs[job_id]['progress'] = 70
|
879 |
-
else:
|
880 |
-
processing_jobs[job_id]['progress'] = 90
|
881 |
-
|
882 |
-
# Create mesh with appropriate detail level
|
883 |
-
mesh_resolution = 100 # Fixed resolution for fair comparison
|
884 |
-
if detail_level == 'high':
|
885 |
-
mesh_resolution = 150
|
886 |
-
elif detail_level == 'low':
|
887 |
-
mesh_resolution = 80
|
888 |
-
|
889 |
-
mesh = depth_to_mesh(depth_map, image,
|
890 |
-
resolution=mesh_resolution,
|
891 |
-
detail_level=detail_level)
|
892 |
-
|
893 |
-
# Export as GLB
|
894 |
-
model_path = os.path.join(output_dir, f"model_{detail_level}.glb")
|
895 |
-
mesh.export(model_path, file_type='glb')
|
896 |
-
|
897 |
-
# Add to result URLs
|
898 |
-
result_urls[detail_level] = f"/compare-download/{job_id}/{detail_level}"
|
899 |
-
|
900 |
-
except Exception as e:
|
901 |
-
print(f"Error processing {detail_level} detail level: {str(e)}")
|
902 |
-
# Continue with other detail levels even if one fails
|
903 |
-
|
904 |
-
# Update job status
|
905 |
-
processing_jobs[job_id]['status'] = 'completed'
|
906 |
-
processing_jobs[job_id]['progress'] = 100
|
907 |
-
processing_jobs[job_id]['result_urls'] = result_urls
|
908 |
-
processing_jobs[job_id]['completed_at'] = time.time()
|
909 |
-
|
910 |
-
# Clean up temporary file
|
911 |
-
if os.path.exists(filepath):
|
912 |
-
os.remove(filepath)
|
913 |
-
|
914 |
-
# Force garbage collection
|
915 |
-
gc.collect()
|
916 |
-
if torch.cuda.is_available():
|
917 |
-
torch.cuda.empty_cache()
|
918 |
-
|
919 |
-
except Exception as e:
|
920 |
-
# Handle errors
|
921 |
-
processing_jobs[job_id]['status'] = 'error'
|
922 |
-
processing_jobs[job_id]['error'] = f"Error during processing: {str(e)}"
|
923 |
-
|
924 |
-
# Clean up on error
|
925 |
-
if os.path.exists(filepath):
|
926 |
-
os.remove(filepath)
|
927 |
-
|
928 |
-
# Start processing thread
|
929 |
-
processing_thread = threading.Thread(target=process_comparison)
|
930 |
-
processing_thread.daemon = True
|
931 |
-
processing_thread.start()
|
932 |
-
|
933 |
-
# Return job ID immediately
|
934 |
-
return jsonify({"job_id": job_id, "check_progress_at": f"/progress/{job_id}"}), 202
|
935 |
-
|
936 |
-
@app.route('/compare-download/<job_id>/<detail_level>', methods=['GET'])
|
937 |
-
def download_comparison_model(job_id, detail_level):
|
938 |
-
if job_id not in processing_jobs or processing_jobs[job_id]['status'] != 'completed':
|
939 |
-
return jsonify({"error": "Model not found or processing not complete"}), 404
|
940 |
-
|
941 |
-
if 'comparison' not in processing_jobs[job_id] or not processing_jobs[job_id]['comparison']:
|
942 |
-
return jsonify({"error": "This is not a comparison job"}), 400
|
943 |
-
|
944 |
-
if detail_level not in ['low', 'medium', 'high']:
|
945 |
-
return jsonify({"error": "Invalid detail level"}), 400
|
946 |
-
|
947 |
-
# Get the output directory for this job
|
948 |
-
output_dir = os.path.join(RESULTS_FOLDER, job_id)
|
949 |
-
model_path = os.path.join(output_dir, f"model_{detail_level}.glb")
|
950 |
-
|
951 |
-
if os.path.exists(model_path):
|
952 |
-
return send_file(model_path, as_attachment=True, download_name=f"model_{detail_level}.glb")
|
953 |
-
|
954 |
-
return jsonify({"error": "File not found"}), 404
|
955 |
-
|
956 |
if __name__ == '__main__':
|
957 |
# Start the cleanup thread
|
958 |
cleanup_old_jobs()
|
959 |
|
960 |
# Use port 7860 which is standard for Hugging Face Spaces
|
961 |
port = int(os.environ.get('PORT', 7860))
|
962 |
-
app.run(host='0.0.0.0', port=port)
|
|
|
14 |
from huggingface_hub import snapshot_download
|
15 |
from flask_cors import CORS
|
16 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
app = Flask(__name__)
|
19 |
CORS(app) # Enable CORS for all routes
|
|
|
33 |
os.environ['HF_HOME'] = CACHE_DIR
|
34 |
os.environ['TRANSFORMERS_CACHE'] = os.path.join(CACHE_DIR, 'transformers')
|
35 |
os.environ['HF_DATASETS_CACHE'] = os.path.join(CACHE_DIR, 'datasets')
|
36 |
+
os.environ['NUMBA_THREADING_LAYER'] = 'omp'
|
37 |
|
38 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
39 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max
|
|
|
42 |
processing_jobs = {}
|
43 |
|
44 |
# Global model variables
|
45 |
+
openlrm_processor = None
|
46 |
+
openlrm_model = None
|
47 |
model_loaded = False
|
48 |
model_loading = False
|
49 |
|
|
|
55 |
class TimeoutError(Exception):
|
56 |
pass
|
57 |
|
58 |
+
# Install necessary dependencies
|
59 |
+
def install_dependencies():
|
60 |
+
try:
|
61 |
+
import subprocess
|
62 |
+
# Install core dependencies
|
63 |
+
subprocess.check_call([
|
64 |
+
"pip", "install",
|
65 |
+
"torch>=2.0.0",
|
66 |
+
"lpips",
|
67 |
+
"omegaconf",
|
68 |
+
"transformers",
|
69 |
+
"safetensors",
|
70 |
+
"accelerate",
|
71 |
+
"imageio[ffmpeg]",
|
72 |
+
"PyMCubes",
|
73 |
+
"trimesh",
|
74 |
+
"opencv-python",
|
75 |
+
"rembg[gpu,cli]",
|
76 |
+
"httpx[socks]",
|
77 |
+
"tensorboard"
|
78 |
+
])
|
79 |
+
|
80 |
+
# Clone OpenLRM repository
|
81 |
+
if not os.path.exists("OpenLRM"):
|
82 |
+
subprocess.check_call(["git", "clone", "https://github.com/3DTopia/OpenLRM.git"])
|
83 |
+
|
84 |
+
# Add OpenLRM to python path
|
85 |
+
if not "OpenLRM" in os.getenv("PYTHONPATH", ""):
|
86 |
+
os.environ["PYTHONPATH"] = f"{os.getenv('PYTHONPATH', '')}:OpenLRM"
|
87 |
+
|
88 |
+
print("Successfully installed dependencies")
|
89 |
+
except Exception as e:
|
90 |
+
print(f"Error installing dependencies: {str(e)}")
|
91 |
+
print(traceback.format_exc())
|
92 |
+
raise
|
93 |
+
|
94 |
# Thread-safe timeout implementation
|
95 |
def process_with_timeout(function, args, timeout):
|
96 |
result = [None]
|
|
|
124 |
def allowed_file(filename):
|
125 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
126 |
|
127 |
+
# Image preprocessing with automatic background removal
|
128 |
def preprocess_image(image_path):
|
129 |
+
try:
|
130 |
+
from rembg import remove
|
131 |
+
with Image.open(image_path) as img:
|
132 |
+
img = img.convert("RGBA")
|
133 |
+
|
134 |
+
# Resize if the image is too large
|
135 |
+
if img.width > MAX_DIMENSION or img.height > MAX_DIMENSION:
|
136 |
+
# Calculate new dimensions while preserving aspect ratio
|
137 |
+
if img.width > img.height:
|
138 |
+
new_width = MAX_DIMENSION
|
139 |
+
new_height = int(img.height * (MAX_DIMENSION / img.width))
|
140 |
+
else:
|
141 |
+
new_height = MAX_DIMENSION
|
142 |
+
new_width = int(img.width * (MAX_DIMENSION / img.height))
|
143 |
+
|
144 |
+
# Use high-quality Lanczos resampling for better detail preservation
|
145 |
+
img = img.resize((new_width, new_height), Image.LANCZOS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
+
# Remove background automatically
|
148 |
+
img_no_bg = remove(img)
|
149 |
|
150 |
+
# Save both versions for flexibility
|
151 |
+
img_path = image_path.replace(".jpg", ".png").replace(".jpeg", ".png")
|
152 |
+
img_no_bg_path = image_path.rsplit(".", 1)[0] + "_nobg.png"
|
153 |
|
154 |
+
img.save(img_path)
|
155 |
+
img_no_bg.save(img_no_bg_path)
|
156 |
|
157 |
+
return img_path, img_no_bg_path
|
158 |
+
except Exception as e:
|
159 |
+
print(f"Error in image preprocessing: {str(e)}")
|
160 |
+
print(traceback.format_exc())
|
161 |
+
# Return original if rembg fails
|
162 |
+
return image_path, image_path
|
163 |
|
164 |
+
# Initialize OpenLRM model
|
165 |
def load_model():
|
166 |
+
global openlrm_model, openlrm_processor, model_loaded, model_loading
|
167 |
|
168 |
if model_loaded:
|
169 |
+
return openlrm_model, openlrm_processor
|
170 |
|
171 |
if model_loading:
|
172 |
# Wait for model to load if it's already in progress
|
173 |
while model_loading and not model_loaded:
|
174 |
time.sleep(0.5)
|
175 |
+
return openlrm_model, openlrm_processor
|
176 |
|
177 |
try:
|
178 |
model_loading = True
|
179 |
+
print("Starting OpenLRM model loading...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
+
# Import OpenLRM components
|
182 |
+
try:
|
183 |
+
from openlrm.utils.preprocess import Preprocessor
|
184 |
+
from openlrm.utils.config import load_config
|
185 |
+
from openlrm.models.registry import get_model
|
186 |
+
from openlrm.pipelines.inference import InferencePipeline
|
187 |
+
|
188 |
+
# Use the small model variant for HF free tier
|
189 |
+
model_name = "zxhezexin/openlrm-mix-small-1.1" # Smallest model for HF free tier
|
190 |
+
|
191 |
+
# Load configuration for inference
|
192 |
+
config_path = "OpenLRM/configs/infer-s.yaml" # Small model config
|
193 |
+
config = load_config(config_path)
|
194 |
+
config.model_name = model_name
|
195 |
+
|
196 |
+
# Initialize preprocessor
|
197 |
+
openlrm_processor = Preprocessor()
|
198 |
+
|
199 |
+
# Initialize model and inference pipeline
|
200 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
201 |
+
openlrm_model = InferencePipeline(config, device)
|
202 |
+
|
203 |
+
print(f"OpenLRM model loaded successfully on {device}")
|
204 |
+
model_loaded = True
|
205 |
+
|
206 |
+
# Optimize memory usage
|
207 |
+
if device == "cuda":
|
208 |
+
torch.cuda.empty_cache()
|
209 |
+
|
210 |
+
return openlrm_model, openlrm_processor
|
211 |
+
|
212 |
+
except ImportError as e:
|
213 |
+
print(f"ImportError: {str(e)}")
|
214 |
+
print("Installing OpenLRM dependencies...")
|
215 |
+
install_dependencies()
|
216 |
+
# Try loading again after installing dependencies
|
217 |
+
from openlrm.utils.preprocess import Preprocessor
|
218 |
+
from openlrm.utils.config import load_config
|
219 |
+
from openlrm.models.registry import get_model
|
220 |
+
from openlrm.pipelines.inference import InferencePipeline
|
221 |
+
|
222 |
+
model_name = "zxhezexin/openlrm-mix-small-1.1"
|
223 |
+
config_path = "OpenLRM/configs/infer-s.yaml"
|
224 |
+
config = load_config(config_path)
|
225 |
+
config.model_name = model_name
|
226 |
+
|
227 |
+
openlrm_processor = Preprocessor()
|
228 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
229 |
+
openlrm_model = InferencePipeline(config, device)
|
230 |
+
|
231 |
+
model_loaded = True
|
232 |
+
print(f"OpenLRM model loaded successfully on {device} after installing dependencies")
|
233 |
+
return openlrm_model, openlrm_processor
|
234 |
+
|
235 |
except Exception as e:
|
236 |
+
print(f"Error loading OpenLRM model: {str(e)}")
|
237 |
print(traceback.format_exc())
|
238 |
raise
|
239 |
finally:
|
240 |
model_loading = False
|
241 |
|
242 |
+
# Fallback to original depth-based implementation if OpenLRM fails
|
243 |
+
def depth_based_fallback(image_path, output_dir, detail_level='high'):
|
244 |
+
try:
|
245 |
+
# This uses your original depth estimation implementation as a fallback
|
246 |
+
# [Implementation would go here]
|
247 |
+
print("Using depth-based fallback implementation")
|
248 |
+
# Your original implementation could be added here
|
249 |
+
pass
|
250 |
+
except Exception as e:
|
251 |
+
print(f"Fallback also failed: {str(e)}")
|
252 |
+
return False
|
253 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
255 |
@app.route('/health', methods=['GET'])
|
256 |
def health_check():
|
257 |
return jsonify({
|
258 |
"status": "healthy",
|
259 |
+
"model": "OpenLRM Image-to-3D Model Generator",
|
260 |
"device": "cuda" if torch.cuda.is_available() else "cpu"
|
261 |
}), 200
|
262 |
|
|
|
314 |
|
315 |
# Get optional parameters with defaults
|
316 |
try:
|
|
|
317 |
output_format = request.form.get('output_format', 'obj').lower()
|
318 |
+
detail_level = request.form.get('detail_level', 'medium').lower()
|
319 |
+
source_cam_dist = float(request.form.get('source_cam_dist', 2.0))
|
320 |
+
remove_bg = request.form.get('remove_bg', 'true').lower() == 'true'
|
321 |
except ValueError:
|
322 |
return jsonify({"error": "Invalid parameter values"}), 400
|
323 |
|
324 |
# Validate output format
|
325 |
+
if output_format not in ['obj', 'glb', 'ply']:
|
326 |
+
return jsonify({"error": "Unsupported output format. Use 'obj', 'glb' or 'ply'"}), 400
|
|
|
|
|
|
|
|
|
|
|
|
|
327 |
|
328 |
# Create a job ID
|
329 |
job_id = str(uuid.uuid4())
|
|
|
352 |
processing_jobs[job_id]['thread_alive'] = lambda: thread.is_alive()
|
353 |
|
354 |
try:
|
355 |
+
# Preprocess image
|
356 |
processing_jobs[job_id]['progress'] = 5
|
357 |
+
img_path, img_no_bg_path = preprocess_image(filepath) if remove_bg else (filepath, filepath)
|
358 |
processing_jobs[job_id]['progress'] = 10
|
359 |
|
360 |
# Load model
|
361 |
try:
|
362 |
+
openlrm_model, openlrm_processor = load_model()
|
363 |
processing_jobs[job_id]['progress'] = 30
|
364 |
except Exception as e:
|
365 |
processing_jobs[job_id]['status'] = 'error'
|
|
|
368 |
|
369 |
# Process image with thread-safe timeout
|
370 |
try:
|
371 |
+
def generate_3d():
|
372 |
+
# Import here to ensure it's within the thread
|
373 |
+
import os
|
374 |
+
from openlrm.pipelines.inference import InferencePipeline
|
375 |
+
|
376 |
+
# Process with OpenLRM
|
377 |
+
image_to_use = img_no_bg_path if remove_bg else img_path
|
378 |
|
379 |
+
# Configure export paths
|
380 |
+
dump_video_path = os.path.join(output_dir, "output.mp4")
|
381 |
+
dump_mesh_path = os.path.join(output_dir, "output.ply") # OpenLRM uses .ply format
|
|
|
|
|
|
|
|
|
382 |
|
383 |
+
# Process with OpenLRM
|
384 |
+
openlrm_model.infer_single(
|
385 |
+
image_path=image_to_use,
|
386 |
+
source_cam_dist=source_cam_dist,
|
387 |
+
export_video=True,
|
388 |
+
export_mesh=True,
|
389 |
+
dump_video_path=dump_video_path,
|
390 |
+
dump_mesh_path=dump_mesh_path,
|
391 |
+
)
|
392 |
+
|
393 |
+
return dump_video_path, dump_mesh_path
|
394 |
|
395 |
+
(video_path, mesh_path), error = process_with_timeout(generate_3d, [], TIMEOUT_SECONDS)
|
396 |
|
397 |
if error:
|
398 |
if isinstance(error, TimeoutError):
|
|
|
400 |
processing_jobs[job_id]['error'] = f"Processing timed out after {TIMEOUT_SECONDS} seconds"
|
401 |
return
|
402 |
else:
|
403 |
+
# Try fallback implementation if OpenLRM fails
|
404 |
+
processing_jobs[job_id]['progress'] = 35
|
405 |
+
processing_jobs[job_id]['error'] = f"Primary method failed: {str(error)}. Trying fallback..."
|
406 |
+
|
407 |
+
# Use fallback depth-based implementation
|
408 |
+
if depth_based_fallback(img_path, output_dir, detail_level):
|
409 |
+
processing_jobs[job_id]['progress'] = 60
|
410 |
+
processing_jobs[job_id]['error'] = None # Clear error if fallback succeeded
|
411 |
+
else:
|
412 |
+
raise Exception(f"Both primary and fallback 3D generation methods failed: {str(error)}")
|
413 |
|
414 |
processing_jobs[job_id]['progress'] = 60
|
415 |
|
416 |
+
# Convert PLY to requested format if needed
|
417 |
+
mesh_path_orig = os.path.join(output_dir, "output.ply")
|
418 |
+
if os.path.exists(mesh_path_orig):
|
419 |
+
if output_format == 'obj':
|
420 |
+
# Convert PLY to OBJ
|
421 |
+
import trimesh
|
422 |
+
mesh = trimesh.load(mesh_path_orig)
|
423 |
+
obj_path = os.path.join(output_dir, "model.obj")
|
424 |
+
mesh.export(obj_path, file_type='obj')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
425 |
|
426 |
+
# Create a zip file with OBJ and MTL
|
427 |
+
zip_path = os.path.join(output_dir, "model.zip")
|
428 |
+
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
429 |
+
zipf.write(obj_path, arcname="model.obj")
|
430 |
+
mtl_path = os.path.join(output_dir, "model.mtl")
|
431 |
+
if os.path.exists(mtl_path):
|
432 |
+
zipf.write(mtl_path, arcname="model.mtl")
|
433 |
+
|
434 |
+
# Include texture file if it exists
|
435 |
+
texture_path = os.path.join(output_dir, "model.png")
|
436 |
+
if os.path.exists(texture_path):
|
437 |
+
zipf.write(texture_path, arcname="model.png")
|
438 |
+
|
439 |
+
processing_jobs[job_id]['result_url'] = f"/download/{job_id}"
|
440 |
+
processing_jobs[job_id]['preview_url'] = f"/preview/{job_id}"
|
441 |
+
|
442 |
+
elif output_format == 'glb':
|
443 |
+
# Convert PLY to GLB
|
444 |
+
import trimesh
|
445 |
+
mesh = trimesh.load(mesh_path_orig)
|
446 |
+
glb_path = os.path.join(output_dir, "model.glb")
|
447 |
+
mesh.export(glb_path, file_type='glb')
|
448 |
+
|
449 |
+
processing_jobs[job_id]['result_url'] = f"/download/{job_id}"
|
450 |
+
processing_jobs[job_id]['preview_url'] = f"/preview/{job_id}"
|
451 |
|
452 |
+
else: # Keep as PLY format
|
453 |
+
import shutil
|
454 |
+
ply_path = os.path.join(output_dir, "model.ply")
|
455 |
+
shutil.copy(mesh_path_orig, ply_path)
|
456 |
+
|
457 |
+
processing_jobs[job_id]['result_url'] = f"/download/{job_id}"
|
458 |
+
processing_jobs[job_id]['preview_url'] = f"/preview/{job_id}"
|
459 |
+
|
460 |
+
processing_jobs[job_id]['progress'] = 90
|
461 |
+
|
462 |
+
# Also save the video preview
|
463 |
+
video_path_orig = os.path.join(output_dir, "output.mp4")
|
464 |
+
if os.path.exists(video_path_orig):
|
465 |
+
preview_path = os.path.join(output_dir, "preview.mp4")
|
466 |
+
import shutil
|
467 |
+
shutil.copy(video_path_orig, preview_path)
|
468 |
+
processing_jobs[job_id]['preview_video'] = f"/preview-video/{job_id}"
|
469 |
|
470 |
# Update job status
|
471 |
processing_jobs[job_id]['status'] = 'completed'
|
472 |
processing_jobs[job_id]['progress'] = 100
|
473 |
+
processing_jobs[job_id]['completed_at'] = time.time()
|
474 |
print(f"Job {job_id} completed successfully")
|
475 |
+
|
476 |
except Exception as e:
|
477 |
error_details = traceback.format_exc()
|
478 |
processing_jobs[job_id]['status'] = 'error'
|
479 |
+
processing_jobs[job_id]['error'] = f"Error during processing: {str(e)}"
|
480 |
+
print(f"Error processing job {job_id}: {str(e)}")
|
481 |
print(error_details)
|
482 |
+
return
|
483 |
|
484 |
# Clean up temporary file
|
485 |
if os.path.exists(filepath):
|
|
|
525 |
zip_path = os.path.join(output_dir, "model.zip")
|
526 |
if os.path.exists(zip_path):
|
527 |
return send_file(zip_path, as_attachment=True, download_name="model.zip")
|
528 |
+
elif output_format == 'glb':
|
529 |
glb_path = os.path.join(output_dir, "model.glb")
|
530 |
if os.path.exists(glb_path):
|
531 |
return send_file(glb_path, as_attachment=True, download_name="model.glb")
|
532 |
+
else: # ply
|
533 |
+
ply_path = os.path.join(output_dir, "model.ply")
|
534 |
+
if os.path.exists(ply_path):
|
535 |
+
return send_file(ply_path, as_attachment=True, download_name="model.ply")
|
536 |
|
537 |
return jsonify({"error": "File not found"}), 404
|
538 |
|
|
|
549 |
obj_path = os.path.join(output_dir, "model.obj")
|
550 |
if os.path.exists(obj_path):
|
551 |
return send_file(obj_path, mimetype='model/obj')
|
552 |
+
elif output_format == 'glb':
|
553 |
glb_path = os.path.join(output_dir, "model.glb")
|
554 |
if os.path.exists(glb_path):
|
555 |
return send_file(glb_path, mimetype='model/gltf-binary')
|
556 |
+
else: # ply
|
557 |
+
ply_path = os.path.join(output_dir, "model.ply")
|
558 |
+
if os.path.exists(ply_path):
|
559 |
+
return send_file(ply_path, mimetype='model/ply')
|
560 |
|
561 |
return jsonify({"error": "Model file not found"}), 404
|
562 |
|
563 |
+
@app.route('/preview-video/<job_id>', methods=['GET'])
|
564 |
+
def preview_video(job_id):
|
565 |
+
if job_id not in processing_jobs or processing_jobs[job_id]['status'] != 'completed':
|
566 |
+
return jsonify({"error": "Video not found or processing not complete"}), 404
|
567 |
+
|
568 |
+
# Get the output directory for this job
|
569 |
+
output_dir = os.path.join(RESULTS_FOLDER, job_id)
|
570 |
+
preview_video_path = os.path.join(output_dir, "preview.mp4")
|
571 |
+
|
572 |
+
if os.path.exists(preview_video_path):
|
573 |
+
return send_file(preview_video_path, mimetype='video/mp4')
|
574 |
+
|
575 |
+
return jsonify({"error": "Video file not found"}), 404
|
576 |
+
|
577 |
# Cleanup old jobs periodically
|
578 |
def cleanup_old_jobs():
|
579 |
current_time = time.time()
|
|
|
634 |
if os.path.exists(zip_path):
|
635 |
model_stats['package_size'] = os.path.getsize(zip_path)
|
636 |
|
637 |
+
elif job['output_format'] == 'glb':
|
638 |
glb_path = os.path.join(output_dir, "model.glb")
|
639 |
if os.path.exists(glb_path):
|
640 |
model_stats['model_size'] = os.path.getsize(glb_path)
|
641 |
|
642 |
+
else: # ply
|
643 |
+
ply_path = os.path.join(output_dir, "model.ply")
|
644 |
+
if os.path.exists(ply_path):
|
645 |
+
model_stats['model_size'] = os.path.getsize(ply_path)
|
646 |
+
|
647 |
# Return detailed info
|
648 |
return jsonify({
|
649 |
"status": job['status'],
|
650 |
"model_format": job['output_format'],
|
651 |
"download_url": job['result_url'],
|
652 |
"preview_url": job['preview_url'],
|
653 |
+
"preview_video": job.get('preview_video'),
|
654 |
"model_stats": model_stats,
|
655 |
"created_at": job.get('created_at'),
|
656 |
"completed_at": job.get('completed_at')
|
|
|
659 |
@app.route('/', methods=['GET'])
|
660 |
def index():
|
661 |
return jsonify({
|
662 |
+
"message": "OpenLRM Image-to-3D Model Generator API",
|
663 |
"endpoints": [
|
664 |
"/convert",
|
665 |
"/progress/<job_id>",
|
666 |
"/download/<job_id>",
|
667 |
"/preview/<job_id>",
|
668 |
+
"/preview-video/<job_id>",
|
669 |
"/model-info/<job_id>"
|
670 |
],
|
671 |
"parameters": {
|
672 |
+
"output_format": "obj, glb, or ply",
|
|
|
673 |
"detail_level": "low, medium, or high - controls the level of detail in the final model",
|
674 |
+
"source_cam_dist": "Camera distance from object (1.0-3.5, default 2.0)",
|
675 |
+
"remove_bg": "true/false - automatically remove background"
|
676 |
},
|
677 |
+
"description": "This API creates high-quality 3D models from 2D images with full structural completion from all angles"
|
678 |
}), 200
|
679 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
680 |
if __name__ == '__main__':
|
681 |
# Start the cleanup thread
|
682 |
cleanup_old_jobs()
|
683 |
|
684 |
# Use port 7860 which is standard for Hugging Face Spaces
|
685 |
port = int(os.environ.get('PORT', 7860))
|
686 |
+
app.run(host='0.0.0.0', port=port)
|