gokaygokay commited on
Commit
ae3c185
1 Parent(s): 39241a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -6,16 +6,16 @@ import numpy as np
6
  def stitch_images(image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct, progress=gr.Progress(track_tqdm=True)):
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 not images or len(images) < 2:
13
- return None, "Please upload at least 2 images."
14
  cv_images = []
15
- for img_path in images:
16
  img = cv2.imread(img_path.name)
17
  if img is None:
18
- return None, f"Failed to read image: {img_path.name}"
19
  cv_images.append(img)
20
 
21
  try:
@@ -27,13 +27,14 @@ def stitch_images(image_mode, image1, image2, images, detector, matcher, estimat
27
  crop=crop,
28
  wave_correct_kind=wave_correct
29
  )
 
30
  panorama = stitcher.stitch(cv_images)
31
 
32
  # Convert back to RGB for display
33
  panorama_rgb = cv2.cvtColor(panorama, cv2.COLOR_BGR2RGB)
34
- return panorama_rgb, "Stitching successful!"
35
  except Exception as e:
36
- return None, f"Stitching failed: {str(e)}"
37
 
38
  def update_image_input(choice):
39
  if choice == "Two Images":
@@ -67,22 +68,23 @@ with gr.Blocks() as iface:
67
 
68
  stitch_button = gr.Button("Stitch Images")
69
 
70
- with gr.Row():
71
- output_image = gr.Image(type="numpy", label="Stitched Panorama")
72
 
73
  stitch_button.click(
74
  stitch_images,
75
  inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
76
- outputs=[output_image]
77
  )
78
 
79
  gr.Examples(
80
- [
81
  ["Two Images", "exposure_error_1.jpg", "exposure_error_2.jpg", None, "orb", "homography", "homography", "multiband", True, "horiz"],
82
  ["Multiple Images", None, None, ["weir_1.jpg", "weir_2.jpg", "weir_3.jpg"], "orb", "homography", "homography", "multiband", True, "horiz"]
83
  ],
84
  inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
85
- cache_examples = True,
 
 
86
  )
87
 
88
  iface.launch()
 
6
  def stitch_images(image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct, progress=gr.Progress(track_tqdm=True)):
7
  if image_mode == "Two Images":
8
  if image1 is None or image2 is None:
9
+ return None
10
  cv_images = [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in [image1, image2]]
11
  else: # Multiple Images
12
  if not images or len(images) < 2:
13
+ return None
14
  cv_images = []
15
+ for img_path in progress.tqdm(images, desc="Loading images"):
16
  img = cv2.imread(img_path.name)
17
  if img is None:
18
+ return None
19
  cv_images.append(img)
20
 
21
  try:
 
27
  crop=crop,
28
  wave_correct_kind=wave_correct
29
  )
30
+ progress(0.5, desc="Stitching images")
31
  panorama = stitcher.stitch(cv_images)
32
 
33
  # Convert back to RGB for display
34
  panorama_rgb = cv2.cvtColor(panorama, cv2.COLOR_BGR2RGB)
35
+ return panorama_rgb
36
  except Exception as e:
37
+ return None
38
 
39
  def update_image_input(choice):
40
  if choice == "Two Images":
 
68
 
69
  stitch_button = gr.Button("Stitch Images")
70
 
71
+ output_image = gr.Image(type="numpy", label="Stitched Panorama")
 
72
 
73
  stitch_button.click(
74
  stitch_images,
75
  inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
76
+ outputs=output_image
77
  )
78
 
79
  gr.Examples(
80
+ examples=[
81
  ["Two Images", "exposure_error_1.jpg", "exposure_error_2.jpg", None, "orb", "homography", "homography", "multiband", True, "horiz"],
82
  ["Multiple Images", None, None, ["weir_1.jpg", "weir_2.jpg", "weir_3.jpg"], "orb", "homography", "homography", "multiband", True, "horiz"]
83
  ],
84
  inputs=[image_mode, image1, image2, images, detector, matcher, estimator, blender_type, crop, wave_correct],
85
+ outputs=output_image,
86
+ fn=stitch_images,
87
+ cache_examples=True,
88
  )
89
 
90
  iface.launch()