Ankan Ghosh
commited on
Upload 2 files
Browse files- app.py +117 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# from panorama import stitch_images
|
6 |
+
|
7 |
+
|
8 |
+
def process_images(image_files, crop, y_start, y_end, x_start, x_end):
|
9 |
+
if not image_files:
|
10 |
+
return "No images uploaded.", None
|
11 |
+
|
12 |
+
# Extract the first element of each tuple if it's a tuple
|
13 |
+
extracted_images = []
|
14 |
+
for item in image_files:
|
15 |
+
if isinstance(item, tuple):
|
16 |
+
# If item is (img, None), extract 'img' only
|
17 |
+
img = item[0]
|
18 |
+
else:
|
19 |
+
# If for some reason it's not a tuple, assume it's already the image
|
20 |
+
img = item
|
21 |
+
|
22 |
+
# Check if img is not None and is a numpy array
|
23 |
+
if img is not None and isinstance(img, np.ndarray):
|
24 |
+
extracted_images.append(img)
|
25 |
+
|
26 |
+
# If after extraction no valid images found
|
27 |
+
if not extracted_images:
|
28 |
+
return "No valid images found for stitching.", None
|
29 |
+
|
30 |
+
try:
|
31 |
+
# Define crop coordinates
|
32 |
+
if crop:
|
33 |
+
crop_coords = (int(y_start), int(y_end), int(x_start), int(x_end))
|
34 |
+
else:
|
35 |
+
crop_coords = None
|
36 |
+
|
37 |
+
# Initialize OpenCV's Stitcher
|
38 |
+
stitcher = cv2.Stitcher_create()
|
39 |
+
|
40 |
+
# Perform stitching
|
41 |
+
status, panorama = stitcher.stitch(extracted_images)
|
42 |
+
|
43 |
+
if status != cv2.Stitcher_OK:
|
44 |
+
raise RuntimeError(f"Stitching failed with status code {status}.")
|
45 |
+
|
46 |
+
if crop:
|
47 |
+
y_start, y_end, x_start, x_end = crop_coords
|
48 |
+
cropped_panorama = panorama[y_start:y_end, x_start:x_end]
|
49 |
+
return panorama, cropped_panorama
|
50 |
+
|
51 |
+
return panorama, None
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
# Raise a gradio Error to avoid returning image outputs as text
|
55 |
+
raise gr.Error(f"Error during stitching: {str(e)}")
|
56 |
+
|
57 |
+
|
58 |
+
# Define Gradio interface using Blocks for better layout control
|
59 |
+
with gr.Blocks(title="Creating Panorama using OpenCV") as demo:
|
60 |
+
gr.Markdown("# Creating Panorama using OpenCV")
|
61 |
+
gr.Markdown("Upload the series of images sequentially and get the perfect Panorama!")
|
62 |
+
|
63 |
+
with gr.Row(equal_height=True):
|
64 |
+
with gr.Column():
|
65 |
+
image_upload = gr.Gallery(
|
66 |
+
columns=4,
|
67 |
+
label="Upload Images",
|
68 |
+
file_types=["image"],
|
69 |
+
format="jpeg",
|
70 |
+
type="numpy",
|
71 |
+
)
|
72 |
+
crop_checkbox = gr.Checkbox(label="Apply Cropping to Panorama", value=False)
|
73 |
+
with gr.Row():
|
74 |
+
y_start = gr.Number(label="Crop Y Start", value=90, interactive=True)
|
75 |
+
y_end = gr.Number(label="Crop Y End", value=867, interactive=True)
|
76 |
+
with gr.Row():
|
77 |
+
x_start = gr.Number(label="Crop X Start", value=1, interactive=True)
|
78 |
+
x_end = gr.Number(label="Crop X End", value=2000, interactive=True)
|
79 |
+
stitch_button = gr.Button("Stitch Images")
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column():
|
82 |
+
stitched_output = gr.Image(label="Stitched Panorama")
|
83 |
+
cropped_output = gr.Image(label="Cropped Panorama")
|
84 |
+
# examples = [
|
85 |
+
# [["./Apollo-8-Launch.png", "./times_square.jpg"], True, 90, 867, 1, 2000],
|
86 |
+
# ]
|
87 |
+
|
88 |
+
# with gr.Row():
|
89 |
+
# gr.Examples(
|
90 |
+
# examples=examples,
|
91 |
+
# inputs=[image_upload, crop_checkbox, y_start, y_end, x_start, x_end],
|
92 |
+
# label="Load Example Images",
|
93 |
+
# )
|
94 |
+
|
95 |
+
# Disable cropping input fields if checkbox is not selected
|
96 |
+
def toggle_crop_inputs(crop):
|
97 |
+
return gr.update(visible=crop), gr.update(visible=crop), gr.update(visible=crop), gr.update(visible=crop)
|
98 |
+
|
99 |
+
crop_checkbox.change(fn=toggle_crop_inputs, inputs=[crop_checkbox], outputs=[y_start, y_end, x_start, x_end])
|
100 |
+
|
101 |
+
# Define the button click event
|
102 |
+
stitch_button.click(
|
103 |
+
fn=process_images,
|
104 |
+
inputs=[image_upload, crop_checkbox, y_start, y_end, x_start, x_end],
|
105 |
+
outputs=[stitched_output, cropped_output],
|
106 |
+
)
|
107 |
+
|
108 |
+
gr.Markdown(
|
109 |
+
"""
|
110 |
+
**Note**:
|
111 |
+
- Ensure that the uploaded images have overlapping regions for successful stitching.
|
112 |
+
- Cropping coordinates should be within the dimensions of the stitched panorama.
|
113 |
+
"""
|
114 |
+
)
|
115 |
+
|
116 |
+
# Launch the Gradio interface
|
117 |
+
demo.launch(show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
opencv-python==4.10.0.84
|
2 |
+
gradio==5.5.0
|