Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -228,14 +228,13 @@ class WarehouseStitcher:
|
|
| 228 |
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 229 |
result_pil = Image.fromarray(result_rgb)
|
| 230 |
|
| 231 |
-
# Save for download
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
# Return (image preview, logs, downloadable file)
|
| 237 |
-
|
| 238 |
-
return result_pil, "\n".join(logs), buf.getvalue()
|
| 239 |
|
| 240 |
|
| 241 |
class PoseGuidedWarehouseStitcher(WarehouseStitcher):
|
|
@@ -400,13 +399,12 @@ class PoseGuidedWarehouseStitcher(WarehouseStitcher):
|
|
| 400 |
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 401 |
result_pil = Image.fromarray(result_rgb)
|
| 402 |
|
| 403 |
-
# Save for download
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
|
| 408 |
-
|
| 409 |
-
return result_pil, "\n".join(logs), buf.getvalue()
|
| 410 |
|
| 411 |
|
| 412 |
def create_demo():
|
|
@@ -489,21 +487,54 @@ def create_demo():
|
|
| 489 |
if len(json_files) == 0:
|
| 490 |
return None, "❌ No JSON metadata files found in ZIP", None
|
| 491 |
|
| 492 |
-
# Match images with metadata by
|
| 493 |
-
|
| 494 |
-
|
| 495 |
|
| 496 |
for img_file in image_files:
|
| 497 |
-
#
|
| 498 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
json_candidates = [j for j in json_files if j.name == json_name]
|
| 500 |
|
| 501 |
if json_candidates:
|
| 502 |
-
|
| 503 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
-
|
| 506 |
-
|
|
|
|
| 507 |
|
| 508 |
# Update configuration
|
| 509 |
pose_stitcher.config['feature_extractor'] = feature_type
|
|
@@ -516,7 +547,8 @@ def create_demo():
|
|
| 516 |
return pose_stitcher.stitch_with_poses(image_paths, metadata_paths)
|
| 517 |
|
| 518 |
except Exception as e:
|
| 519 |
-
|
|
|
|
| 520 |
|
| 521 |
# Custom CSS
|
| 522 |
custom_css = """
|
|
@@ -695,8 +727,7 @@ dataset.zip
|
|
| 695 |
)
|
| 696 |
|
| 697 |
download_btn = gr.File(
|
| 698 |
-
label="⬇️ Download High-Resolution Result"
|
| 699 |
-
type="binary"
|
| 700 |
)
|
| 701 |
|
| 702 |
with gr.Column(scale=1):
|
|
@@ -742,6 +773,6 @@ if __name__ == "__main__":
|
|
| 742 |
demo.launch(
|
| 743 |
server_name="0.0.0.0",
|
| 744 |
server_port=7860,
|
| 745 |
-
share=
|
| 746 |
show_error=True
|
| 747 |
)
|
|
|
|
| 228 |
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 229 |
result_pil = Image.fromarray(result_rgb)
|
| 230 |
|
| 231 |
+
# Save to temporary file for download
|
| 232 |
+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.png', delete=False) as f:
|
| 233 |
+
result_pil.save(f, format='PNG', optimize=True)
|
| 234 |
+
temp_path = f.name
|
| 235 |
+
|
| 236 |
+
# Return (image preview, logs, downloadable file path)
|
| 237 |
+
return result_pil, "\n".join(logs), temp_path
|
|
|
|
| 238 |
|
| 239 |
|
| 240 |
class PoseGuidedWarehouseStitcher(WarehouseStitcher):
|
|
|
|
| 399 |
result_rgb = cv2.cvtColor(result, cv2.COLOR_BGR2RGB)
|
| 400 |
result_pil = Image.fromarray(result_rgb)
|
| 401 |
|
| 402 |
+
# Save to temporary file for download
|
| 403 |
+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.png', delete=False) as f:
|
| 404 |
+
result_pil.save(f, format='PNG', optimize=True)
|
| 405 |
+
temp_path = f.name
|
| 406 |
|
| 407 |
+
return result_pil, "\n".join(logs), temp_path
|
|
|
|
| 408 |
|
| 409 |
|
| 410 |
def create_demo():
|
|
|
|
| 487 |
if len(json_files) == 0:
|
| 488 |
return None, "❌ No JSON metadata files found in ZIP", None
|
| 489 |
|
| 490 |
+
# NEW: Match images with metadata by extracting base timestamp
|
| 491 |
+
# Pattern: z-9000_cam0_20251030_152626_496053_cam0.png -> z-9000_cam0_20251030_152626_496053
|
| 492 |
+
image_metadata_pairs = []
|
| 493 |
|
| 494 |
for img_file in image_files:
|
| 495 |
+
img_name = img_file.stem # Filename without extension
|
| 496 |
+
|
| 497 |
+
# Try to extract base name (remove _camX suffix if present)
|
| 498 |
+
# Pattern: z-9000_cam0_20251030_152626_496053_cam0 -> z-9000_cam0_20251030_152626_496053
|
| 499 |
+
if '_cam' in img_name:
|
| 500 |
+
parts = img_name.rsplit('_cam', 1)
|
| 501 |
+
if len(parts) == 2:
|
| 502 |
+
base_name = parts[0] # Everything before last _camX
|
| 503 |
+
else:
|
| 504 |
+
base_name = img_name
|
| 505 |
+
else:
|
| 506 |
+
base_name = img_name
|
| 507 |
+
|
| 508 |
+
# Look for matching JSON with base name
|
| 509 |
+
json_name = base_name + '.json'
|
| 510 |
json_candidates = [j for j in json_files if j.name == json_name]
|
| 511 |
|
| 512 |
if json_candidates:
|
| 513 |
+
image_metadata_pairs.append({
|
| 514 |
+
'image': str(img_file),
|
| 515 |
+
'metadata': str(json_candidates[0]),
|
| 516 |
+
'base_name': base_name,
|
| 517 |
+
'timestamp': None # Will be loaded from JSON
|
| 518 |
+
})
|
| 519 |
+
|
| 520 |
+
if len(image_metadata_pairs) < 2:
|
| 521 |
+
return None, f"❌ Only {len(image_metadata_pairs)} images have matching metadata. Need at least 2.\n\nFound images: {[f.name for f in image_files[:5]]}\nFound JSON: {[j.name for j in json_files[:5]]}", None
|
| 522 |
+
|
| 523 |
+
# Load timestamps and sort
|
| 524 |
+
for pair in image_metadata_pairs:
|
| 525 |
+
try:
|
| 526 |
+
with open(pair['metadata'], 'r') as f:
|
| 527 |
+
metadata = json.load(f)
|
| 528 |
+
pair['timestamp'] = metadata['nav_snapshot']['timestamp_usec']
|
| 529 |
+
except Exception as e:
|
| 530 |
+
return None, f"❌ Error reading metadata {pair['metadata']}: {str(e)}", None
|
| 531 |
+
|
| 532 |
+
# Sort by timestamp
|
| 533 |
+
image_metadata_pairs.sort(key=lambda x: x['timestamp'])
|
| 534 |
|
| 535 |
+
# Extract sorted lists
|
| 536 |
+
image_paths = [p['image'] for p in image_metadata_pairs]
|
| 537 |
+
metadata_paths = [p['metadata'] for p in image_metadata_pairs]
|
| 538 |
|
| 539 |
# Update configuration
|
| 540 |
pose_stitcher.config['feature_extractor'] = feature_type
|
|
|
|
| 547 |
return pose_stitcher.stitch_with_poses(image_paths, metadata_paths)
|
| 548 |
|
| 549 |
except Exception as e:
|
| 550 |
+
import traceback
|
| 551 |
+
return None, f"❌ Error processing ZIP: {str(e)}\n\n{traceback.format_exc()}", None
|
| 552 |
|
| 553 |
# Custom CSS
|
| 554 |
custom_css = """
|
|
|
|
| 727 |
)
|
| 728 |
|
| 729 |
download_btn = gr.File(
|
| 730 |
+
label="⬇️ Download High-Resolution Result"
|
|
|
|
| 731 |
)
|
| 732 |
|
| 733 |
with gr.Column(scale=1):
|
|
|
|
| 773 |
demo.launch(
|
| 774 |
server_name="0.0.0.0",
|
| 775 |
server_port=7860,
|
| 776 |
+
share=True,
|
| 777 |
show_error=True
|
| 778 |
)
|