File size: 36,426 Bytes
bffff04
7acaad7
8320ccc
 
 
 
57c1094
 
8320ccc
bffff04
 
2d554b0
 
8320ccc
 
5069bec
7acaad7
8320ccc
 
7acaad7
4a7fc02
e9f6961
7acaad7
 
 
 
 
 
 
 
 
 
 
 
 
2f1d0e2
 
 
 
 
7acaad7
 
 
 
 
 
 
 
 
 
 
 
 
 
7419d98
 
 
 
 
 
 
7acaad7
2f1d0e2
2507d2f
 
 
 
 
 
 
7acaad7
2507d2f
 
 
 
7acaad7
2507d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7acaad7
2507d2f
 
 
 
 
7acaad7
2507d2f
a44851c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2507d2f
 
 
 
 
 
2f1d0e2
2507d2f
 
57c1094
2507d2f
 
 
 
 
 
 
 
 
 
 
 
2f1d0e2
2507d2f
 
8320ccc
 
 
 
 
2f1d0e2
8320ccc
 
2507d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7acaad7
2507d2f
 
 
 
7acaad7
2507d2f
 
7acaad7
2507d2f
 
 
 
 
7acaad7
2507d2f
 
 
 
 
 
 
 
7acaad7
2507d2f
 
7acaad7
2507d2f
 
 
 
 
 
 
 
 
a44851c
 
 
 
 
 
2507d2f
 
 
 
 
 
57c1094
2507d2f
 
 
 
 
 
 
 
a44851c
 
 
2507d2f
 
 
 
 
e9f6961
 
 
 
 
 
 
 
 
 
 
 
 
 
2507d2f
 
 
 
 
2f1d0e2
e9f6961
 
 
 
 
 
 
4f55d39
 
 
 
 
e9f6961
 
 
 
 
 
4f55d39
 
 
 
 
e9f6961
 
 
 
2507d2f
 
 
68a65da
 
 
2507d2f
 
7acaad7
2507d2f
 
 
e9f6961
2507d2f
 
 
4a7fc02
e9f6961
 
 
 
 
2507d2f
 
 
 
 
7acaad7
 
2507d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68a65da
2507d2f
 
 
 
 
 
 
7acaad7
 
 
57c1094
7acaad7
 
2507d2f
 
 
 
 
 
 
 
 
 
7acaad7
 
 
 
3c77caa
68a65da
a44851c
7acaad7
2507d2f
 
 
 
 
7acaad7
2507d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68a65da
2507d2f
7acaad7
2507d2f
e9f6961
 
 
 
 
 
 
2507d2f
 
 
 
 
 
 
 
 
 
7acaad7
e8c2e78
57c1094
 
 
 
 
 
 
2d554b0
7acaad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a44851c
2f1d0e2
a44851c
7acaad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a44851c
7acaad7
 
 
 
 
 
 
 
 
 
bffff04
7acaad7
 
 
 
 
 
57c1094
7acaad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68a65da
a44851c
7acaad7
2d36d99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae8c1a
2d36d99
 
 
 
 
 
 
 
10dcc2e
2d36d99
 
 
 
 
 
 
3c77caa
2d36d99
 
 
3c77caa
2d36d99
 
b5957bd
 
 
 
2d36d99
 
e8c2e78
 
 
57c1094
 
 
2d554b0
 
e8c2e78
 
 
 
 
2d554b0
 
 
 
e8c2e78
 
 
 
 
57c1094
 
 
2d554b0
57c1094
 
 
 
 
 
 
e8c2e78
 
 
 
 
 
 
 
 
 
2f1d0e2
e8c2e78
 
 
 
 
57c1094
 
 
 
 
e8c2e78
 
 
 
 
 
57c1094
e8c2e78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57c1094
e8c2e78
 
 
 
 
 
 
 
57c1094
e8c2e78
 
 
 
 
 
57c1094
 
e8c2e78
 
 
 
 
 
 
 
 
57c1094
 
e8c2e78
 
 
 
 
57c1094
 
 
 
 
 
 
 
 
 
 
 
 
 
e8c2e78
 
 
 
 
 
 
 
 
57c1094
e8c2e78
 
 
 
 
 
 
57c1094
e8c2e78
 
 
 
 
 
 
 
 
57c1094
e8c2e78
 
 
 
 
 
 
57c1094
 
