aknapitsch user commited on
Commit
65dafca
·
1 Parent(s): 0fc3f5c

HEIC support

Browse files
app.py CHANGED
@@ -17,6 +17,10 @@ import gradio as gr
17
  import numpy as np
18
  import spaces
19
  import torch
 
 
 
 
20
 
21
  sys.path.append("mapanything/")
22
 
@@ -117,8 +121,8 @@ def run_model(
117
  print("Running inference...")
118
  # apply_mask: Whether to apply the non-ambiguous mask to the output. Defaults to True.
119
  # mask_edges: Whether to compute an edge mask based on normals and depth and apply it to the output. Defaults to True.
120
- # Use checkbox values
121
- outputs = model.infer(views, apply_mask=apply_mask, mask_edges=mask_edges)
122
 
123
  # Convert predictions to format expected by visualization
124
  predictions = {}
@@ -389,9 +393,36 @@ def handle_uploads(input_video, input_images, s_time_interval=1.0):
389
  file_path = file_data["name"]
390
  else:
391
  file_path = file_data
392
- dst_path = os.path.join(target_dir_images, os.path.basename(file_path))
393
- shutil.copy(file_path, dst_path)
394
- image_paths.append(dst_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
 
396
  # --- Handle video ---
397
  if input_video is not None:
@@ -460,7 +491,6 @@ def gradio_demo(
460
  filter_black_bg=False,
461
  filter_white_bg=False,
462
  apply_mask=True,
463
- mask_edges=True,
464
  show_mesh=True,
465
  ):
466
  """
@@ -485,7 +515,7 @@ def gradio_demo(
485
 
486
  print("Running MapAnything model...")
487
  with torch.no_grad():
488
- predictions, processed_data = run_model(target_dir, apply_mask, mask_edges)
489
 
490
  # Save predictions
491
  prediction_save_path = os.path.join(target_dir, "predictions.npz")
@@ -1080,7 +1110,9 @@ with gr.Blocks(theme=theme, css=GRADIO_CSS) as demo:
1080
 
1081
  with gr.Column(scale=4):
1082
  with gr.Column():
1083
- gr.Markdown("**Metric 3D Reconstruction (Point Cloud and Camera Poses)**")
 
 
1084
  log_output = gr.Markdown(
1085
  "Please upload a video or images, then click Reconstruct.",
1086
  elem_classes=["custom-log"],
@@ -1192,7 +1224,8 @@ with gr.Blocks(theme=theme, css=GRADIO_CSS) as demo:
1192
  )
1193
  gr.Markdown("### Reconstruction Options: (updated on next run)")
1194
  apply_mask_checkbox = gr.Checkbox(
1195
- label="Apply mask for predicted ambiguous depth classes & edges", value=True
 
1196
  )
1197
  # ---------------------- Example Scenes Section ----------------------
1198
  gr.Markdown("## Example Scenes (lists all scenes in the examples folder)")
 
17
  import numpy as np
18
  import spaces
19
  import torch
20
+ from pillow_heif import register_heif_opener
21
+ from PIL import Image
22
+
23
+ register_heif_opener()
24
 
25
  sys.path.append("mapanything/")
26
 
 
121
  print("Running inference...")
122
  # apply_mask: Whether to apply the non-ambiguous mask to the output. Defaults to True.
123
  # mask_edges: Whether to compute an edge mask based on normals and depth and apply it to the output. Defaults to True.
124
+ # Use checkbox values - mask_edges is set to True by default since there's no UI control for it
125
+ outputs = model.infer(views, apply_mask=apply_mask, mask_edges=True)
126
 
127
  # Convert predictions to format expected by visualization
128
  predictions = {}
 
393
  file_path = file_data["name"]
394
  else:
395
  file_path = file_data
396
+
397
+ # Check if the file is a HEIC image
398
+ file_ext = os.path.splitext(file_path)[1].lower()
399
+ if file_ext in ['.heic', '.heif']:
400
+ # Convert HEIC to JPEG for better gallery compatibility
401
+ try:
402
+ with Image.open(file_path) as img:
403
+ # Convert to RGB if necessary (HEIC can have different color modes)
404
+ if img.mode not in ('RGB', 'L'):
405
+ img = img.convert('RGB')
406
+
407
+ # Create JPEG filename
408
+ base_name = os.path.splitext(os.path.basename(file_path))[0]
409
+ dst_path = os.path.join(target_dir_images, f"{base_name}.jpg")
410
+
411
+ # Save as JPEG with high quality
412
+ img.save(dst_path, 'JPEG', quality=95)
413
+ image_paths.append(dst_path)
414
+ print(f"Converted HEIC to JPEG: {os.path.basename(file_path)} -> {os.path.basename(dst_path)}")
415
+ except Exception as e:
416
+ print(f"Error converting HEIC file {file_path}: {e}")
417
+ # Fall back to copying as is
418
+ dst_path = os.path.join(target_dir_images, os.path.basename(file_path))
419
+ shutil.copy(file_path, dst_path)
420
+ image_paths.append(dst_path)
421
+ else:
422
+ # Regular image files - copy as is
423
+ dst_path = os.path.join(target_dir_images, os.path.basename(file_path))
424
+ shutil.copy(file_path, dst_path)
425
+ image_paths.append(dst_path)
426
 
427
  # --- Handle video ---
428
  if input_video is not None:
 
491
  filter_black_bg=False,
492
  filter_white_bg=False,
493
  apply_mask=True,
 
494
  show_mesh=True,
495
  ):
496
  """
 
515
 
516
  print("Running MapAnything model...")
517
  with torch.no_grad():
518
+ predictions, processed_data = run_model(target_dir, apply_mask)
519
 
520
  # Save predictions
521
  prediction_save_path = os.path.join(target_dir, "predictions.npz")
 
1110
 
1111
  with gr.Column(scale=4):
1112
  with gr.Column():
1113
+ gr.Markdown(
1114
+ "**Metric 3D Reconstruction (Point Cloud and Camera Poses)**"
1115
+ )
1116
  log_output = gr.Markdown(
1117
  "Please upload a video or images, then click Reconstruct.",
1118
  elem_classes=["custom-log"],
 
1224
  )
1225
  gr.Markdown("### Reconstruction Options: (updated on next run)")
1226
  apply_mask_checkbox = gr.Checkbox(
1227
+ label="Apply mask for predicted ambiguous depth classes & edges",
1228
+ value=True,
1229
  )
1230
  # ---------------------- Example Scenes Section ----------------------
1231
  gr.Markdown("## Example Scenes (lists all scenes in the examples folder)")
mapanything/utils/hf_utils/css_and_html.py CHANGED
@@ -148,7 +148,7 @@ def get_description_html():
148
  </details>
149
  </li>
150
  </ol>
151
- <p><strong style="color: #555555;">Please note:</strong> <span style="color: #555555;">Our model itself usually only needs less than 1 second to reconstruct a scene. However, visualizing 3D points may take tens of seconds due to third-party rendering, which is independent of MapAnything's processing time. Please be patient or, for faster visualization, use a local machine to run our demo from our <a href="https://github.com/facebookresearch/map-anything">GitHub repository</a>. </span></p>
152
  </div>
153
  """
154
 
 
148
  </details>
149
  </li>
150
  </ol>
151
+ <p><strong style="color: #555555;">Please note:</strong> <span style="color: #555555;">Depending on the amount of images, our model usually only needs about 1 second to reconstruct a scene. However, downloading model weights and visualizing 3D points may take tens of seconds. Please be patient or, for faster visualization, use a local machine to run our demo from our <a href="https://github.com/facebookresearch/map-anything">GitHub repository</a>. </span></p>
152
  </div>
153
  """
154