Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,14 +11,14 @@ import io
|
|
11 |
import zipfile
|
12 |
import uuid
|
13 |
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
|
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
|
@@ -46,11 +46,13 @@ 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 |
|
53 |
-
#
|
54 |
TIMEOUT_SECONDS = 240 # 4 minutes max for processing
|
55 |
MAX_DIMENSION = 512 # Max image dimension to process
|
56 |
|
@@ -91,7 +93,7 @@ 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 |
-
# Enhanced image preprocessing
|
95 |
def preprocess_image(image_path):
|
96 |
with Image.open(image_path) as img:
|
97 |
img = img.convert("RGB")
|
@@ -106,14 +108,13 @@ def preprocess_image(image_path):
|
|
106 |
new_height = MAX_DIMENSION
|
107 |
new_width = int(img.width * (MAX_DIMENSION / img.height))
|
108 |
|
109 |
-
# Use high-quality Lanczos resampling
|
110 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
111 |
|
112 |
# Convert to numpy array for additional preprocessing
|
113 |
img_array = np.array(img)
|
114 |
|
115 |
-
#
|
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)
|
@@ -134,73 +135,313 @@ def preprocess_image(image_path):
|
|
134 |
|
135 |
return img
|
136 |
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("
|
152 |
|
153 |
-
#
|
154 |
-
|
155 |
-
|
156 |
|
157 |
-
# Download model
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
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 |
-
#
|
|
|
178 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
179 |
|
180 |
-
#
|
181 |
-
|
182 |
-
"depth-estimation",
|
183 |
-
model=model_name,
|
184 |
-
device=device if device == "cuda" else -1,
|
185 |
-
cache_dir=CACHE_DIR
|
186 |
-
)
|
187 |
|
188 |
-
#
|
189 |
-
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
model_loaded = True
|
193 |
-
print(f"
|
194 |
-
return
|
195 |
|
196 |
except Exception as e:
|
197 |
-
print(f"Error loading model: {str(e)}")
|
198 |
print(traceback.format_exc())
|
199 |
-
|
|
|
|
|
|
|
200 |
finally:
|
201 |
model_loading = False
|
202 |
|
203 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
def enhance_depth_map(depth_map, detail_level='medium'):
|
205 |
"""Apply sophisticated processing to enhance depth map details"""
|
206 |
# Convert to numpy array if needed
|
@@ -214,7 +455,7 @@ def enhance_depth_map(depth_map, detail_level='medium'):
|
|
214 |
# Create a copy for processing
|
215 |
enhanced_depth = depth_map.copy().astype(np.float32)
|
216 |
|
217 |
-
# Remove outliers using percentile clipping
|
218 |
p_low, p_high = np.percentile(enhanced_depth, [1, 99])
|
219 |
enhanced_depth = np.clip(enhanced_depth, p_low, p_high)
|
220 |
|
@@ -223,33 +464,26 @@ def enhance_depth_map(depth_map, detail_level='medium'):
|
|
223 |
|
224 |
# Apply different enhancement methods based on detail level
|
225 |
if detail_level == 'high':
|
226 |
-
# Apply unsharp masking for edge enhancement
|
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
|
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
|
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
|
253 |
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.7)
|
254 |
|
255 |
# Normalize again after processing
|
@@ -257,9 +491,9 @@ def enhance_depth_map(depth_map, detail_level='medium'):
|
|
257 |
|
258 |
return enhanced_depth
|
259 |
|
260 |
-
#
|
261 |
def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
262 |
-
"""Convert depth map to 3D mesh with
|
263 |
# First, enhance the depth map for better details
|
264 |
enhanced_depth = enhance_depth_map(depth_map, detail_level)
|
265 |
|
@@ -271,50 +505,31 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
271 |
y = np.linspace(0, h-1, resolution)
|
272 |
x_grid, y_grid = np.meshgrid(x, y)
|
273 |
|
274 |
-
#
|
275 |
-
|
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
|
303 |
elif detail_level == 'medium':
|
304 |
z_scaling = 2.0 # Standard depth
|
305 |
else:
|
306 |
-
z_scaling = 1.5 #
|
307 |
|
308 |
z_values = z_values * z_scaling
|
309 |
|
310 |
-
# Normalize
|
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)
|
318 |
faces = []
|
319 |
for i in range(resolution-1):
|
320 |
for j in range(resolution-1):
|
@@ -323,32 +538,16 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
323 |
p3 = (i + 1) * resolution + j
|
324 |
p4 = (i + 1) * resolution + (j + 1)
|
325 |
|
326 |
-
#
|
327 |
-
|
328 |
-
|
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
|
352 |
if image:
|
353 |
# Convert to numpy array if needed
|
354 |
if isinstance(image, Image.Image):
|
@@ -356,30 +555,29 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
356 |
else:
|
357 |
img_array = image
|
358 |
|
359 |
-
# Create vertex colors
|
360 |
if resolution <= img_array.shape[0] and resolution <= img_array.shape[1]:
|
361 |
-
# Create vertex colors by sampling the image
|
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
|
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
|
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 |
-
#
|
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
|
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] +
|
@@ -396,9 +594,9 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
396 |
(1-wx)*wy*img_array[y1, x0, c] +
|
397 |
wx*wy*img_array[y1, x1, c])
|
398 |
else:
|
399 |
-
# Handle grayscale
|
400 |
gray = int((1-wx)*(1-wy)*img_array[y0, x0] + wx*(1-wy)*img_array[y0, x1] +
|
401 |
-
|
402 |
vertex_colors[vertex_idx, :3] = [gray, gray, gray]
|
403 |
vertex_colors[vertex_idx, 3] = 255
|
404 |
|
@@ -406,20 +604,104 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
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 |
-
#
|
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": "Enhanced
|
423 |
"device": "cuda" if torch.cuda.is_available() else "cpu"
|
424 |
}), 200
|
425 |
|
@@ -446,15 +728,15 @@ def progress(job_id):
|
|
446 |
time.sleep(0.5)
|
447 |
check_count += 1
|
448 |
|
449 |
-
#
|
450 |
if check_count > 60: # 30 seconds with no updates
|
451 |
if 'thread_alive' in job and not job['thread_alive']():
|
452 |
job['status'] = 'error'
|
453 |
job['error'] = 'Processing thread died unexpectedly'
|
454 |
break
|
455 |
check_count = 0
|
456 |
-
|
457 |
-
|
458 |
if job['status'] == 'completed':
|
459 |
yield f"data: {json.dumps({'status': 'completed', 'progress': 100, 'result_url': job['result_url'], 'preview_url': job['preview_url']})}\n\n"
|
460 |
else:
|
@@ -480,7 +762,7 @@ def convert_image_to_3d():
|
|
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() # Parameter for detail level
|
483 |
-
|
484 |
except ValueError:
|
485 |
return jsonify({"error": "Invalid parameter values"}), 400
|
486 |
|
@@ -488,12 +770,6 @@ def convert_image_to_3d():
|
|
488 |
if output_format not in ['obj', 'glb']:
|
489 |
return jsonify({"error": "Unsupported output format. Use 'obj' or 'glb'"}), 400
|
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())
|
499 |
output_dir = os.path.join(RESULTS_FOLDER, job_id)
|
@@ -526,58 +802,17 @@ def convert_image_to_3d():
|
|
526 |
image = preprocess_image(filepath)
|
527 |
processing_jobs[job_id]['progress'] = 10
|
528 |
|
529 |
-
#
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
return
|
537 |
|
538 |
-
|
539 |
-
try:
|
540 |
-
def estimate_depth():
|
541 |
-
# Get depth map
|
542 |
-
result = model(image)
|
543 |
-
depth_map = result["depth"]
|
544 |
-
|
545 |
-
# Convert to numpy array if needed
|
546 |
-
if isinstance(depth_map, torch.Tensor):
|
547 |
-
depth_map = depth_map.cpu().numpy()
|
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 |
-
return depth_map
|
554 |
-
|
555 |
-
depth_map, error = process_with_timeout(estimate_depth, [], TIMEOUT_SECONDS)
|
556 |
-
|
557 |
-
if error:
|
558 |
-
if isinstance(error, TimeoutError):
|
559 |
-
processing_jobs[job_id]['status'] = 'error'
|
560 |
-
processing_jobs[job_id]['error'] = f"Processing timed out after {TIMEOUT_SECONDS} seconds"
|
561 |
-
return
|
562 |
-
else:
|
563 |
-
raise error
|
564 |
-
|
565 |
-
processing_jobs[job_id]['progress'] = 60
|
566 |
-
|
567 |
-
# Create mesh from depth map with enhanced detail handling
|
568 |
-
mesh_resolution_int = int(mesh_resolution)
|
569 |
-
mesh = depth_to_mesh(depth_map, image, resolution=mesh_resolution_int, detail_level=detail_level)
|
570 |
-
processing_jobs[job_id]['progress'] = 80
|
571 |
-
|
572 |
-
except Exception as e:
|
573 |
-
error_details = traceback.format_exc()
|
574 |
-
processing_jobs[job_id]['status'] = 'error'
|
575 |
-
processing_jobs[job_id]['error'] = f"Error during processing: {str(e)}"
|
576 |
-
print(f"Error processing job {job_id}: {str(e)}")
|
577 |
-
print(error_details)
|
578 |
-
return
|
579 |
|
580 |
-
# Export based on requested format
|
581 |
try:
|
582 |
if output_format == 'obj':
|
583 |
obj_path = os.path.join(output_dir, "model.obj")
|
@@ -607,7 +842,7 @@ def convert_image_to_3d():
|
|
607 |
processing_jobs[job_id]['preview_url'] = f"/preview/{job_id}"
|
608 |
|
609 |
elif output_format == 'glb':
|
610 |
-
# Export as GLB
|
611 |
glb_path = os.path.join(output_dir, "model.glb")
|
612 |
mesh.export(
|
613 |
glb_path,
|
@@ -620,6 +855,7 @@ def convert_image_to_3d():
|
|
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()
|
@@ -729,7 +965,7 @@ def cleanup_old_jobs():
|
|
729 |
# Schedule the next cleanup
|
730 |
threading.Timer(300, cleanup_old_jobs).start() # Run every 5 minutes
|
731 |
|
732 |
-
#
|
733 |
@app.route('/model-info/<job_id>', methods=['GET'])
|
734 |
def model_info(job_id):
|
735 |
if job_id not in processing_jobs:
|
@@ -778,7 +1014,7 @@ def model_info(job_id):
|
|
778 |
@app.route('/', methods=['GET'])
|
779 |
def index():
|
780 |
return jsonify({
|
781 |
-
"message": "Enhanced
|
782 |
"endpoints": [
|
783 |
"/convert",
|
784 |
"/progress/<job_id>",
|
@@ -790,14 +1026,14 @@ def index():
|
|
790 |
"mesh_resolution": "Integer (50-200), controls mesh density",
|
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
|
799 |
-
@app.route('/
|
800 |
-
def
|
801 |
# Check if image is in the request
|
802 |
if 'image' not in request.files:
|
803 |
return jsonify({"error": "No image provided"}), 400
|
@@ -823,15 +1059,13 @@ def compare_detail_levels():
|
|
823 |
processing_jobs[job_id] = {
|
824 |
'status': 'processing',
|
825 |
'progress': 0,
|
826 |
-
'
|
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
|
835 |
def process_comparison():
|
836 |
thread = threading.current_thread()
|
837 |
processing_jobs[job_id]['thread_alive'] = lambda: thread.is_alive()
|
@@ -841,65 +1075,37 @@ def compare_detail_levels():
|
|
841 |
image = preprocess_image(filepath)
|
842 |
processing_jobs[job_id]['progress'] = 10
|
843 |
|
844 |
-
#
|
845 |
-
|
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
|
854 |
try:
|
855 |
-
|
856 |
-
|
857 |
-
|
858 |
-
|
859 |
-
|
860 |
-
|
861 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
862 |
|
863 |
-
processing_jobs[job_id]['progress'] = 40
|
864 |
except Exception as e:
|
865 |
-
|
866 |
-
|
867 |
-
|
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'
|
@@ -933,30 +1139,56 @@ def compare_detail_levels():
|
|
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>/<
|
937 |
-
def download_comparison_model(job_id,
|
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
|
945 |
-
return jsonify({"error": "Invalid
|
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_{
|
950 |
|
951 |
if os.path.exists(model_path):
|
952 |
-
return send_file(model_path, as_attachment=True, download_name=f"model_{
|
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)
|
|
|
11 |
import zipfile
|
12 |
import uuid
|
13 |
import traceback
|
14 |
+
from huggingface_hub import snapshot_download, hf_hub_download
|
15 |
from flask_cors import CORS
|
16 |
import numpy as np
|
17 |
import trimesh
|
18 |
+
from scipy.ndimage import gaussian_filter
|
|
|
|
|
19 |
import cv2
|
20 |
+
import torch.nn.functional as F
|
21 |
+
from transformers import AutoFeatureExtractor, AutoModelForDepthEstimation
|
22 |
|
23 |
app = Flask(__name__)
|
24 |
CORS(app) # Enable CORS for all routes
|
|
|
46 |
processing_jobs = {}
|
47 |
|
48 |
# Global model variables
|
49 |
+
depth_model = None
|
50 |
+
feature_extractor = None
|
51 |
+
openlrm_model = None
|
52 |
model_loaded = False
|
53 |
model_loading = False
|
54 |
|
55 |
+
# Constants for processing
|
56 |
TIMEOUT_SECONDS = 240 # 4 minutes max for processing
|
57 |
MAX_DIMENSION = 512 # Max image dimension to process
|
58 |
|
|
|
93 |
def allowed_file(filename):
|
94 |
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
95 |
|
96 |
+
# Enhanced image preprocessing
|
97 |
def preprocess_image(image_path):
|
98 |
with Image.open(image_path) as img:
|
99 |
img = img.convert("RGB")
|
|
|
108 |
new_height = MAX_DIMENSION
|
109 |
new_width = int(img.width * (MAX_DIMENSION / img.height))
|
110 |
|
111 |
+
# Use high-quality Lanczos resampling
|
112 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
113 |
|
114 |
# Convert to numpy array for additional preprocessing
|
115 |
img_array = np.array(img)
|
116 |
|
117 |
+
# Apply adaptive histogram equalization for better contrast
|
|
|
118 |
if len(img_array.shape) == 3 and img_array.shape[2] == 3:
|
119 |
# Convert to LAB color space
|
120 |
lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2LAB)
|
|
|
135 |
|
136 |
return img
|
137 |
|
138 |
+
# Remove background function to help with 3D reconstruction
|
139 |
+
def remove_background(image):
|
140 |
+
try:
|
141 |
+
import rembg
|
142 |
+
return rembg.remove(image)
|
143 |
+
except ImportError:
|
144 |
+
print("Rembg not available, skipping background removal")
|
145 |
+
# Create a white background transparent image as fallback
|
146 |
+
return image
|
147 |
+
|
148 |
+
# Load OpenLRM model for 3D reconstruction
|
149 |
+
def load_openlrm_model():
|
150 |
+
global openlrm_model, model_loaded, model_loading
|
151 |
|
152 |
if model_loaded:
|
153 |
+
return openlrm_model
|
154 |
|
155 |
if model_loading:
|
156 |
# Wait for model to load if it's already in progress
|
157 |
while model_loading and not model_loaded:
|
158 |
time.sleep(0.5)
|
159 |
+
return openlrm_model
|
160 |
|
161 |
try:
|
162 |
model_loading = True
|
163 |
+
print("Loading OpenLRM model...")
|
164 |
|
165 |
+
# For Hugging Face free tier, use the smaller model
|
166 |
+
model_repo = "zxhezexin/openlrm-mix-small-1.1" # Smallest OpenLRM model that works well
|
167 |
+
model_file = "model.safetensors"
|
168 |
|
169 |
+
# Download OpenLRM model
|
170 |
+
model_path = hf_hub_download(
|
171 |
+
repo_id=model_repo,
|
172 |
+
filename=model_file,
|
173 |
+
cache_dir=CACHE_DIR,
|
174 |
+
resume_download=True
|
175 |
+
)
|
176 |
|
177 |
+
# Download config file
|
178 |
+
config_path = hf_hub_download(
|
179 |
+
repo_id=model_repo,
|
180 |
+
filename="config.json",
|
181 |
+
cache_dir=CACHE_DIR,
|
182 |
+
resume_download=True
|
183 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
+
# Load OpenLRM for inference
|
186 |
+
# Simplified loading for memory efficiency
|
187 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
188 |
|
189 |
+
# Import necessary modules for OpenLRM
|
190 |
+
from transformers import AutoConfig
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
+
# Load configuration
|
193 |
+
config = AutoConfig.from_pretrained(config_path)
|
194 |
+
|
195 |
+
# Initialize a lightweight model class
|
196 |
+
class OpenLRMWrapper:
|
197 |
+
def __init__(self, model_path, config, device):
|
198 |
+
self.model_path = model_path
|
199 |
+
self.config = config
|
200 |
+
self.device = device
|
201 |
+
# Load the model weights lazily when needed
|
202 |
+
self.model = None
|
203 |
+
|
204 |
+
def __call__(self, image):
|
205 |
+
# Only load the full model when it's actually used
|
206 |
+
if self.model is None:
|
207 |
+
# Import necessary modules
|
208 |
+
from transformers import AutoModelForSeq2SeqLM
|
209 |
+
|
210 |
+
# Load model with minimal memory footprint for Hugging Face free tier
|
211 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(
|
212 |
+
self.model_path,
|
213 |
+
config=self.config,
|
214 |
+
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
215 |
+
device_map=self.device
|
216 |
+
)
|
217 |
+
|
218 |
+
# Process image
|
219 |
+
with torch.no_grad():
|
220 |
+
# Convert image to tensor and process
|
221 |
+
image_tensor = self._preprocess_image(image)
|
222 |
+
result = self.model.generate(image_tensor)
|
223 |
+
return self._process_result(result)
|
224 |
+
|
225 |
+
def _preprocess_image(self, image):
|
226 |
+
# Convert PIL image to tensor and normalize
|
227 |
+
from torchvision import transforms
|
228 |
+
transform = transforms.Compose([
|
229 |
+
transforms.Resize((224, 224)),
|
230 |
+
transforms.ToTensor(),
|
231 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
232 |
+
])
|
233 |
+
tensor = transform(image).unsqueeze(0).to(self.device)
|
234 |
+
return tensor
|
235 |
+
|
236 |
+
def _process_result(self, result):
|
237 |
+
# Process the model output to get 3D mesh data
|
238 |
+
# This is a simplified version for illustration
|
239 |
+
# The actual processing depends on the model's output format
|
240 |
+
return result
|
241 |
+
|
242 |
+
# Create a wrapper instance
|
243 |
+
openlrm_model = OpenLRMWrapper(model_path, config, device)
|
244 |
|
245 |
model_loaded = True
|
246 |
+
print(f"OpenLRM model loaded successfully on {device}")
|
247 |
+
return openlrm_model
|
248 |
|
249 |
except Exception as e:
|
250 |
+
print(f"Error loading OpenLRM model: {str(e)}")
|
251 |
print(traceback.format_exc())
|
252 |
+
|
253 |
+
# Fallback to depth estimation model if OpenLRM fails
|
254 |
+
load_depth_model()
|
255 |
+
return None
|
256 |
finally:
|
257 |
model_loading = False
|
258 |
|
259 |
+
# Load depth estimation model as fallback
|
260 |
+
def load_depth_model():
|
261 |
+
global depth_model, feature_extractor, model_loaded, model_loading
|
262 |
+
|
263 |
+
if depth_model is not None and feature_extractor is not None:
|
264 |
+
return depth_model, feature_extractor
|
265 |
+
|
266 |
+
try:
|
267 |
+
print("Loading depth estimation model as fallback...")
|
268 |
+
|
269 |
+
# Use DINOv2-small which provides good balance between quality and memory usage
|
270 |
+
model_name = "LiheYoung/depth-anything-small"
|
271 |
+
|
272 |
+
# Initialize model with appropriate precision
|
273 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
274 |
+
|
275 |
+
# Load feature extractor and model
|
276 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
277 |
+
depth_model = AutoModelForDepthEstimation.from_pretrained(model_name)
|
278 |
+
|
279 |
+
if device == "cuda":
|
280 |
+
depth_model = depth_model.to(device)
|
281 |
+
|
282 |
+
print(f"Depth model loaded successfully on {device}")
|
283 |
+
return depth_model, feature_extractor
|
284 |
+
|
285 |
+
except Exception as e:
|
286 |
+
print(f"Error loading depth model: {str(e)}")
|
287 |
+
print(traceback.format_exc())
|
288 |
+
raise
|
289 |
+
|
290 |
+
# Process image to create 3D model using OpenLRM
|
291 |
+
def process_openlrm(image, job_id, detail_level='medium', output_format='obj'):
|
292 |
+
try:
|
293 |
+
# Load OpenLRM model
|
294 |
+
model = load_openlrm_model()
|
295 |
+
if model is None:
|
296 |
+
# Fallback to depth-based approach
|
297 |
+
return process_depth_based(image, job_id, detail_level, output_format)
|
298 |
+
|
299 |
+
# Preprocess image - remove background for better results
|
300 |
+
image_rgba = remove_background(image)
|
301 |
+
|
302 |
+
# Update progress
|
303 |
+
processing_jobs[job_id]['progress'] = 30
|
304 |
+
|
305 |
+
# Process with OpenLRM model to get 3D mesh
|
306 |
+
# This is where the magic happens - OpenLRM will create a full 3D model
|
307 |
+
result = model(image_rgba)
|
308 |
+
|
309 |
+
# Update progress
|
310 |
+
processing_jobs[job_id]['progress'] = 60
|
311 |
+
|
312 |
+
# Convert model result to trimesh format
|
313 |
+
mesh = convert_to_trimesh(result, image)
|
314 |
+
|
315 |
+
# Update progress
|
316 |
+
processing_jobs[job_id]['progress'] = 80
|
317 |
+
|
318 |
+
# Return the created mesh
|
319 |
+
return mesh
|
320 |
+
|
321 |
+
except Exception as e:
|
322 |
+
print(f"Error in OpenLRM processing: {str(e)}")
|
323 |
+
print(traceback.format_exc())
|
324 |
+
# Fallback to depth-based approach if OpenLRM fails
|
325 |
+
return process_depth_based(image, job_id, detail_level, output_format)
|
326 |
+
|
327 |
+
# Convert OpenLRM result to trimesh
|
328 |
+
def convert_to_trimesh(result, image):
|
329 |
+
# This is a placeholder for the actual conversion from OpenLRM output to trimesh
|
330 |
+
# Actual implementation depends on the output format of OpenLRM
|
331 |
+
|
332 |
+
# For now, create a sample mesh with UV mapping
|
333 |
+
# In a real implementation, this would use the actual model output
|
334 |
+
vertices = result.get("vertices", generate_sample_vertices())
|
335 |
+
faces = result.get("faces", generate_sample_faces())
|
336 |
+
|
337 |
+
# Create mesh with texture coordinates
|
338 |
+
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
339 |
+
|
340 |
+
# Add texture from the original image
|
341 |
+
if hasattr(image, 'convert'):
|
342 |
+
img_array = np.array(image.convert("RGBA"))
|
343 |
+
if img_array.shape[2] == 4: # RGBA
|
344 |
+
vertex_colors = sample_texture_from_image(img_array, vertices)
|
345 |
+
mesh.visual.vertex_colors = vertex_colors
|
346 |
+
|
347 |
+
return mesh
|
348 |
+
|
349 |
+
# Sample helper functions for mesh creation
|
350 |
+
def generate_sample_vertices():
|
351 |
+
# Create a cube-like object for testing
|
352 |
+
x = np.linspace(-1, 1, 10)
|
353 |
+
y = np.linspace(-1, 1, 10)
|
354 |
+
z = np.linspace(-1, 1, 10)
|
355 |
+
x_grid, y_grid, z_grid = np.meshgrid(x, y, z)
|
356 |
+
vertices = np.vstack([x_grid.flatten(), y_grid.flatten(), z_grid.flatten()]).T
|
357 |
+
return vertices
|
358 |
+
|
359 |
+
def generate_sample_faces():
|
360 |
+
# Create simple faces connecting vertices
|
361 |
+
faces = []
|
362 |
+
n = 10 # Grid size from generate_sample_vertices
|
363 |
+
for i in range(n-1):
|
364 |
+
for j in range(n-1):
|
365 |
+
for k in range(n-1):
|
366 |
+
idx = i*n*n + j*n + k
|
367 |
+
faces.append([idx, idx+1, idx+n])
|
368 |
+
faces.append([idx+1, idx+n+1, idx+n])
|
369 |
+
return np.array(faces)
|
370 |
+
|
371 |
+
def sample_texture_from_image(image, vertices):
|
372 |
+
# Sample colors from image based on vertex positions
|
373 |
+
h, w = image.shape[:2]
|
374 |
+
colors = np.zeros((len(vertices), 4), dtype=np.uint8)
|
375 |
+
|
376 |
+
# Normalize vertex positions to [0,1] for sampling
|
377 |
+
pos = (vertices[:, :2] + 1) / 2 # Assuming vertices are in [-1,1] range
|
378 |
+
|
379 |
+
# Sample image colors
|
380 |
+
for i, p in enumerate(pos):
|
381 |
+
if 0 <= p[0] <= 1 and 0 <= p[1] <= 1:
|
382 |
+
x = int(p[0] * (w-1))
|
383 |
+
y = int(p[1] * (h-1))
|
384 |
+
colors[i] = image[y, x]
|
385 |
+
else:
|
386 |
+
colors[i] = [200, 200, 200, 255] # Default color
|
387 |
+
|
388 |
+
return colors
|
389 |
+
|
390 |
+
# Process using depth-based approach as fallback
|
391 |
+
def process_depth_based(image, job_id, detail_level='medium', output_format='obj'):
|
392 |
+
try:
|
393 |
+
# Load depth model if not already loaded
|
394 |
+
global depth_model, feature_extractor
|
395 |
+
if depth_model is None or feature_extractor is None:
|
396 |
+
depth_model, feature_extractor = load_depth_model()
|
397 |
+
|
398 |
+
# Update progress
|
399 |
+
processing_jobs[job_id]['progress'] = 30
|
400 |
+
|
401 |
+
# Get depth map
|
402 |
+
with torch.no_grad():
|
403 |
+
# Prepare image for the model
|
404 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
405 |
+
if torch.cuda.is_available():
|
406 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
407 |
+
|
408 |
+
# Forward pass
|
409 |
+
outputs = depth_model(**inputs)
|
410 |
+
predicted_depth = outputs.predicted_depth
|
411 |
+
|
412 |
+
# Normalize and resize depth to original image size
|
413 |
+
depth_map = F.interpolate(
|
414 |
+
predicted_depth.unsqueeze(1),
|
415 |
+
size=(image.height, image.width),
|
416 |
+
mode="bicubic",
|
417 |
+
align_corners=False,
|
418 |
+
).squeeze().cpu().numpy()
|
419 |
+
|
420 |
+
# Update progress
|
421 |
+
processing_jobs[job_id]['progress'] = 60
|
422 |
+
|
423 |
+
# Convert to normalized depth map
|
424 |
+
depth_min = depth_map.min()
|
425 |
+
depth_max = depth_map.max()
|
426 |
+
depth_normalized = (depth_map - depth_min) / (depth_max - depth_min)
|
427 |
+
|
428 |
+
# Create mesh from depth map
|
429 |
+
mesh = depth_to_mesh(depth_normalized, image,
|
430 |
+
resolution=100 if detail_level == 'medium' else
|
431 |
+
150 if detail_level == 'high' else 80,
|
432 |
+
detail_level=detail_level)
|
433 |
+
|
434 |
+
# Update progress
|
435 |
+
processing_jobs[job_id]['progress'] = 80
|
436 |
+
|
437 |
+
return mesh
|
438 |
+
|
439 |
+
except Exception as e:
|
440 |
+
print(f"Error in depth-based processing: {str(e)}")
|
441 |
+
print(traceback.format_exc())
|
442 |
+
raise
|
443 |
+
|
444 |
+
# Enhanced depth map processing
|
445 |
def enhance_depth_map(depth_map, detail_level='medium'):
|
446 |
"""Apply sophisticated processing to enhance depth map details"""
|
447 |
# Convert to numpy array if needed
|
|
|
455 |
# Create a copy for processing
|
456 |
enhanced_depth = depth_map.copy().astype(np.float32)
|
457 |
|
458 |
+
# Remove outliers using percentile clipping
|
459 |
p_low, p_high = np.percentile(enhanced_depth, [1, 99])
|
460 |
enhanced_depth = np.clip(enhanced_depth, p_low, p_high)
|
461 |
|
|
|
464 |
|
465 |
# Apply different enhancement methods based on detail level
|
466 |
if detail_level == 'high':
|
467 |
+
# Apply unsharp masking for edge enhancement
|
|
|
468 |
blurred = gaussian_filter(enhanced_depth, sigma=1.5)
|
|
|
469 |
mask = enhanced_depth - blurred
|
|
|
470 |
enhanced_depth = enhanced_depth + 1.5 * mask
|
471 |
|
472 |
+
# Apply bilateral filter simulation
|
|
|
473 |
smooth1 = gaussian_filter(enhanced_depth, sigma=0.5)
|
474 |
smooth2 = gaussian_filter(enhanced_depth, sigma=2.0)
|
475 |
edge_mask = enhanced_depth - smooth2
|
476 |
enhanced_depth = smooth1 + 1.2 * edge_mask
|
477 |
|
478 |
elif detail_level == 'medium':
|
479 |
+
# Less aggressive enhancement
|
|
|
480 |
blurred = gaussian_filter(enhanced_depth, sigma=1.0)
|
481 |
mask = enhanced_depth - blurred
|
482 |
enhanced_depth = enhanced_depth + 0.8 * mask
|
|
|
|
|
483 |
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.5)
|
484 |
|
485 |
else: # low
|
486 |
+
# Just apply noise reduction
|
487 |
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.7)
|
488 |
|
489 |
# Normalize again after processing
|
|
|
491 |
|
492 |
return enhanced_depth
|
493 |
|
494 |
+
# Improved depth to mesh conversion with better detail
|
495 |
def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
496 |
+
"""Convert depth map to 3D mesh with improved detail preservation"""
|
497 |
# First, enhance the depth map for better details
|
498 |
enhanced_depth = enhance_depth_map(depth_map, detail_level)
|
499 |
|
|
|
505 |
y = np.linspace(0, h-1, resolution)
|
506 |
x_grid, y_grid = np.meshgrid(x, y)
|
507 |
|
508 |
+
# Sample depth at grid points
|
509 |
+
from scipy import interpolate
|
510 |
interp_func = interpolate.RectBivariateSpline(
|
511 |
np.arange(h), np.arange(w), enhanced_depth, kx=3, ky=3
|
512 |
)
|
|
|
|
|
513 |
z_values = interp_func(y, x, grid=True)
|
514 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
515 |
# Apply depth scaling appropriate to the detail level
|
516 |
if detail_level == 'high':
|
517 |
+
z_scaling = 2.5 # More pronounced depth
|
518 |
elif detail_level == 'medium':
|
519 |
z_scaling = 2.0 # Standard depth
|
520 |
else:
|
521 |
+
z_scaling = 1.5 # Subtle depth
|
522 |
|
523 |
z_values = z_values * z_scaling
|
524 |
|
525 |
+
# Normalize coordinates
|
526 |
x_grid = (x_grid / w - 0.5) * 2.0 # Map to -1 to 1
|
527 |
y_grid = (y_grid / h - 0.5) * 2.0 # Map to -1 to 1
|
528 |
|
529 |
# Create vertices
|
530 |
vertices = np.vstack([x_grid.flatten(), -y_grid.flatten(), -z_values.flatten()]).T
|
531 |
|
532 |
+
# Create faces (triangles)
|
533 |
faces = []
|
534 |
for i in range(resolution-1):
|
535 |
for j in range(resolution-1):
|
|
|
538 |
p3 = (i + 1) * resolution + j
|
539 |
p4 = (i + 1) * resolution + (j + 1)
|
540 |
|
541 |
+
# Standard triangulation
|
542 |
+
faces.append([p1, p2, p4])
|
543 |
+
faces.append([p1, p4, p3])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
544 |
|
545 |
faces = np.array(faces)
|
546 |
|
547 |
# Create mesh
|
548 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
549 |
|
550 |
+
# Apply texturing if image is provided
|
551 |
if image:
|
552 |
# Convert to numpy array if needed
|
553 |
if isinstance(image, Image.Image):
|
|
|
555 |
else:
|
556 |
img_array = image
|
557 |
|
558 |
+
# Create vertex colors
|
559 |
if resolution <= img_array.shape[0] and resolution <= img_array.shape[1]:
|
560 |
+
# Create vertex colors by sampling the image
|
561 |
vertex_colors = np.zeros((vertices.shape[0], 4), dtype=np.uint8)
|
562 |
|
|
|
563 |
for i in range(resolution):
|
564 |
for j in range(resolution):
|
565 |
+
# Calculate image coordinates
|
566 |
img_x = j * (img_array.shape[1] - 1) / (resolution - 1)
|
567 |
img_y = i * (img_array.shape[0] - 1) / (resolution - 1)
|
568 |
|
569 |
+
# Bilinear interpolation
|
570 |
x0, y0 = int(img_x), int(img_y)
|
571 |
x1, y1 = min(x0 + 1, img_array.shape[1] - 1), min(y0 + 1, img_array.shape[0] - 1)
|
572 |
|
573 |
+
# Interpolation weights
|
574 |
wx = img_x - x0
|
575 |
wy = img_y - y0
|
576 |
|
577 |
vertex_idx = i * resolution + j
|
578 |
|
579 |
if len(img_array.shape) == 3 and img_array.shape[2] == 3: # RGB
|
580 |
+
# Perform bilinear interpolation
|
581 |
r = int((1-wx)*(1-wy)*img_array[y0, x0, 0] + wx*(1-wy)*img_array[y0, x1, 0] +
|
582 |
(1-wx)*wy*img_array[y1, x0, 0] + wx*wy*img_array[y1, x1, 0])
|
583 |
g = int((1-wx)*(1-wy)*img_array[y0, x0, 1] + wx*(1-wy)*img_array[y0, x1, 1] +
|
|
|
594 |
(1-wx)*wy*img_array[y1, x0, c] +
|
595 |
wx*wy*img_array[y1, x1, c])
|
596 |
else:
|
597 |
+
# Handle grayscale
|
598 |
gray = int((1-wx)*(1-wy)*img_array[y0, x0] + wx*(1-wy)*img_array[y0, x1] +
|
599 |
+
(1-wx)*wy*img_array[y1, x0] + wx*wy*img_array[y1, x1])
|
600 |
vertex_colors[vertex_idx, :3] = [gray, gray, gray]
|
601 |
vertex_colors[vertex_idx, 3] = 255
|
602 |
|
|
|
604 |
|
605 |
# Apply smoothing to get rid of staircase artifacts
|
606 |
if detail_level != 'high':
|
|
|
|
|
607 |
mesh = mesh.smoothed(method='laplacian', iterations=1)
|
608 |
|
609 |
+
# Fix normals for better rendering
|
610 |
mesh.fix_normals()
|
611 |
|
612 |
+
# Simulate full 3D by duplicating and flipping the mesh
|
613 |
+
if detail_level != 'low':
|
614 |
+
# Create a complete 3D object by duplicating and flipping the mesh
|
615 |
+
back_mesh = mesh.copy()
|
616 |
+
# Flip to create the back side
|
617 |
+
back_mesh.vertices[:, 2] = -back_mesh.vertices[:, 2]
|
618 |
+
# Fix normals after flipping
|
619 |
+
back_mesh.fix_normals()
|
620 |
+
|
621 |
+
# Combine front and back meshes
|
622 |
+
combined_mesh = trimesh.util.concatenate([mesh, back_mesh])
|
623 |
+
|
624 |
+
# Add side panels to create a watertight model
|
625 |
+
combined_mesh = create_watertight_model(combined_mesh)
|
626 |
+
|
627 |
+
return combined_mesh
|
628 |
+
|
629 |
+
return mesh
|
630 |
+
|
631 |
+
# Create a watertight model by adding side panels
|
632 |
+
def create_watertight_model(mesh):
|
633 |
+
# Extract boundary edges
|
634 |
+
edges = mesh.edges_unique
|
635 |
+
edge_faces = mesh.edges_unique_inverse
|
636 |
+
boundary_edges = edges[np.where(np.bincount(edge_faces) == 1)[0]]
|
637 |
+
|
638 |
+
# If no boundary edges, return the original mesh
|
639 |
+
if len(boundary_edges) == 0:
|
640 |
+
return mesh
|
641 |
+
|
642 |
+
# Create side panels along boundary edges
|
643 |
+
new_faces = []
|
644 |
+
|
645 |
+
# Sort boundary edges to form loops
|
646 |
+
edge_loops = []
|
647 |
+
current_loop = [boundary_edges[0][0], boundary_edges[0][1]]
|
648 |
+
boundary_edges = boundary_edges[1:]
|
649 |
+
|
650 |
+
# Try to create continuous edge loops
|
651 |
+
while len(boundary_edges) > 0:
|
652 |
+
found = False
|
653 |
+
for i, edge in enumerate(boundary_edges):
|
654 |
+
if edge[0] == current_loop[-1]:
|
655 |
+
current_loop.append(edge[1])
|
656 |
+
boundary_edges = np.delete(boundary_edges, i, axis=0)
|
657 |
+
found = True
|
658 |
+
break
|
659 |
+
elif edge[1] == current_loop[-1]:
|
660 |
+
current_loop.append(edge[0])
|
661 |
+
boundary_edges = np.delete(boundary_edges, i, axis=0)
|
662 |
+
found = True
|
663 |
+
break
|
664 |
+
|
665 |
+
if not found:
|
666 |
+
# Start a new loop
|
667 |
+
edge_loops.append(current_loop)
|
668 |
+
if len(boundary_edges) > 0:
|
669 |
+
current_loop = [boundary_edges[0][0], boundary_edges[0][1]]
|
670 |
+
boundary_edges = boundary_edges[1:]
|
671 |
+
else:
|
672 |
+
break
|
673 |
+
|
674 |
+
if len(current_loop) > 0:
|
675 |
+
edge_loops.append(current_loop)
|
676 |
+
|
677 |
+
# Create faces for each loop
|
678 |
+
for loop in edge_loops:
|
679 |
+
if len(loop) < 3:
|
680 |
+
continue
|
681 |
+
|
682 |
+
# Create triangles by triangulating the loop
|
683 |
+
for i in range(1, len(loop) - 1):
|
684 |
+
new_faces.append([loop[0], loop[i], loop[i+1]])
|
685 |
+
|
686 |
+
# Add new faces to the mesh
|
687 |
+
if len(new_faces) > 0:
|
688 |
+
new_faces = np.array(new_faces)
|
689 |
+
combined_faces = np.vstack([mesh.faces, new_faces])
|
690 |
+
watertight_mesh = trimesh.Trimesh(vertices=mesh.vertices, faces=combined_faces)
|
691 |
+
|
692 |
+
# Copy vertex colors if they exist
|
693 |
+
if hasattr(mesh.visual, 'vertex_colors') and mesh.visual.vertex_colors is not None:
|
694 |
+
watertight_mesh.visual.vertex_colors = mesh.visual.vertex_colors
|
695 |
+
|
696 |
+
return watertight_mesh
|
697 |
+
|
698 |
return mesh
|
699 |
|
700 |
@app.route('/health', methods=['GET'])
|
701 |
def health_check():
|
702 |
return jsonify({
|
703 |
"status": "healthy",
|
704 |
+
"model": "Enhanced 3D Model Generator with OpenLRM and Depth-Anything",
|
705 |
"device": "cuda" if torch.cuda.is_available() else "cpu"
|
706 |
}), 200
|
707 |
|
|
|
728 |
time.sleep(0.5)
|
729 |
check_count += 1
|
730 |
|
731 |
+
# Check if job is still running
|
732 |
if check_count > 60: # 30 seconds with no updates
|
733 |
if 'thread_alive' in job and not job['thread_alive']():
|
734 |
job['status'] = 'error'
|
735 |
job['error'] = 'Processing thread died unexpectedly'
|
736 |
break
|
737 |
check_count = 0
|
738 |
+
|
739 |
+
# Send final status
|
740 |
if job['status'] == 'completed':
|
741 |
yield f"data: {json.dumps({'status': 'completed', 'progress': 100, 'result_url': job['result_url'], 'preview_url': job['preview_url']})}\n\n"
|
742 |
else:
|
|
|
762 |
mesh_resolution = min(int(request.form.get('mesh_resolution', 100)), 200) # Limit max resolution
|
763 |
output_format = request.form.get('output_format', 'obj').lower()
|
764 |
detail_level = request.form.get('detail_level', 'medium').lower() # Parameter for detail level
|
765 |
+
model_type = request.form.get('model_type', 'openlrm').lower() # 'openlrm' or 'depth'
|
766 |
except ValueError:
|
767 |
return jsonify({"error": "Invalid parameter values"}), 400
|
768 |
|
|
|
770 |
if output_format not in ['obj', 'glb']:
|
771 |
return jsonify({"error": "Unsupported output format. Use 'obj' or 'glb'"}), 400
|
772 |
|
|
|
|
|
|
|
|
|
|
|
|
|
773 |
# Create a job ID
|
774 |
job_id = str(uuid.uuid4())
|
775 |
output_dir = os.path.join(RESULTS_FOLDER, job_id)
|
|
|
802 |
image = preprocess_image(filepath)
|
803 |
processing_jobs[job_id]['progress'] = 10
|
804 |
|
805 |
+
# Process image based on selected model type
|
806 |
+
if model_type == 'depth' or model_type == 'depth-based':
|
807 |
+
# Use depth-based approach
|
808 |
+
mesh = process_depth_based(image, job_id, detail_level, output_format)
|
809 |
+
else:
|
810 |
+
# Default to OpenLRM approach
|
811 |
+
mesh = process_openlrm(image, job_id, detail_level, output_format)
|
|
|
812 |
|
813 |
+
processing_jobs[job_id]['progress'] = 80
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
814 |
|
815 |
+
# Export based on requested format
|
816 |
try:
|
817 |
if output_format == 'obj':
|
818 |
obj_path = os.path.join(output_dir, "model.obj")
|
|
|
842 |
processing_jobs[job_id]['preview_url'] = f"/preview/{job_id}"
|
843 |
|
844 |
elif output_format == 'glb':
|
845 |
+
# Export as GLB
|
846 |
glb_path = os.path.join(output_dir, "model.glb")
|
847 |
mesh.export(
|
848 |
glb_path,
|
|
|
855 |
# Update job status
|
856 |
processing_jobs[job_id]['status'] = 'completed'
|
857 |
processing_jobs[job_id]['progress'] = 100
|
858 |
+
processing_jobs[job_id]['completed_at'] = time.time()
|
859 |
print(f"Job {job_id} completed successfully")
|
860 |
except Exception as e:
|
861 |
error_details = traceback.format_exc()
|
|
|
965 |
# Schedule the next cleanup
|
966 |
threading.Timer(300, cleanup_old_jobs).start() # Run every 5 minutes
|
967 |
|
968 |
+
# Get detailed information about a model
|
969 |
@app.route('/model-info/<job_id>', methods=['GET'])
|
970 |
def model_info(job_id):
|
971 |
if job_id not in processing_jobs:
|
|
|
1014 |
@app.route('/', methods=['GET'])
|
1015 |
def index():
|
1016 |
return jsonify({
|
1017 |
+
"message": "Enhanced 3D Model Generator with OpenLRM",
|
1018 |
"endpoints": [
|
1019 |
"/convert",
|
1020 |
"/progress/<job_id>",
|
|
|
1026 |
"mesh_resolution": "Integer (50-200), controls mesh density",
|
1027 |
"output_format": "obj or glb",
|
1028 |
"detail_level": "low, medium, or high - controls the level of detail in the final model",
|
1029 |
+
"model_type": "openlrm (default, full 3D) or depth (faster but simpler)"
|
1030 |
},
|
1031 |
+
"description": "This API creates high-quality 3D models from 2D images with full 3D structure and texturing"
|
1032 |
}), 200
|
1033 |
|
1034 |
+
# Example endpoint showing model comparison
|
1035 |
+
@app.route('/model-comparison', methods=['POST'])
|
1036 |
+
def compare_models():
|
1037 |
# Check if image is in the request
|
1038 |
if 'image' not in request.files:
|
1039 |
return jsonify({"error": "No image provided"}), 400
|
|
|
1059 |
processing_jobs[job_id] = {
|
1060 |
'status': 'processing',
|
1061 |
'progress': 0,
|
1062 |
+
'result_urls': {},
|
|
|
1063 |
'error': None,
|
|
|
1064 |
'created_at': time.time(),
|
1065 |
'comparison': True
|
1066 |
}
|
1067 |
|
1068 |
+
# Process in separate thread to create models with both approaches
|
1069 |
def process_comparison():
|
1070 |
thread = threading.current_thread()
|
1071 |
processing_jobs[job_id]['thread_alive'] = lambda: thread.is_alive()
|
|
|
1075 |
image = preprocess_image(filepath)
|
1076 |
processing_jobs[job_id]['progress'] = 10
|
1077 |
|
1078 |
+
# Dictionary to store results
|
1079 |
+
result_urls = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
1080 |
|
1081 |
+
# Process with both models
|
1082 |
try:
|
1083 |
+
# First try with OpenLRM for full 3D
|
1084 |
+
processing_jobs[job_id]['progress'] = 30
|
1085 |
+
openlrm_mesh = process_openlrm(image, job_id, 'medium', 'glb')
|
1086 |
+
|
1087 |
+
# Export OpenLRM result
|
1088 |
+
openlrm_path = os.path.join(output_dir, "model_openlrm.glb")
|
1089 |
+
openlrm_mesh.export(openlrm_path, file_type='glb')
|
1090 |
+
result_urls['openlrm'] = f"/compare-download/{job_id}/openlrm"
|
1091 |
+
|
1092 |
+
processing_jobs[job_id]['progress'] = 60
|
1093 |
+
|
1094 |
+
# Then process with depth-based approach
|
1095 |
+
depth_mesh = process_depth_based(image, job_id, 'medium', 'glb')
|
1096 |
+
|
1097 |
+
# Export depth-based result
|
1098 |
+
depth_path = os.path.join(output_dir, "model_depth.glb")
|
1099 |
+
depth_mesh.export(depth_path, file_type='glb')
|
1100 |
+
result_urls['depth'] = f"/compare-download/{job_id}/depth"
|
1101 |
+
|
1102 |
+
processing_jobs[job_id]['progress'] = 90
|
1103 |
|
|
|
1104 |
except Exception as e:
|
1105 |
+
print(f"Error in comparison processing: {str(e)}")
|
1106 |
+
# If at least one model was successful, continue
|
1107 |
+
if not result_urls:
|
1108 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1109 |
|
1110 |
# Update job status
|
1111 |
processing_jobs[job_id]['status'] = 'completed'
|
|
|
1139 |
# Return job ID immediately
|
1140 |
return jsonify({"job_id": job_id, "check_progress_at": f"/progress/{job_id}"}), 202
|
1141 |
|
1142 |
+
@app.route('/compare-download/<job_id>/<model_type>', methods=['GET'])
|
1143 |
+
def download_comparison_model(job_id, model_type):
|
1144 |
if job_id not in processing_jobs or processing_jobs[job_id]['status'] != 'completed':
|
1145 |
return jsonify({"error": "Model not found or processing not complete"}), 404
|
1146 |
|
1147 |
if 'comparison' not in processing_jobs[job_id] or not processing_jobs[job_id]['comparison']:
|
1148 |
return jsonify({"error": "This is not a comparison job"}), 400
|
1149 |
|
1150 |
+
if model_type not in ['openlrm', 'depth']:
|
1151 |
+
return jsonify({"error": "Invalid model type"}), 400
|
1152 |
|
1153 |
# Get the output directory for this job
|
1154 |
output_dir = os.path.join(RESULTS_FOLDER, job_id)
|
1155 |
+
model_path = os.path.join(output_dir, f"model_{model_type}.glb")
|
1156 |
|
1157 |
if os.path.exists(model_path):
|
1158 |
+
return send_file(model_path, as_attachment=True, download_name=f"model_{model_type}.glb")
|
1159 |
|
1160 |
return jsonify({"error": "File not found"}), 404
|
1161 |
|
1162 |
+
@app.route('/install-dependencies', methods=['POST'])
|
1163 |
+
def install_dependencies():
|
1164 |
+
"""Admin route to install additional dependencies if needed"""
|
1165 |
+
try:
|
1166 |
+
# Check for admin token (you should implement proper authentication)
|
1167 |
+
token = request.json.get('token')
|
1168 |
+
if token != 'admin_secure_token': # Replace with proper auth
|
1169 |
+
return jsonify({"error": "Unauthorized"}), 401
|
1170 |
+
|
1171 |
+
# Install dependencies
|
1172 |
+
import subprocess
|
1173 |
+
|
1174 |
+
# Install rembg for background removal
|
1175 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "rembg"])
|
1176 |
+
|
1177 |
+
# Try to install torchmcubes with CUDA support
|
1178 |
+
try:
|
1179 |
+
subprocess.check_call([sys.executable, "-m", "pip", "uninstall", "-y", "torchmcubes"])
|
1180 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "git+https://github.com/tatsy/torchmcubes.git"])
|
1181 |
+
except:
|
1182 |
+
print("Could not install torchmcubes with CUDA support")
|
1183 |
+
|
1184 |
+
return jsonify({"message": "Dependencies installed successfully"}), 200
|
1185 |
+
except Exception as e:
|
1186 |
+
return jsonify({"error": f"Failed to install dependencies: {str(e)}"}), 500
|
1187 |
+
|
1188 |
if __name__ == '__main__':
|
1189 |
# Start the cleanup thread
|
1190 |
cleanup_old_jobs()
|
1191 |
|
1192 |
# Use port 7860 which is standard for Hugging Face Spaces
|
1193 |
port = int(os.environ.get('PORT', 7860))
|
1194 |
+
app.run(host='0.0.0.0', port=port)
|