e8c2e78
 
 
 
57c1094
 
 
 
 
 
 
 
e8c2e78
 
 
 
 
 
 
57c1094
 
 
 
 
 
e8c2e78
57c1094
 
 
 
 
 
e8c2e78
57c1094
 
 
 
 
 
e8c2e78
57c1094
e8c2e78
 
 
2d554b0
 
57c1094
 
2d554b0
57c1094
 
 
 
 
 
2d554b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
import sys
from pathlib import Path
from typing import Any, Dict, Optional, Tuple

import gradio as gr
import numpy as np
from easydict import EasyDict as edict
from omegaconf import OmegaConf

sys.path.append(str(Path(__file__).parents[1]))

from ui.sfm import SfmEngine
from ui.utils import (
    GRADIO_VERSION,
    gen_examples,
    generate_warp_images,
    get_matcher_zoo,
    load_config,
    ransac_zoo,
    run_matching,
    run_ransac,
    send_to_match,
)

DESCRIPTION = """
# Image Matching WebUI
This Space demonstrates [Image Matching WebUI](https://github.com/Vincentqyw/image-matching-webui) by vincent qin. Feel free to play with it, or duplicate to run image matching without a queue!
<br/>
🔎 For more details about supported local features and matchers, please refer to https://github.com/Vincentqyw/image-matching-webui

🚀 All algorithms run on CPU for inference, causing slow speeds and high latency. For faster inference, please download the [source code](https://github.com/Vincentqyw/image-matching-webui) for local deployment.

🐛 Your feedback is valuable to me. Please do not hesitate to report any bugs [here](https://github.com/Vincentqyw/image-matching-webui/issues).
"""

CSS = """
#warning {background-color: #FFCCCB}
.logs_class textarea {font-size: 12px !important}
"""


