Spaces:
Running on Zero
Running on Zero
fix bug
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
|
|
|
| 4 |
import uuid
|
| 5 |
from pathlib import Path
|
| 6 |
from typing import Optional
|
|
@@ -213,7 +214,99 @@ def _export_glb_from_points(xyz: np.ndarray, rgb: np.ndarray, output_path: Path)
|
|
| 213 |
cloud.export(output_path.as_posix())
|
| 214 |
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
@spaces.GPU(duration=180)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
def run_demo(
|
| 218 |
image: np.ndarray,
|
| 219 |
depth_file,
|
|
@@ -228,71 +321,40 @@ def run_demo(
|
|
| 228 |
cy_org: Optional[float],
|
| 229 |
):
|
| 230 |
request_id = uuid.uuid4().hex[:8]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
try:
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
depth_path = None
|
| 236 |
-
if depth_file is not None:
|
| 237 |
-
depth_path = depth_file if isinstance(depth_file, str) else depth_file.name
|
| 238 |
-
|
| 239 |
-
image_shape = tuple(int(dim) for dim in image.shape) if image is not None else None
|
| 240 |
-
Log.info(
|
| 241 |
-
f"[{request_id}] run_demo start: model_type={model_type}, "
|
| 242 |
-
f"input_size={input_size}, output_resolution_mode={output_resolution_mode}, "
|
| 243 |
-
f"upsample_ratio={upsample_ratio}, max_points_preview={max_points_preview}, "
|
| 244 |
-
f"depth_path={depth_path}, image_shape={image_shape}"
|
| 245 |
-
)
|
| 246 |
-
|
| 247 |
-
result = run_single_image_demo(
|
| 248 |
-
image_np=image,
|
| 249 |
-
depth_path=depth_path,
|
| 250 |
model_type=model_type,
|
| 251 |
-
|
| 252 |
output_resolution_mode=output_resolution_mode,
|
| 253 |
-
upsample_ratio=
|
| 254 |
-
max_points_preview=
|
| 255 |
-
fx_org=
|
| 256 |
-
fy_org=
|
| 257 |
-
cx_org=
|
| 258 |
-
cy_org=
|
| 259 |
-
model_cache=MODEL_CACHE,
|
| 260 |
-
)
|
| 261 |
-
output_dir = _prepare_output_dir()
|
| 262 |
-
glb_path = output_dir / "pointcloud.glb"
|
| 263 |
-
ply_path = output_dir / "pointcloud.ply"
|
| 264 |
-
depth_vis_path = output_dir / "depth_colorized.png"
|
| 265 |
-
|
| 266 |
-
_export_glb_from_points(result.xyz, result.rgb, glb_path)
|
| 267 |
-
if os.path.exists(result.ply_path):
|
| 268 |
-
shutil.copy2(result.ply_path, ply_path)
|
| 269 |
-
depth_vis_uint8 = result.depth_vis if result.depth_vis.dtype == np.uint8 else result.depth_vis.astype(np.uint8)
|
| 270 |
-
Image.fromarray(depth_vis_uint8).save(depth_vis_path)
|
| 271 |
-
|
| 272 |
-
depth_npy_path = output_dir / "depth.npy"
|
| 273 |
-
if os.path.exists(result.depth_npy_path):
|
| 274 |
-
shutil.copy2(result.depth_npy_path, depth_npy_path)
|
| 275 |
-
|
| 276 |
-
download_files = [glb_path.as_posix(), depth_vis_path.as_posix()]
|
| 277 |
-
if depth_npy_path.exists():
|
| 278 |
-
download_files.append(depth_npy_path.as_posix())
|
| 279 |
-
if ply_path.exists():
|
| 280 |
-
download_files.append(ply_path.as_posix())
|
| 281 |
-
|
| 282 |
-
status = (
|
| 283 |
-
f"Done. Output depth: {result.output_size[0]}x{result.output_size[1]}. "
|
| 284 |
-
f"Depth source: {result.depth_source_label}. "
|
| 285 |
-
f"Preview points: {result.xyz.shape[0]}. "
|
| 286 |
-
f"Camera intrinsics: {'default' if result.used_default_intrinsics else 'custom'}."
|
| 287 |
-
)
|
| 288 |
-
Log.info(
|
| 289 |
-
f"[{request_id}] run_demo success: output_dir={output_dir}, output_size={result.output_size}, "
|
| 290 |
-
f"depth_source={result.depth_source_label}, preview_points={result.xyz.shape[0]}"
|
| 291 |
)
|
| 292 |
-
|
|
|
|
|
|
|
| 293 |
except Exception as exc:
|
|
|
|
| 294 |
Log.exception(f"[{request_id}] run_demo failed")
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
DESCRIPTION_MD = """
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
import tempfile
|
| 4 |
+
import traceback
|
| 5 |
import uuid
|
| 6 |
from pathlib import Path
|
| 7 |
from typing import Optional
|
|
|
|
| 214 |
cloud.export(output_path.as_posix())
|
| 215 |
|
| 216 |
|
| 217 |
+
def _run_demo_impl(
|
| 218 |
+
image: np.ndarray,
|
| 219 |
+
depth_file,
|
| 220 |
+
model_type: str,
|
| 221 |
+
input_size: str,
|
| 222 |
+
output_resolution_mode: str,
|
| 223 |
+
upsample_ratio: int,
|
| 224 |
+
max_points_preview: int,
|
| 225 |
+
fx_org: Optional[float],
|
| 226 |
+
fy_org: Optional[float],
|
| 227 |
+
cx_org: Optional[float],
|
| 228 |
+
cy_org: Optional[float],
|
| 229 |
+
):
|
| 230 |
+
if image is None:
|
| 231 |
+
raise ValueError("Input RGB image is required")
|
| 232 |
+
|
| 233 |
+
depth_path = None
|
| 234 |
+
if depth_file is not None:
|
| 235 |
+
depth_path = depth_file if isinstance(depth_file, str) else depth_file.name
|
| 236 |
+
|
| 237 |
+
result = run_single_image_demo(
|
| 238 |
+
image_np=image,
|
| 239 |
+
depth_path=depth_path,
|
| 240 |
+
model_type=model_type,
|
| 241 |
+
input_size_text=input_size,
|
| 242 |
+
output_resolution_mode=output_resolution_mode,
|
| 243 |
+
upsample_ratio=int(upsample_ratio),
|
| 244 |
+
max_points_preview=int(max_points_preview),
|
| 245 |
+
fx_org=_none_if_invalid(fx_org),
|
| 246 |
+
fy_org=_none_if_invalid(fy_org),
|
| 247 |
+
cx_org=_none_if_invalid(cx_org),
|
| 248 |
+
cy_org=_none_if_invalid(cy_org),
|
| 249 |
+
model_cache=MODEL_CACHE,
|
| 250 |
+
)
|
| 251 |
+
output_dir = _prepare_output_dir()
|
| 252 |
+
glb_path = output_dir / "pointcloud.glb"
|
| 253 |
+
ply_path = output_dir / "pointcloud.ply"
|
| 254 |
+
depth_vis_path = output_dir / "depth_colorized.png"
|
| 255 |
+
|
| 256 |
+
_export_glb_from_points(result.xyz, result.rgb, glb_path)
|
| 257 |
+
if os.path.exists(result.ply_path):
|
| 258 |
+
shutil.copy2(result.ply_path, ply_path)
|
| 259 |
+
depth_vis_uint8 = result.depth_vis if result.depth_vis.dtype == np.uint8 else result.depth_vis.astype(np.uint8)
|
| 260 |
+
Image.fromarray(depth_vis_uint8).save(depth_vis_path)
|
| 261 |
+
|
| 262 |
+
depth_npy_path = output_dir / "depth.npy"
|
| 263 |
+
if os.path.exists(result.depth_npy_path):
|
| 264 |
+
shutil.copy2(result.depth_npy_path, depth_npy_path)
|
| 265 |
+
|
| 266 |
+
download_files = [glb_path.as_posix(), depth_vis_path.as_posix()]
|
| 267 |
+
if depth_npy_path.exists():
|
| 268 |
+
download_files.append(depth_npy_path.as_posix())
|
| 269 |
+
if ply_path.exists():
|
| 270 |
+
download_files.append(ply_path.as_posix())
|
| 271 |
+
|
| 272 |
+
status = (
|
| 273 |
+
f"Done. Output depth: {result.output_size[0]}x{result.output_size[1]}. "
|
| 274 |
+
f"Depth source: {result.depth_source_label}. "
|
| 275 |
+
f"Preview points: {result.xyz.shape[0]}. "
|
| 276 |
+
f"Camera intrinsics: {'default' if result.used_default_intrinsics else 'custom'}."
|
| 277 |
+
)
|
| 278 |
+
return result.depth_vis, glb_path.as_posix(), download_files, status
|
| 279 |
+
|
| 280 |
+
|
| 281 |
@spaces.GPU(duration=180)
|
| 282 |
+
def run_demo_gpu(
|
| 283 |
+
image: np.ndarray,
|
| 284 |
+
depth_file,
|
| 285 |
+
model_type: str,
|
| 286 |
+
input_size: str,
|
| 287 |
+
output_resolution_mode: str,
|
| 288 |
+
upsample_ratio: int,
|
| 289 |
+
max_points_preview: int,
|
| 290 |
+
fx_org: Optional[float],
|
| 291 |
+
fy_org: Optional[float],
|
| 292 |
+
cx_org: Optional[float],
|
| 293 |
+
cy_org: Optional[float],
|
| 294 |
+
):
|
| 295 |
+
return _run_demo_impl(
|
| 296 |
+
image=image,
|
| 297 |
+
depth_file=depth_file,
|
| 298 |
+
model_type=model_type,
|
| 299 |
+
input_size=input_size,
|
| 300 |
+
output_resolution_mode=output_resolution_mode,
|
| 301 |
+
upsample_ratio=upsample_ratio,
|
| 302 |
+
max_points_preview=max_points_preview,
|
| 303 |
+
fx_org=fx_org,
|
| 304 |
+
fy_org=fy_org,
|
| 305 |
+
cx_org=cx_org,
|
| 306 |
+
cy_org=cy_org,
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
|
| 310 |
def run_demo(
|
| 311 |
image: np.ndarray,
|
| 312 |
depth_file,
|
|
|
|
| 321 |
cy_org: Optional[float],
|
| 322 |
):
|
| 323 |
request_id = uuid.uuid4().hex[:8]
|
| 324 |
+
depth_path = None
|
| 325 |
+
if depth_file is not None:
|
| 326 |
+
depth_path = depth_file if isinstance(depth_file, str) else depth_file.name
|
| 327 |
+
image_shape = tuple(int(dim) for dim in image.shape) if image is not None else None
|
| 328 |
+
Log.info(
|
| 329 |
+
f"[{request_id}] run_demo start: model_type={model_type}, "
|
| 330 |
+
f"input_size={input_size}, output_resolution_mode={output_resolution_mode}, "
|
| 331 |
+
f"upsample_ratio={upsample_ratio}, max_points_preview={max_points_preview}, "
|
| 332 |
+
f"depth_path={depth_path}, image_shape={image_shape}"
|
| 333 |
+
)
|
| 334 |
try:
|
| 335 |
+
result = run_demo_gpu(
|
| 336 |
+
image=image,
|
| 337 |
+
depth_file=depth_file,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
model_type=model_type,
|
| 339 |
+
input_size=input_size,
|
| 340 |
output_resolution_mode=output_resolution_mode,
|
| 341 |
+
upsample_ratio=upsample_ratio,
|
| 342 |
+
max_points_preview=max_points_preview,
|
| 343 |
+
fx_org=fx_org,
|
| 344 |
+
fy_org=fy_org,
|
| 345 |
+
cx_org=cx_org,
|
| 346 |
+
cy_org=cy_org,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
)
|
| 348 |
+
status_text = result[3] if isinstance(result, tuple) and len(result) == 4 else "Done."
|
| 349 |
+
Log.info(f"[{request_id}] run_demo success: {status_text}")
|
| 350 |
+
return result
|
| 351 |
except Exception as exc:
|
| 352 |
+
error_trace = traceback.format_exc()
|
| 353 |
Log.exception(f"[{request_id}] run_demo failed")
|
| 354 |
+
error_message = f"Error [{request_id}]: {exc}"
|
| 355 |
+
if os.getenv("INFINIDEPTH_SHOW_TRACEBACK", "0") == "1":
|
| 356 |
+
error_message = f"{error_message}\n\n{error_trace}"
|
| 357 |
+
return None, None, [], error_message
|
| 358 |
|
| 359 |
|
| 360 |
DESCRIPTION_MD = """
|