gokaygokay commited on
Commit
c1fa1e5
1 Parent(s): b22dc2b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from stitching import Stitcher
3
+ import cv2
4
+ import numpy as np
5
+
6
+ def stitch_images(image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct):
7
+ if image_mode == "Two Images":
8
+ if image1 is None or image2 is None:
9
+ return None, "Please upload both images."
10
+ cv_images = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in [image1, image2]]
11
+ else: # Multiple Images
12
+ if len(images) < 2:
13
+ return None, "Please upload at least 2 images."
14
+ cv_images = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in images]
15
+
16
+ try:
17
+ stitcher = Stitcher(
18
+ detector=detector,
19
+ matcher_type=matcher,
20
+ estimator=estimator,
21
+ blender_type=blender_type,
22
+ crop=crop,
23
+ wave_correct_kind=wave_correct
24
+ )
25
+ panorama = stitcher.stitch(cv_images)
26
+
27
+ # Convert back to RGB for display
28
+ panorama_rgb = cv2.cvtColor(panorama, cv2.COLOR_BGR2RGB)
29
+ return panorama_rgb, "Stitching successful!"
30
+ except Exception as e:
31
+ return None, f"Stitching failed: {str(e)}"
32
+
33
+ def update_image_input(choice):
34
+ if choice == "Two Images":
35
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)
36
+ else:
37
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
38
+
39
+ with gr.Blocks() as iface:
40
+ gr.Markdown("# Advanced Image Stitcher")
41
+ gr.Markdown("Upload images to create a panorama. Adjust parameters for better results.")
42
+
43
+ with gr.Row():
44
+ image_mode = gr.Radio(["Two Images", "Multiple Images"], label="Image Upload Mode", value="Two Images")
45
+
46
+ with gr.Row():
47
+ image1 = gr.Image(label="Image 1")
48
+ image2 = gr.Image(label="Image 2")
49
+ images = gr.File(file_count="multiple", label="Upload multiple images", visible=False)
50
+
51
+ image_mode.change(update_image_input, image_mode, [image1, image2, images])
52
+
53
+ with gr.Row():
54
+ detector = gr.Dropdown(["orb", "sift", "surf", "akaze"], label="Feature Detector", value="orb")
55
+ matcher = gr.Dropdown(["homography", "affine"], label="Matcher Type", value="homography")
56
+ estimator = gr.Dropdown(["homography", "affine"], label="Estimator", value="homography")
57
+
58
+ with gr.Row():
59
+ blender_type = gr.Dropdown(["multiband", "feather", "no"], label="Blender Type", value="multiband")
60
+ crop = gr.Checkbox(label="Crop Result", value=True)
61
+ wave_correct = gr.Radio(["horiz", "vert", "no"], label="Wave Correction", value="horiz")
62
+
63
+ stitch_button = gr.Button("Stitch Images")
64
+
65
+ with gr.Row():
66
+ output_image = gr.Image(type="numpy", label="Stitched Panorama")
67
+ status = gr.Textbox(label="Status")
68
+
69
+ stitch_button.click(
70
+ stitch_images,
71
+ inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
72
+ outputs=[output_image, status]
73
+ )
74
+
75
+ gr.Examples(
76
+ [
77
+ ["Two Images", "exposure_error_1.jpg", "exposure_error_2.jpg", None, "orb", "homography", "homography", "multiband", True, "horiz"],
78
+ ["Multiple Images", None, None, ["weir_1.jpg", "weir_2.jpg", "weir_3.jpg"], "orb", "homography", "homography", "multiband", True, "horiz"]
79
+ ],
80
+ inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
81
+ )
82
+
83
+ iface.launch()