class ImageMatchingApp:
    def __init__(self, server_name="0.0.0.0", server_port=7860, **kwargs):
        self.server_name = server_name
        self.server_port = server_port
        self.config_path = kwargs.get(
            "config", Path(__file__).parent / "config.yaml"
        )
        self.cfg = load_config(self.config_path)
        self.matcher_zoo = get_matcher_zoo(self.cfg["matcher_zoo"])
        self.app = None
        self.init_interface()
        # print all the keys

    def init_matcher_dropdown(self):
        algos = []
        for k, v in self.cfg["matcher_zoo"].items():
            if v.get("enable", True):
                algos.append(k)
        return algos

    def init_interface(self):
        with gr.Blocks(css=CSS) as self.app:
            with gr.Tab("Image Matching"):
                with gr.Row():
                    with gr.Column(scale=1):
                        gr.Image(
                            str(
                                Path(__file__).parent.parent
                                / "assets/logo.webp"
                            ),
                            elem_id="logo-img",
                            show_label=False,
                            show_share_button=False,
                            show_download_button=False,
                        )
                    with gr.Column(scale=3):
                        gr.Markdown(DESCRIPTION)
                with gr.Row(equal_height=False):
                    with gr.Column():
                        with gr.Row():
                            matcher_list = gr.Dropdown(
                                choices=self.init_matcher_dropdown(),
                                value="disk+lightglue",
                                label="Matching Model",
                                interactive=True,
                            )
                            match_image_src = gr.Radio(
                                (
                                    ["upload", "webcam", "clipboard"]
                                    if GRADIO_VERSION > "3"
                                    else ["upload", "webcam", "canvas"]
                                ),
                                label="Image Source",
                                value="upload",
                            )
                        with gr.Row():
                            input_image0 = gr.Image(
                                label="Image 0",
                                type="numpy",
                                image_mode="RGB",
                                height=300 if GRADIO_VERSION > "3" else None,
                                interactive=True,
                            )
                            input_image1 = gr.Image(
                                label="Image 1",
                                type="numpy",
                                image_mode="RGB",
                                height=300 if GRADIO_VERSION > "3" else None,
                                interactive=True,
                            )

                        with gr.Row():
                            button_reset = gr.Button(value="Reset")
                            button_run = gr.Button(
                                value="Run Match", variant="primary"
                            )

                        with gr.Accordion("Advanced Setting", open=False):
                            with gr.Accordion("Image Setting", open=True):
                                with gr.Row():
                                    image_force_resize_cb = gr.Checkbox(
                                        label="Force Resize",
                                        value=False,
                                        interactive=True,
                                    )
                                    image_setting_height = gr.Slider(
                                        minimum=48,
                                        maximum=2048,
                                        step=16,
                                        label="Image Height",
                                        value=480,
                                        visible=False,
                                    )
                                    image_setting_width = gr.Slider(
                                        minimum=64,
                                        maximum=2048,
                                        step=16,
                                        label="Image Width",
                                        value=640,
                                        visible=False,
                                    )
                            with gr.Accordion("Matching Setting", open=True):
                                with gr.Row():
                                    match_setting_threshold = gr.Slider(
                                        minimum=0.0,
                                        maximum=1,
                                        step=0.001,
                                        label="Match threshold",
                                        value=0.1,
                                    )
                                    match_setting_max_keypoints = gr.Slider(
                                        minimum=10,
                                        maximum=10000,
                                        step=10,
                                        label="Max features",
                                        value=1000,
                                    )
                                # TODO: add line settings
                                with gr.Row():
                                    detect_keypoints_threshold = gr.Slider(
                                        minimum=0,
                                        maximum=1,
                                        step=0.001,
                                        label="Keypoint threshold",
                                        value=0.015,
                                    )
                                    detect_line_threshold = (  # noqa: F841
                                        gr.Slider(
                                            minimum=0.1,
                                            maximum=1,
                                            step=0.01,
                                            label="Line threshold",
                                            value=0.2,
                                        )
                                    )
                                # matcher_lists = gr.Radio(
                                #     ["NN-mutual", "Dual-Softmax"],
                                #     label="Matcher mode",
                                #     value="NN-mutual",
                                # )
                            with gr.Accordion("RANSAC Setting", open=True):
                                with gr.Row(equal_height=False):
                                    ransac_method = gr.Dropdown(
                                        choices=ransac_zoo.keys(),
                                        value=self.cfg["defaults"][
                                            "ransac_method"
                                        ],
                                        label="RANSAC Method",
                                        interactive=True,
                                    )
                                ransac_reproj_threshold = gr.Slider(
                                    minimum=0.0,
                                    maximum=12,
                                    step=0.01,
                                    label="Ransac Reproj threshold",
                                    value=8.0,
                                )
                                ransac_confidence = gr.Slider(
                                    minimum=0.0,
                                    maximum=1,
                                    step=0.00001,
                                    label="Ransac Confidence",
                                    value=self.cfg["defaults"][
                                        "ransac_confidence"
                                    ],
                                )
                                ransac_max_iter = gr.Slider(
                                    minimum=0.0,
                                    maximum=100000,
                                    step=100,
                                    label="Ransac Iterations",
                                    value=self.cfg["defaults"][
                                        "ransac_max_iter"
                                    ],
                                )
                                button_ransac = gr.Button(
                                    value="Rerun RANSAC", variant="primary"
                                )
                            with gr.Accordion("Geometry Setting", open=False):
                                with gr.Row(equal_height=False):
                                    choice_geometry_type = gr.Radio(
                                        ["Fundamental", "Homography"],
                                        label="Reconstruct Geometry",
                                        value=self.cfg["defaults"][
                                            "setting_geometry"
                                        ],
                                    )
                        # image resize
                        image_force_resize_cb.select(
                            fn=self._on_select_force_resize,
                            inputs=image_force_resize_cb,
                            outputs=[image_setting_width, image_setting_height],
                        )
                        # collect inputs
                        state_cache = gr.State({})
                        inputs = [
                            input_image0,
                            input_image1,
                            match_setting_threshold,
                            match_setting_max_keypoints,
                            detect_keypoints_threshold,
                            matcher_list,
                            ransac_method,
                            ransac_reproj_threshold,
                            ransac_confidence,
                            ransac_max_iter,
                            choice_geometry_type,
                            gr.State(self.matcher_zoo),
                            image_force_resize_cb,
                            image_setting_width,
                            image_setting_height,
                        ]

                        # Add some examples
                        with gr.Row():
                            # Example inputs
                            with gr.Accordion(
                                "Open for More: Examples", open=True
                            ):
                                gr.Examples(
                                    examples=gen_examples(),
                                    inputs=inputs,
                                    outputs=[],
                                    fn=run_matching,
                                    cache_examples=False,
                                    label=(
                                        "Examples (click one of the images below to Run"
                                        " Match). Thx: WxBS"
                                    ),
                                )
                        with gr.Accordion("Supported Algorithms", open=False):
                            # add a table of supported algorithms
                            self.display_supported_algorithms()

                    with gr.Column():

                        with gr.Accordion(
                            "Open for More: Keypoints", open=True
                        ):
                            output_keypoints = gr.Image(
                                label="Keypoints", type="numpy"
                            )
                        with gr.Accordion(
                            (
                                "Open for More: Raw Matches"
                                " (Green for good matches, Red for bad)"
                            ),
                            open=False,
                        ):
                            output_matches_raw = gr.Image(
                                label="Raw Matches",
                                type="numpy",
                            )
                        with gr.Accordion(
                            (
                                "Open for More: Ransac Matches"
                                " (Green for good matches, Red for bad)"
                            ),
                            open=True,
                        ):
                            output_matches_ransac = gr.Image(
                                label="Ransac Matches", type="numpy"
                            )
                        with gr.Accordion(
                            "Open for More: Matches Statistics", open=False
                        ):
                            output_pred = gr.File(
                                label="Outputs", elem_id="download"
                            )
                            matches_result_info = gr.JSON(
                                label="Matches Statistics"
                            )
                            matcher_info = gr.JSON(label="Match info")

                        with gr.Accordion(
                            "Open for More: Warped Image", open=True
                        ):
                            output_wrapped = gr.Image(
                                label="Wrapped Pair", type="numpy"
                            )
                            # send to input
                            button_rerun = gr.Button(
                                value="Send to Input Match Pair",
                                variant="primary",
                            )
                            with gr.Accordion(
                                "Open for More: Geometry info", open=False
                            ):
                                geometry_result = gr.JSON(
                                    label="Reconstructed Geometry"
                                )

                    # callbacks
                    match_image_src.change(
                        fn=self.ui_change_imagebox,
                        inputs=match_image_src,
                        outputs=input_image0,
                    )
                    match_image_src.change(
                        fn=self.ui_change_imagebox,
                        inputs=match_image_src,
                        outputs=input_image1,
                    )
                    # collect outputs
                    outputs = [
                        output_keypoints,
                        output_matches_raw,
                        output_matches_ransac,
                        matches_result_info,
                        matcher_info,
                        geometry_result,
                        output_wrapped,
                        state_cache,
                        output_pred,
                    ]
                    # button callbacks
                    button_run.click(
                        fn=run_matching, inputs=inputs, outputs=outputs
                    )
                    # Reset images
                    reset_outputs = [
                        input_image0,
                        input_image1,
                        match_setting_threshold,
                        match_setting_max_keypoints,
                        detect_keypoints_threshold,
                        matcher_list,
                        input_image0,
                        input_image1,
                        match_image_src,
                        output_keypoints,
                        output_matches_raw,
                        output_matches_ransac,
                        matches_result_info,
                        matcher_info,
                        output_wrapped,
                        geometry_result,
                        ransac_method,
                        ransac_reproj_threshold,
                        ransac_confidence,
                        ransac_max_iter,
                        choice_geometry_type,
                        output_pred,
                        image_force_resize_cb,
                    ]
                    button_reset.click(
                        fn=self.ui_reset_state,
                        inputs=None,
                        outputs=reset_outputs,
                    )

                    # run ransac button action
                    button_ransac.click(
                        fn=run_ransac,
                        inputs=[
                            state_cache,
                            choice_geometry_type,
                            ransac_method,
                            ransac_reproj_threshold,
                            ransac_confidence,
                            ransac_max_iter,
                        ],
                        outputs=[
                            output_matches_ransac,
                            matches_result_info,
                            output_wrapped,
                            output_pred,
                        ],
                    )

                    # send warped image to match
                    button_rerun.click(
                        fn=send_to_match,
                        inputs=[state_cache],
                        outputs=[input_image0, input_image1],
                    )

                    # estimate geo
                    choice_geometry_type.change(
                        fn=generate_warp_images,
                        inputs=[
                            input_image0,
                            input_image1,
                            geometry_result,
                            choice_geometry_type,
                        ],
                        outputs=[output_wrapped, geometry_result],
                    )
            with gr.Tab("Structure from Motion(under-dev)"):
                sfm_ui = AppSfmUI(  # noqa: F841
                    {
                        **self.cfg,
                        "matcher_zoo": self.matcher_zoo,
                        "outputs": "experiments/sfm",
                    }
                )
                sfm_ui.call_empty()

    def run(self):
        self.app.queue().launch(
            server_name=self.server_name,
            server_port=self.server_port,
            share=False,
        )

    def ui_change_imagebox(self, choice):
        """
        Updates the image box with the given choice.

        Args:
            choice (list): The list of image sources to be displayed in the image box.

        Returns:
            dict: A dictionary containing the updated value, sources, and type for the image box.
        """
        ret_dict = {
            "value": None,  # The updated value of the image box
            "__type__": "update",  # The type of update for the image box
        }
        if GRADIO_VERSION > "3":
            return {
                **ret_dict,
                "sources": choice,  # The list of image sources to be displayed
            }
        else:
            return {
                **ret_dict,
                "source": choice,  # The list of image sources to be displayed
            }

    def _on_select_force_resize(self, visible: bool = False):
        return gr.update(visible=visible), gr.update(visible=visible)

    def ui_reset_state(
        self,
        *args: Any,
    ) -> Tuple[
        Optional[np.ndarray],
        Optional[np.ndarray],
        float,
        int,
        float,
        str,
        Dict[str, Any],
        Dict[str, Any],
        str,
        Optional[np.ndarray],
        Optional[np.ndarray],
        Optional[np.ndarray],
        Dict[str, Any],
        Dict[str, Any],
        Optional[np.ndarray],
        Dict[str, Any],
        str,
        int,
        float,
        int,
        bool,
    ]:
        """
        Reset the state of the UI.

        Returns:
            tuple: A tuple containing the initial values for the UI state.
        """
        key: str = list(self.matcher_zoo.keys())[
            0
        ]  # Get the first key from matcher_zoo
        # flush_logs()
        return (
            None,  # image0: Optional[np.ndarray]
            None,  # image1: Optional[np.ndarray]
            self.cfg["defaults"][
                "match_threshold"
            ],  # matching_threshold: float
            self.cfg["defaults"]["max_keypoints"],  # max_keypoints: int
            self.cfg["defaults"][
                "keypoint_threshold"
            ],  # keypoint_threshold: float
            key,  # matcher: str
            self.ui_change_imagebox("upload"),  # input image0: Dict[str, Any]
            self.ui_change_imagebox("upload"),  # input image1: Dict[str, Any]
            "upload",  # match_image_src: str
            None,  # keypoints: Optional[np.ndarray]
            None,  # raw matches: Optional[np.ndarray]
            None,  # ransac matches: Optional[np.ndarray]
            {},  # matches result info: Dict[str, Any]
            {},  # matcher config: Dict[str, Any]
            None,  # warped image: Optional[np.ndarray]
            {},  # geometry result: Dict[str, Any]
            self.cfg["defaults"]["ransac_method"],  # ransac_method: str
            self.cfg["defaults"][
                "ransac_reproj_threshold"
            ],  # ransac_reproj_threshold: float
            self.cfg["defaults"][
                "ransac_confidence"
            ],  # ransac_confidence: float
            self.cfg["defaults"]["ransac_max_iter"],  # ransac_max_iter: int
            self.cfg["defaults"]["setting_geometry"],  # geometry: str
            None,  # predictions
            False,
        )

    def display_supported_algorithms(self, style="tab"):
        def get_link(link, tag="Link"):
            return "[{}]({})".format(tag, link) if link is not None else "None"

        data = []
        cfg = self.cfg["matcher_zoo"]
        if style == "md":
            markdown_table = "| Algo. | Conference | Code | Project | Paper |\n"
            markdown_table += (
                "| ----- | ---------- | ---- | ------- | ----- |\n"
            )

            for k, v in cfg.items():
                if not v["info"]["display"]:
                    continue
                github_link = get_link(v["info"]["github"])
                project_link = get_link(v["info"]["project"])
                paper_link = get_link(
                    v["info"]["paper"],
                    (
                        Path(v["info"]["paper"]).name[-10:]
                        if v["info"]["paper"] is not None
                        else "Link"
                    ),
                )

                markdown_table += "{}|{}|{}|{}|{}\n".format(
                    v["info"]["name"],  # display name
                    v["info"]["source"],
                    github_link,
                    project_link,
                    paper_link,
                )
            return gr.Markdown(markdown_table)
        elif style == "tab":
            for k, v in cfg.items():
                if not v["info"].get("display", True):
                    continue
                data.append(
                    [
                        v["info"]["name"],
                        v["info"]["source"],
                        v["info"]["github"],
                        v["info"]["paper"],
                        v["info"]["project"],
                    ]
                )
            tab = gr.Dataframe(
                headers=["Algo.", "Conference", "Code", "Paper", "Project"],
                datatype=["str", "str", "str", "str", "str"],
                col_count=(5, "fixed"),
                value=data,
                # wrap=True,
                # min_width = 1000,
                # height=1000,
            )
            return tab


