Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -139,10 +139,11 @@ def preprocess_image(image_path):
|
|
139 |
new_width = int(img.width * (MAX_DIMENSION / img.height))
|
140 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
141 |
|
142 |
-
# Remove background
|
143 |
-
|
|
|
144 |
|
145 |
-
img_array = np.array(
|
146 |
if len(img_array.shape) == 3 and img_array.shape[2] == 3:
|
147 |
lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2LAB)
|
148 |
l, a, b = cv2.split(lab)
|
@@ -150,9 +151,9 @@ def preprocess_image(image_path):
|
|
150 |
cl = clahe.apply(l)
|
151 |
enhanced_lab = cv2.merge((cl, a, b))
|
152 |
img_array = cv2.cvtColor(enhanced_lab, cv2.COLOR_LAB2RGB)
|
153 |
-
|
154 |
|
155 |
-
return
|
156 |
|
157 |
def load_models():
|
158 |
global dpt_estimator, depth_anything_model, depth_anything_processor, model_loaded, model_loading
|
@@ -293,7 +294,7 @@ def enhance_depth_map(depth_map, detail_level='medium'):
|
|
293 |
smooth1 = gaussian_filter(enhanced_depth, sigma=0.3)
|
294 |
smooth2 = gaussian_filter(enhanced_depth, sigma=1.5)
|
295 |
edge_mask = enhanced_depth - smooth2
|
296 |
-
enhanced_depth = smooth1 + 0.8 * edge_mask
|
297 |
elif detail_level == 'medium':
|
298 |
blurred = gaussian_filter(enhanced_depth, sigma=0.7)
|
299 |
mask = enhanced_depth - blurred
|
@@ -357,7 +358,7 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
357 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
358 |
|
359 |
if image:
|
360 |
-
img_array = np.array(image)
|
361 |
vertex_colors = np.zeros((vertices.shape[0], 4), dtype=np.uint8)
|
362 |
for i in range(resolution):
|
363 |
for j in range(resolution):
|
@@ -368,15 +369,21 @@ def depth_to_mesh(depth_map, image, resolution=100, detail_level='medium'):
|
|
368 |
wx = img_x - x0
|
369 |
wy = img_y - y0
|
370 |
vertex_idx = i * resolution + j
|
371 |
-
if len(img_array.shape) == 3 and img_array.shape[2] ==
|
372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
373 |
vertex_colors[vertex_idx, c] = int((1-wx)*(1-wy)*img_array[y0, x0, c] +
|
374 |
wx*(1-wy)*img_array[y0, x1, c] +
|
375 |
(1-wx)*wy*img_array[y1, x0, c] +
|
376 |
wx*wy*img_array[y1, x1, c])
|
377 |
-
else:
|
378 |
-
r, g, b = img_array[y0, x0]
|
379 |
-
vertex_colors[vertex_idx, :3] = [r, g, b]
|
380 |
vertex_colors[vertex_idx, 3] = 255
|
381 |
mesh.visual.vertex_colors = vertex_colors
|
382 |
|
|
|
139 |
new_width = int(img.width * (MAX_DIMENSION / img.height))
|
140 |
img = img.resize((new_width, new_height), Image.LANCZOS)
|
141 |
|
142 |
+
# Remove background and convert back to RGB for processor
|
143 |
+
img_with_alpha = remove_background(img)
|
144 |
+
img_rgb = img_with_alpha.convert("RGB") # Convert to RGB for processor
|
145 |
|
146 |
+
img_array = np.array(img_rgb)
|
147 |
if len(img_array.shape) == 3 and img_array.shape[2] == 3:
|
148 |
lab = cv2.cvtColor(img_array, cv2.COLOR_RGB2LAB)
|
149 |
l, a, b = cv2.split(lab)
|
|
|
151 |
cl = clahe.apply(l)
|
152 |
enhanced_lab = cv2.merge((cl, a, b))
|
153 |
img_array = cv2.cvtColor(enhanced_lab, cv2.COLOR_LAB2RGB)
|
154 |
+
img_rgb = Image.fromarray(img_array)
|
155 |
|
156 |
+
return img_rgb # Return RGB image
|
157 |
|
158 |
def load_models():
|
159 |
global dpt_estimator, depth_anything_model, depth_anything_processor, model_loaded, model_loading
|
|
|
294 |
smooth1 = gaussian_filter(enhanced_depth, sigma=0.3)
|
295 |
smooth2 = gaussian_filter(enhanced_depth, sigma=1.5)
|
296 |
edge_mask = enhanced_depth - smooth2
|
297 |
+
enhanced_depth = smooth1 + 0.8 * edge_mask
|
298 |
elif detail_level == 'medium':
|
299 |
blurred = gaussian_filter(enhanced_depth, sigma=0.7)
|
300 |
mask = enhanced_depth - blurred
|
|
|
358 |
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
359 |
|
360 |
if image:
|
361 |
+
img_array = np.array(image.convert("RGB")) # Ensure RGB for consistency
|
362 |
vertex_colors = np.zeros((vertices.shape[0], 4), dtype=np.uint8)
|
363 |
for i in range(resolution):
|
364 |
for j in range(resolution):
|
|
|
369 |
wx = img_x - x0
|
370 |
wy = img_y - y0
|
371 |
vertex_idx = i * resolution + j
|
372 |
+
if len(img_array.shape) == 3 and img_array.shape[2] == 3:
|
373 |
+
r = int((1-wx)*(1-wy)*img_array[y0, x0, 0] + wx*(1-wy)*img_array[y0, x1, 0] +
|
374 |
+
(1-wx)*wy*img_array[y1, x0, 0] + wx*wy*img_array[y1, x1, 0])
|
375 |
+
g = int((1-wx)*(1-wy)*img_array[y0, x0, 1] + wx*(1-wy)*img_array[y0, x1, 1] +
|
376 |
+
(1-wx)*wy*img_array[y1, x0, 1] + wx*wy*img_array[y1, x1, 1])
|
377 |
+
b = int((1-wx)*(1-wy)*img_array[y0, x0, 2] + wx*(1-wy)*img_array[y0, x1, 2] +
|
378 |
+
(1-wx)*wy*img_array[y1, x0, 2] + wx*wy*img_array[y1, x1, 2])
|
379 |
+
vertex_colors[vertex_idx, :3] = [r, g, b]
|
380 |
+
vertex_colors[vertex_idx, 3] = 255
|
381 |
+
elif len(img_array.shape) == 3 and img_array.shape[2] == 4:
|
382 |
+
for c in range(3): # Use only RGB channels
|
383 |
vertex_colors[vertex_idx, c] = int((1-wx)*(1-wy)*img_array[y0, x0, c] +
|
384 |
wx*(1-wy)*img_array[y0, x1, c] +
|
385 |
(1-wx)*wy*img_array[y1, x0, c] +
|
386 |
wx*wy*img_array[y1, x1, c])
|
|
|
|
|
|
|
387 |
vertex_colors[vertex_idx, 3] = 255
|
388 |
mesh.visual.vertex_colors = vertex_colors
|
389 |
|