File size: 12,243 Bytes
a5b5add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
from typing import Tuple

import gradio as gr
import numpy as np
import cv2
import torch
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from numpy import ndarray
import visualize

CSS = """

#desc, #desc * {

    text-align: center !important;

    justify-content: center !important;

    align-items: center !important;

}

"""

DESCRIPTION = """

<div align="center">

<h1><ins>MapGlue</ins> 🗺️</h1>

<h2>

    MapGlue: Multimodal Remote Sensing Image Matching

</h2>

<p>

    Advanced feature matching system supporting various image modalities including:<br>

    SAR-Visible, Map-Visible, Depth-Visible, Infrared-Visible, Day-Night matching

</p>

</div>

"""

examples = [
    [
        "assets/day-night/L1.png",
        "assets/day-night/R1.png",
    ],
    [
        "assets/day-night/L2.png",
        "assets/day-night/R2.png",
    ],
    [
        "assets/depth-visible/L1.jpg",
        "assets/depth-visible/R1.jpg",
    ],
    [
        "assets/depth-visible/L2.png",
        "assets/depth-visible/R2.png",
    ],
    [
        "assets/infrared-visible/L1.png",
        "assets/infrared-visible/R1.png",
    ],
    [
        "assets/infrared-visible/L2.png",
        "assets/infrared-visible/R2.png",
    ],
    [
        "assets/map-visible/L1.jpg",
        "assets/map-visible/R1.jpg",
    ],
    [
        "assets/map-visible/L2.png", 
        "assets/map-visible/R2.png",
    ],
    [
        "assets/sar-visible/L1.jpg",
        "assets/sar-visible/R1.jpg",
    ],
    [
        "assets/sar-visible/L2.jpg",
        "assets/sar-visible/R2.jpg",
    ],
    [
        "assets/sar-visible/L3.png",
        "assets/sar-visible/R3.png",
    ],
]


def fig_to_ndarray(fig: Figure) -> ndarray:
    """Convert matplotlib figure to numpy array."""
    fig.canvas.draw()
    w, h = fig.canvas.get_width_height()
    buffer = fig.canvas.buffer_rgba()
    out = np.frombuffer(buffer, dtype=np.uint8).reshape(h, w, 4)
    return out

def load_mapglue_model():
    """Load the MapGlue TorchScript model."""
    # device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
    device = 'cpu'
    model_path = './weights/fastmapglue_model.pt'
    
    if not os.path.exists(model_path):
        raise FileNotFoundError(
            f"Model file not found: {model_path}\n"
            f"Please ensure the HF_TOKEN environment variable is set to download the model."
        )
    
    model = torch.jit.load(model_path, map_location=device)
    model.eval()
    model.to(device)
    return model, device


def run_mapglue_matching(

    path0: str,

    path1: str,

    model_name: str,

    num_keypoints: int,

    ransac_threshold: float,

) -> Tuple[ndarray, ndarray, ndarray, ndarray]:
    """

    Run MapGlue matching on two input images using Homography RANSAC.

    

    Args:

        path0, path1: Paths to input images

        model_name: Name of the matching model (currently supports FastMapGlue)

        num_keypoints: Number of keypoints to extract

        ransac_threshold: RANSAC reprojection threshold

    

    Returns:

        Tuple of (raw_keypoint_fig, raw_matching_fig, ransac_keypoint_fig, ransac_matching_fig)

    """
    try:
        # Load model
        model, device = load_mapglue_model()
        
        # Load and preprocess images
        image0 = cv2.imread(path0)
        image1 = cv2.imread(path1)
        
        if image0 is None or image1 is None:
            raise ValueError("Could not load one or both images")
        
        # Convert BGR to RGB
        image0 = cv2.cvtColor(image0, cv2.COLOR_BGR2RGB)
        image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)
        
        # Convert to torch tensors
        image0_tensor = torch.from_numpy(image0).to(device)
        image1_tensor = torch.from_numpy(image1).to(device)
        num_keypoints_tensor = torch.tensor(num_keypoints).to(device)
        
        # Run inference
        with torch.no_grad():
            points_tensor = model(image0_tensor, image1_tensor, num_keypoints_tensor)
            points0 = points_tensor[:, :2]
            points1 = points_tensor[:, 2:]
        
        # Create raw matching visualization
        plt.figure(figsize=(12, 6))
        axes = visualize.show_images([image0, image1])
        visualize.draw_matches(points0, points1, line_colors="lime", line_width=0.8)
        visualize.add_text(0, f'Raw matches: {len(points0)}', font_size=16)
        raw_matching_fig = fig_to_ndarray(plt.gcf())
        
        # Create raw keypoints visualization
        plt.figure(figsize=(12, 6))
        axes = visualize.show_images([image0, image1])
        visualize.draw_keypoints([points0.cpu().numpy(), points1.cpu().numpy()], 
                               kp_color=["lime", "lime"], kp_size=20)
        visualize.add_text(0, f'Raw keypoints: {len(points0)}', font_size=16)
        raw_keypoint_fig = fig_to_ndarray(plt.gcf())
        
        # Apply RANSAC filtering
        points0_np = points0.cpu().numpy()
        points1_np = points1.cpu().numpy()

  
        H_pred, inlier_mask = cv2.findHomography(
            points0_np, points1_np,
            cv2.USAC_MAGSAC,
            ransacReprojThreshold=ransac_threshold,
            maxIters=10000,
            confidence=0.9999
        )

        if inlier_mask is not None and inlier_mask.sum() > 0:
            inlier_mask = inlier_mask.ravel() > 0
            mkpts0 = points0_np[inlier_mask]
            mkpts1 = points1_np[inlier_mask]
            
            # Create RANSAC matching visualization
            plt.figure(figsize=(12, 6))
            axes = visualize.show_images([image0, image1])
            visualize.draw_matches(mkpts0, mkpts1, line_colors="lime", line_width=1)
            visualize.add_text(0, f'RANSAC matches @{ransac_threshold}px: {len(mkpts0)}/{len(points0)}', font_size=16)
            ransac_matching_fig = fig_to_ndarray(plt.gcf())
            
            # Create RANSAC keypoints visualization
            plt.figure(figsize=(12, 6))
            axes = visualize.show_images([image0, image1])
            visualize.draw_keypoints([mkpts0, mkpts1], 
                                   kp_color=["lime", "lime"], kp_size=20)
            visualize.add_text(0, f'RANSAC keypoints @{ransac_threshold}px: {len(mkpts0)}', font_size=16)
            ransac_keypoint_fig = fig_to_ndarray(plt.gcf())
        else:
            # No inliers found
            ransac_matching_fig = None
            ransac_keypoint_fig = None
        
        plt.close('all')  # Clean up matplotlib figures
        
        return (
            raw_keypoint_fig,
            raw_matching_fig, 
            ransac_keypoint_fig,
            ransac_matching_fig,
        )
        
    except Exception as e:
        print(f"Error in matching: {str(e)}")
        # Return empty arrays in case of error
        empty_img = np.zeros((400, 800, 4), dtype=np.uint8)
        return (empty_img, empty_img, empty_img, empty_img)