class AppBaseUI:
    def __init__(self, cfg: Dict[str, Any] = {}):
        self.cfg = OmegaConf.create(cfg)
        self.inputs = edict({})
        self.outputs = edict({})
        self.ui = edict({})

    def _init_ui(self):
        NotImplemented

    def call(self, **kwargs):
        NotImplemented

    def info(self):
        gr.Info("SFM is under construction.")


class AppSfmUI(AppBaseUI):
    def __init__(self, cfg: Dict[str, Any] = None):
        super().__init__(cfg)
        assert "matcher_zoo" in self.cfg
        self.matcher_zoo = self.cfg["matcher_zoo"]
        self.sfm_engine = SfmEngine(cfg)
        self._init_ui()

    def init_retrieval_dropdown(self):
        algos = []
        for k, v in self.cfg["retrieval_zoo"].items():
            if v.get("enable", True):
                algos.append(k)
        return algos

    def _update_options(self, option):
        if option == "sparse":
            return gr.Textbox("sparse", visible=True)
        elif option == "dense":
            return gr.Textbox("dense", visible=True)
        else:
            return gr.Textbox("not set", visible=True)

    def _on_select_custom_params(self, value: bool = False):
        return gr.update(visible=value)

    def _init_ui(self):
        with gr.Row():
            # data settting and camera settings
            with gr.Column():
                self.inputs.input_images = gr.File(
                    label="SfM",
                    interactive=True,
                    file_count="multiple",
                    min_width=300,
                )
                # camera setting
                with gr.Accordion("Camera Settings", open=True):
                    with gr.Column():
                        with gr.Row():
                            with gr.Column():
                                self.inputs.camera_model = gr.Dropdown(
                                    choices=[
                                        "PINHOLE",
                                        "SIMPLE_RADIAL",
                                        "OPENCV",
                                    ],
                                    value="PINHOLE",
                                    label="Camera Model",
                                    interactive=True,
                                )
                            with gr.Column():
                                gr.Checkbox(
                                    label="Shared Params",
                                    value=True,
                                    interactive=True,
                                )
                                camera_custom_params_cb = gr.Checkbox(
                                    label="Custom Params",
                                    value=False,
                                    interactive=True,
                                )
                        with gr.Row():
                            self.inputs.camera_params = gr.Textbox(
                                label="Camera Params",
                                value="0,0,0,0",
                                interactive=False,
                                visible=False,
                            )
                        camera_custom_params_cb.select(
                            fn=self._on_select_custom_params,
                            inputs=camera_custom_params_cb,
                            outputs=self.inputs.camera_params,
                        )

                with gr.Accordion("Matching Settings", open=True):
                    # feature extraction and matching setting
                    with gr.Row():
                        # matcher setting
                        self.inputs.matcher_key = gr.Dropdown(
                            choices=self.matcher_zoo.keys(),
                            value="disk+lightglue",
                            label="Matching Model",
                            interactive=True,
                        )
                    with gr.Row():
                        with gr.Accordion("Advanced Settings", open=False):
                            with gr.Column():
                                with gr.Row():
                                    # matching setting
                                    self.inputs.max_keypoints = gr.Slider(
                                        label="Max Keypoints",
                                        minimum=100,
                                        maximum=10000,
                                        value=1000,
                                        interactive=True,
                                    )
                                    self.inputs.keypoint_threshold = gr.Slider(
                                        label="Keypoint Threshold",
                                        minimum=0,
                                        maximum=1,
                                        value=0.01,
                                    )
                                with gr.Row():
                                    self.inputs.match_threshold = gr.Slider(
                                        label="Match Threshold",
                                        minimum=0.01,
                                        maximum=12.0,
                                        value=0.2,
                                    )
                                    self.inputs.ransac_threshold = gr.Slider(
                                        label="Ransac Threshold",
                                        minimum=0.01,
                                        maximum=12.0,
                                        value=4.0,
                                        step=0.01,
                                        interactive=True,
                                    )

                                with gr.Row():
                                    self.inputs.ransac_confidence = gr.Slider(
                                        label="Ransac Confidence",
                                        minimum=0.01,
                                        maximum=1.0,
                                        value=0.9999,
                                        step=0.0001,
                                        interactive=True,
                                    )
                                    self.inputs.ransac_max_iter = gr.Slider(
                                        label="Ransac Max Iter",
                                        minimum=1,
                                        maximum=100,
                                        value=100,
                                        step=1,
                                        interactive=True,
                                    )
                with gr.Accordion("Scene Graph Settings", open=True):
                    # mapping setting
                    self.inputs.scene_graph = gr.Dropdown(
                        choices=["all", "swin", "oneref"],
                        value="all",
                        label="Scene Graph",
                        interactive=True,
                    )

                    # global feature setting
                    self.inputs.global_feature = gr.Dropdown(
                        choices=self.init_retrieval_dropdown(),
                        value="netvlad",
                        label="Global features",
                        interactive=True,
                    )
                    self.inputs.top_k = gr.Slider(
                        label="Number of Images per Image to Match",
                        minimum=1,
                        maximum=100,
                        value=10,
                        step=1,
                    )
                # button_match = gr.Button("Run Matching", variant="primary")

            # mapping setting
            with gr.Column():
                with gr.Accordion("Mapping Settings", open=True):
                    with gr.Row():
                        with gr.Accordion("Buddle Settings", open=True):
                            with gr.Row():
                                self.inputs.mapper_refine_focal_length = (
                                    gr.Checkbox(
                                        label="Refine Focal Length",
                                        value=False,
                                        interactive=True,
                                    )
                                )
                                self.inputs.mapper_refine_principle_points = (
                                    gr.Checkbox(
                                        label="Refine Principle Points",
                                        value=False,
                                        interactive=True,
                                    )
                                )
                                self.inputs.mapper_refine_extra_params = (
                                    gr.Checkbox(
                                        label="Refine Extra Params",
                                        value=False,
                                        interactive=True,
                                    )
                                )
                    with gr.Accordion("Retriangluation Settings", open=True):
                        gr.Textbox(
                            label="Retriangluation Details",
                        )
                    self.ui.button_sfm = gr.Button("Run SFM", variant="primary")
                self.outputs.model_3d = gr.Model3D(
                    interactive=True,
                )
                self.outputs.output_image = gr.Image(
                    label="SFM Visualize",
                    type="numpy",
                    image_mode="RGB",
                    interactive=False,
                )

    def call_empty(self):
        self.ui.button_sfm.click(fn=self.info, inputs=[], outputs=[])

    def call(self):
        self.ui.button_sfm.click(
            fn=self.sfm_engine.call,
            inputs=[
                self.inputs.matcher_key,
                self.inputs.input_images,  # images
                self.inputs.camera_model,
                self.inputs.camera_params,
                self.inputs.max_keypoints,
                self.inputs.keypoint_threshold,
                self.inputs.match_threshold,
                self.inputs.ransac_threshold,
                self.inputs.ransac_confidence,
                self.inputs.ransac_max_iter,
                self.inputs.scene_graph,
                self.inputs.global_feature,
                self.inputs.top_k,
                self.inputs.mapper_refine_focal_length,
                self.inputs.mapper_refine_principle_points,
                self.inputs.mapper_refine_extra_params,
            ],
            outputs=[self.outputs.model_3d, self.outputs.output_image],
        )