File size: 3,630 Bytes
c1fa1e5
 
 
 
 
39441f8
c1fa1e5
 
ae3c185
c1fa1e5
 
b4eede5
ae3c185
b4eede5
322f851
b4eede5
 
ae3c185
b4eede5
c1fa1e5
 
 
 
 
 
 
 
 
 
 
 
 
 
ae3c185
c1fa1e5
ae3c185
c1fa1e5
 
 
 
 
 
 
1e56a95
 
 
 
 
 
 
c1fa1e5
 
 
 
 
0bdaad7
 
c1fa1e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae3c185
c1fa1e5
 
 
 
ae3c185
c1fa1e5
 
 
ae3c185
c1fa1e5
 
 
 
ae3c185
 
 
c1fa1e5
 
 
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
import gradio as gr
from stitching import Stitcher
import cv2
import numpy as np

def stitch_images(image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct):
    if image_mode == "Two Images":
        if image1 is None or image2 is None:
            return None
        cv_images = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in [image1, image2]]
    else:  # Multiple Images
        if not images or len(images) < 2:
            return None
        cv_images = []
        for img_path in images:
            img = cv2.imread(img_path.name)
            if img is None:
                return None
            cv_images.append(img)
    
    try:
        stitcher = Stitcher(
            detector=detector,
            matcher_type=matcher,
            estimator=estimator,
            blender_type=blender_type,
            crop=crop,
            wave_correct_kind=wave_correct
        )
        panorama = stitcher.stitch(cv_images)
        
        # Convert back to RGB for display
        panorama_rgb = cv2.cvtColor(panorama, cv2.COLOR_BGR2RGB)
        return panorama_rgb
    except Exception as e:
        return None

def update_image_input(choice):
    if choice == "Two Images":
        return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
    else:
        return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)

title = """<h1 align="center">Stitching</h1>
<p><center>"Upload images to create a panorama. Adjust parameters for better results."</center></p>
"""

with gr.Blocks(theme='bethecloud/storj_theme') as iface:
    
    gr.HTML(title)
    
    with gr.Row():
        image_mode = gr.Radio(["Two Images", "Multiple Images"], label="Image Upload Mode", value="Two Images")
    
    with gr.Row():
        image1 = gr.Image(label="Image 1", height=300)
        image2 = gr.Image(label="Image 2", height=300)
        images = gr.File(file_count="multiple", label="Upload multiple images", visible=False)
    
    image_mode.change(update_image_input, image_mode, [image1, image2, images])
    
    with gr.Row():
        detector = gr.Dropdown(["orb", "sift", "surf", "akaze"], label="Feature Detector", value="orb")
        matcher = gr.Dropdown(["homography", "affine"], label="Matcher Type", value="homography")
        estimator = gr.Dropdown(["homography", "affine"], label="Estimator", value="homography")
    
    with gr.Row():
        blender_type = gr.Dropdown(["multiband", "feather", "no"], label="Blender Type", value="multiband")
        crop = gr.Checkbox(label="Crop Result", value=True)
        wave_correct = gr.Radio(["horiz", "vert", "no"], label="Wave Correction", value="horiz")
    
    stitch_button = gr.Button("Stitch Images")
    
    output_image = gr.Image(type="numpy", label="Stitched Panorama")
    
    stitch_button.click(
        stitch_images,
        inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
        outputs=output_image
    )
    
    gr.Examples(
        examples=[
            ["Two Images", "exposure_error_1.jpg", "exposure_error_2.jpg", None, "orb", "homography", "homography", "multiband", True, "horiz"],
            ["Multiple Images", None, None, ["weir_1.jpg", "weir_2.jpg", "weir_3.jpg"], "orb", "homography", "homography", "multiband", True, "horiz"]
        ],
        inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
        outputs=output_image,
        fn=stitch_images,
        cache_examples=True,
    )

iface.launch()