with gr.Blocks(css=CSS) as demo:
    with gr.Tab("Image Matching"):
        with gr.Row():
            with gr.Column(scale=3):
                gr.HTML(DESCRIPTION, elem_id="desc")
        with gr.Row():
            with gr.Column():
                gr.Markdown("### Input Panels:")
                with gr.Row():
                    model_name = gr.Dropdown(
                        choices=["FastMapGlue"],
                        value="FastMapGlue",
                        label="Matching Model",
                    )
                with gr.Row():
                    path0 = gr.Image(
                        height=300,
                        image_mode="RGB",
                        type="filepath",
                        label="Image 0",
                    )
                    path1 = gr.Image(
                        height=300,
                        image_mode="RGB", 
                        type="filepath",
                        label="Image 1",
                    )
                with gr.Row():
                    stop = gr.Button(value="Stop", variant="stop")
                    run = gr.Button(value="Run", variant="primary")
                    
                with gr.Accordion("Advanced Settings", open=False):
                    with gr.Accordion("Matching Settings"):
                        with gr.Row():
                            num_keypoints = gr.Slider(
                                minimum=512,
                                maximum=4096,
                                value=2048,
                                step=256,
                                label="Number of Keypoints",
                            )
                    with gr.Accordion("RANSAC Settings"):
                        with gr.Row():
                            ransac_threshold = gr.Slider(
                                minimum=0.5,
                                maximum=10.0,
                                value=5.0,
                                step=0.5,
                                label="RANSAC Threshold",
                            )
                            
                with gr.Row():
                    with gr.Accordion("Example Pairs"):
                        gr.Examples(
                            examples=examples,
                            inputs=[path0, path1],
                            label="Click an example pair below",
                        )
                        
            with gr.Column():
                gr.Markdown(
                    "### Output Panels"
                )
                with gr.Accordion("Raw Keypoints", open=False):
                    raw_keypoint_fig = gr.Image(
                        format="png", type="numpy", label="Raw Keypoints"
                    )
                with gr.Accordion("Raw Matches"):
                    raw_matching_fig = gr.Image(
                        format="png", type="numpy", label="Raw Matches"
                    )
                with gr.Accordion("RANSAC Keypoints", open=False):
                    ransac_keypoint_fig = gr.Image(
                        format="png", type="numpy", label="RANSAC Keypoints"
                    )
                with gr.Accordion("RANSAC Matches"):
                    ransac_matching_fig = gr.Image(
                        format="png", type="numpy", label="RANSAC Matches"
                    )

        inputs = [
            path0,
            path1,
            model_name,
            num_keypoints,
            ransac_threshold,
        ]
        outputs = [
            raw_keypoint_fig,
            raw_matching_fig,
            ransac_keypoint_fig,
            ransac_matching_fig,
        ]

        running_event = run.click(
            fn=run_mapglue_matching, inputs=inputs, outputs=outputs
        )
        stop.click(
            fn=None, inputs=None, outputs=None, cancels=[running_event]
        )

if __name__ == "__main__":
    # Download model weights on startup if HF_TOKEN is available
    HF_TOKEN = os.getenv("HF_TOKEN")
    if HF_TOKEN:
        model_path = './weights/fastmapglue_model.pt'
        if not os.path.exists(model_path):
            try:
                import requests
                # 使用 resolve 来直接下载文件
                model_url = "https://huggingface.co/wupeihao/mapglue/resolve/main/fastmapglue_model.pt"
                headers = {"Authorization": f"Bearer {HF_TOKEN}"}
                
                print("Downloading MapGlue model...")
                response = requests.get(model_url, headers=headers)
                response.raise_for_status()
                
                os.makedirs('./weights', exist_ok=True)
                with open(model_path, 'wb') as f:
                    f.write(response.content)
                print("Model downloaded successfully!")
            except Exception as e:
                print(f"Failed to download model: {str(e)}")
    
    demo.launch()