Upload 10 files
Browse files- .gitattributes +8 -0
- app.py +120 -0
- requirements.txt +2 -0
- scene/scene1.jpg +3 -0
- scene/scene2.jpg +3 -0
- scene/scene3.jpg +3 -0
- scene/scene4.jpg +3 -0
- scene/scene5.jpg +3 -0
- scene/scene6.jpg +3 -0
- scene/scene7.jpg +3 -0
- scene/scene8.jpg +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,11 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
scene/scene1.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
scene/scene2.jpg filter=lfs diff=lfs merge=lfs -text
|
38 |
+
scene/scene3.jpg filter=lfs diff=lfs merge=lfs -text
|
39 |
+
scene/scene4.jpg filter=lfs diff=lfs merge=lfs -text
|
40 |
+
scene/scene5.jpg filter=lfs diff=lfs merge=lfs -text
|
41 |
+
scene/scene6.jpg filter=lfs diff=lfs merge=lfs -text
|
42 |
+
scene/scene7.jpg filter=lfs diff=lfs merge=lfs -text
|
43 |
+
scene/scene8.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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", theme=gr.themes.Soft()) 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 |
+
|
65 |
+
examples = [
|
66 |
+
"./scene/scene1.jpg",
|
67 |
+
"./scene/scene2.jpg",
|
68 |
+
"./scene/scene3.jpg",
|
69 |
+
"./scene/scene4.jpg",
|
70 |
+
"./scene/scene5.jpg",
|
71 |
+
"./scene/scene6.jpg",
|
72 |
+
"./scene/scene7.jpg",
|
73 |
+
"./scene/scene8.jpg",
|
74 |
+
]
|
75 |
+
with gr.Column():
|
76 |
+
image_upload = gr.Gallery(
|
77 |
+
value=examples,
|
78 |
+
columns=4,
|
79 |
+
label="Upload Images",
|
80 |
+
file_types=["image"],
|
81 |
+
format="jpeg",
|
82 |
+
type="numpy",
|
83 |
+
)
|
84 |
+
crop_checkbox = gr.Checkbox(label="Apply Cropping to Panorama", value=False)
|
85 |
+
with gr.Row():
|
86 |
+
y_start = gr.Number(label="Crop Y Start", value=90, interactive=True)
|
87 |
+
y_end = gr.Number(label="Crop Y End", value=867, interactive=True)
|
88 |
+
with gr.Row():
|
89 |
+
x_start = gr.Number(label="Crop X Start", value=1, interactive=True)
|
90 |
+
x_end = gr.Number(label="Crop X End", value=2000, interactive=True)
|
91 |
+
stitch_button = gr.Button("Stitch Images")
|
92 |
+
with gr.Row():
|
93 |
+
with gr.Column():
|
94 |
+
stitched_output = gr.Image(label="Stitched Panorama")
|
95 |
+
cropped_output = gr.Image(label="Cropped Panorama")
|
96 |
+
|
97 |
+
|
98 |
+
# Disable cropping input fields if checkbox is not selected
|
99 |
+
def toggle_crop_inputs(crop):
|
100 |
+
return gr.update(visible=crop), gr.update(visible=crop), gr.update(visible=crop), gr.update(visible=crop)
|
101 |
+
|
102 |
+
crop_checkbox.change(fn=toggle_crop_inputs, inputs=[crop_checkbox], outputs=[y_start, y_end, x_start, x_end])
|
103 |
+
|
104 |
+
# Define the button click event
|
105 |
+
stitch_button.click(
|
106 |
+
fn=process_images,
|
107 |
+
inputs=[image_upload, crop_checkbox, y_start, y_end, x_start, x_end],
|
108 |
+
outputs=[stitched_output, cropped_output],
|
109 |
+
)
|
110 |
+
|
111 |
+
gr.Markdown(
|
112 |
+
"""
|
113 |
+
**Note**:
|
114 |
+
- Ensure that the uploaded images have overlapping regions for successful stitching.
|
115 |
+
- Cropping coordinates should be within the dimensions of the stitched panorama.
|
116 |
+
"""
|
117 |
+
)
|
118 |
+
|
119 |
+
# Launch the Gradio interface
|
120 |
+
demo.launch(show_error=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
opencv-python==4.10.0.84
|
2 |
+
gradio
|
scene/scene1.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene2.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene3.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene4.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene5.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene6.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene7.jpg
ADDED
![]() |
Git LFS Details
|
scene/scene8.jpg
ADDED
![]() |
Git LFS Details
|