Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ import io
|
|
11 |
import zipfile
|
12 |
import uuid
|
13 |
import traceback
|
14 |
-
from huggingface_hub import snapshot_download, login
|
15 |
from flask_cors import CORS
|
16 |
import numpy as np
|
17 |
import trimesh
|
@@ -35,6 +35,8 @@ os.makedirs(RESULTS_FOLDER, exist_ok=True)
|
|
35 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
36 |
|
37 |
os.environ['HF_HOME'] = CACHE_DIR
|
|
|
|
|
38 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
39 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
40 |
|
@@ -92,7 +94,7 @@ def remove_background(image_path):
|
|
92 |
result = remove(img_data)
|
93 |
img = Image.open(io.BytesIO(result)).convert("RGBA")
|
94 |
|
95 |
-
# Check if image is fully transparent
|
96 |
img_array = np.array(img)
|
97 |
if np.all(img_array[:, :, 3] == 0):
|
98 |
print(f"Warning: Image {image_path} is fully transparent or no object detected")
|
@@ -107,7 +109,6 @@ def remove_background(image_path):
|
|
107 |
raise
|
108 |
|
109 |
def preprocess_image(image_path):
|
110 |
-
# Remove background and add black background
|
111 |
img = remove_background(image_path)
|
112 |
if img is None:
|
113 |
raise ValueError("Image is fully transparent or no object detected")
|
@@ -157,26 +158,32 @@ def load_models():
|
|
157 |
print("Warning: HF_TOKEN not found in environment")
|
158 |
|
159 |
dpt_model_name = "Intel/dpt-large"
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
print(f"
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
dpt_estimator = pipeline(
|
182 |
"depth-estimation",
|
@@ -188,29 +195,34 @@ def load_models():
|
|
188 |
print("DPT-Large loaded")
|
189 |
gc.collect()
|
190 |
|
191 |
-
da_model_name = "depth-anything
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
depth_anything_processor = AutoImageProcessor.from_pretrained(
|
216 |
da_model_name,
|
@@ -288,8 +300,8 @@ def enhance_depth_map(depth_map, detail_level='medium'):
|
|
288 |
else:
|
289 |
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.7)
|
290 |
|
291 |
-
|
292 |
-
return
|
293 |
|
294 |
def depth_to_mesh(depth_map, image, resolution=80, detail_level='medium', view_angle=0):
|
295 |
enhanced_depth = enhance_depth_map(depth_map, detail_level)
|
@@ -319,7 +331,6 @@ def depth_to_mesh(depth_map, image, resolution=80, detail_level='medium', view_a
|
|
319 |
y_grid = (y_grid / h - 0.5) * 2.0
|
320 |
vertices = np.vstack([x_grid.flatten(), -y_grid.flatten(), -z_values.flatten()]).T
|
321 |
|
322 |
-
# Rotate vertices based on view angle (in radians)
|
323 |
if view_angle != 0:
|
324 |
rotation_matrix = trimesh.transformations.rotation_matrix(view_angle, [0, 1, 0])
|
325 |
vertices = trimesh.transform_points(vertices, rotation_matrix)
|
@@ -398,11 +409,8 @@ def combine_meshes(meshes):
|
|
398 |
|
399 |
combined_mesh = trimesh.Trimesh(vertices=combined_vertices, faces=combined_faces)
|
400 |
|
401 |
-
# Stitch overlapping vertices
|
402 |
combined_mesh = combined_mesh.subdivide_to_size(max_edge=0.05)
|
403 |
combined_mesh = combined_mesh.smoothed(method='laplacian', iterations=2)
|
404 |
-
|
405 |
-
# Ensure watertight mesh
|
406 |
combined_mesh.fill_holes()
|
407 |
combined_mesh.fix_normals()
|
408 |
|
@@ -532,11 +540,9 @@ def convert_image_to_3d():
|
|
532 |
view_angles = {'front': 0, 'back': np.pi, 'left': np.pi/2, 'right': -np.pi/2}
|
533 |
with torch.no_grad():
|
534 |
for view, image in images.items():
|
535 |
-
# DPT-Large
|
536 |
dpt_result = dpt_model(image)
|
537 |
dpt_depth = dpt_result["depth"]
|
538 |
|
539 |
-
# Depth Anything (if loaded)
|
540 |
if da_model and da_processor:
|
541 |
inputs = da_processor(images=image, return_tensors="pt")
|
542 |
inputs = {k: v.to("cpu") for k, v in inputs.items()}
|
@@ -760,7 +766,7 @@ def index():
|
|
760 |
"detail_level": "low, medium, or high",
|
761 |
"texture_quality": "low, medium, or high"
|
762 |
},
|
763 |
-
"description": "Creates high-quality 3D models from multiple 2D images
|
764 |
}), 200
|
765 |
|
766 |
if __name__ == '__main__':
|
|
|
11 |
import zipfile
|
12 |
import uuid
|
13 |
import traceback
|
14 |
+
from huggingface_hub import snapshot_download, login, HfFileSystem
|
15 |
from flask_cors import CORS
|
16 |
import numpy as np
|
17 |
import trimesh
|
|
|
35 |
os.makedirs(CACHE_DIR, exist_ok=True)
|
36 |
|
37 |
os.environ['HF_HOME'] = CACHE_DIR
|
38 |
+
os.environ['TRANSFORMERS_CACHE'] = os.path.join(CACHE_DIR, 'transformers')
|
39 |
+
os.environ['HF_DATASETS_CACHE'] = os.path.join(CACHE_DIR, 'datasets')
|
40 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
41 |
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
|
42 |
|
|
|
94 |
result = remove(img_data)
|
95 |
img = Image.open(io.BytesIO(result)).convert("RGBA")
|
96 |
|
97 |
+
# Check if image is fully transparent
|
98 |
img_array = np.array(img)
|
99 |
if np.all(img_array[:, :, 3] == 0):
|
100 |
print(f"Warning: Image {image_path} is fully transparent or no object detected")
|
|
|
109 |
raise
|
110 |
|
111 |
def preprocess_image(image_path):
|
|
|
112 |
img = remove_background(image_path)
|
113 |
if img is None:
|
114 |
raise ValueError("Image is fully transparent or no object detected")
|
|
|
158 |
print("Warning: HF_TOKEN not found in environment")
|
159 |
|
160 |
dpt_model_name = "Intel/dpt-large"
|
161 |
+
fs = HfFileSystem(token=hf_token)
|
162 |
+
model_cached = os.path.exists(os.path.join(CACHE_DIR, "hub", "models--Intel--dpt-large"))
|
163 |
+
|
164 |
+
if not model_cached:
|
165 |
+
max_retries = 3
|
166 |
+
retry_delay = 5
|
167 |
+
for attempt in range(max_retries):
|
168 |
+
try:
|
169 |
+
print(f"Attempting to download {dpt_model_name}, attempt {attempt+1}")
|
170 |
+
snapshot_download(
|
171 |
+
repo_id=dpt_model_name,
|
172 |
+
cache_dir=CACHE_DIR,
|
173 |
+
resume_download=True,
|
174 |
+
token=hf_token
|
175 |
+
)
|
176 |
+
print(f"Successfully downloaded {dpt_model_name}")
|
177 |
+
break
|
178 |
+
except Exception as e:
|
179 |
+
if attempt < max_retries - 1:
|
180 |
+
print(f"DPT download attempt {attempt+1} failed: {str(e)}. Retrying after {retry_delay}s...")
|
181 |
+
time.sleep(retry_delay)
|
182 |
+
retry_delay *= 2
|
183 |
+
else:
|
184 |
+
raise
|
185 |
+
else:
|
186 |
+
print(f"{dpt_model_name} already cached in {CACHE_DIR}")
|
187 |
|
188 |
dpt_estimator = pipeline(
|
189 |
"depth-estimation",
|
|
|
195 |
print("DPT-Large loaded")
|
196 |
gc.collect()
|
197 |
|
198 |
+
da_model_name = "LiheYoung/depth-anything-v2-small"
|
199 |
+
da_model_cached = os.path.exists(os.path.join(CACHE_DIR, "hub", "models--LiheYoung--depth-anything-v2-small"))
|
200 |
+
|
201 |
+
if not da_model_cached:
|
202 |
+
for attempt in range(max_retries):
|
203 |
+
try:
|
204 |
+
print(f"Attempting to download {da_model_name}, attempt {attempt+1}")
|
205 |
+
snapshot_download(
|
206 |
+
repo_id=da_model_name,
|
207 |
+
cache_dir=CACHE_DIR,
|
208 |
+
resume_download=True,
|
209 |
+
token=hf_token
|
210 |
+
)
|
211 |
+
print(f"Successfully downloaded {da_model_name}")
|
212 |
+
break
|
213 |
+
except Exception as e:
|
214 |
+
if attempt < max_retries - 1:
|
215 |
+
print(f"Depth Anything download attempt {attempt+1} failed: {str(e)}. Retrying after {retry_delay}s...")
|
216 |
+
time.sleep(retry_delay)
|
217 |
+
retry_delay *= 2
|
218 |
+
else:
|
219 |
+
print(f"Failed to load Depth Anything: {str(e)}. Falling back to DPT-Large only.")
|
220 |
+
depth_anything_model = None
|
221 |
+
depth_anything_processor = None
|
222 |
+
model_loaded = True
|
223 |
+
return dpt_estimator, None, None
|
224 |
+
else:
|
225 |
+
print(f"{da_model_name} already cached in {CACHE_DIR}")
|
226 |
|
227 |
depth_anything_processor = AutoImageProcessor.from_pretrained(
|
228 |
da_model_name,
|
|
|
300 |
else:
|
301 |
enhanced_depth = gaussian_filter(enhanced_depth, sigma=0.7)
|
302 |
|
303 |
+
enhanced_depth = np.clip(enhanced_depth, 0, 1)
|
304 |
+
return enhanced_depth
|
305 |
|
306 |
def depth_to_mesh(depth_map, image, resolution=80, detail_level='medium', view_angle=0):
|
307 |
enhanced_depth = enhance_depth_map(depth_map, detail_level)
|
|
|
331 |
y_grid = (y_grid / h - 0.5) * 2.0
|
332 |
vertices = np.vstack([x_grid.flatten(), -y_grid.flatten(), -z_values.flatten()]).T
|
333 |
|
|
|
334 |
if view_angle != 0:
|
335 |
rotation_matrix = trimesh.transformations.rotation_matrix(view_angle, [0, 1, 0])
|
336 |
vertices = trimesh.transform_points(vertices, rotation_matrix)
|
|
|
409 |
|
410 |
combined_mesh = trimesh.Trimesh(vertices=combined_vertices, faces=combined_faces)
|
411 |
|
|
|
412 |
combined_mesh = combined_mesh.subdivide_to_size(max_edge=0.05)
|
413 |
combined_mesh = combined_mesh.smoothed(method='laplacian', iterations=2)
|
|
|
|
|
414 |
combined_mesh.fill_holes()
|
415 |
combined_mesh.fix_normals()
|
416 |
|
|
|
540 |
view_angles = {'front': 0, 'back': np.pi, 'left': np.pi/2, 'right': -np.pi/2}
|
541 |
with torch.no_grad():
|
542 |
for view, image in images.items():
|
|
|
543 |
dpt_result = dpt_model(image)
|
544 |
dpt_depth = dpt_result["depth"]
|
545 |
|
|
|
546 |
if da_model and da_processor:
|
547 |
inputs = da_processor(images=image, return_tensors="pt")
|
548 |
inputs = {k: v.to("cpu") for k, v in inputs.items()}
|
|
|
766 |
"detail_level": "low, medium, or high",
|
767 |
"texture_quality": "low, medium, or high"
|
768 |
},
|
769 |
+
"description": "Creates high-quality 3D models from multiple 2D images using DPT-Large and Depth Anything."
|
770 |
}), 200
|
771 |
|
772 |
if __name__ == '__main__':
|