Ayesha352 commited on
Commit
438fcec
·
verified ·
1 Parent(s): 6c99f79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -93,6 +93,7 @@ def get_bounding_box_points(json_data):
93
  def process_and_plot_all_detectors(image1_np, image2_np, json_file):
94
  """
95
  Processes the images with all available detectors and returns image data for display and download.
 
96
  """
97
  if image1_np is None or image2_np is None:
98
  return [None] * 6
@@ -114,29 +115,34 @@ def process_and_plot_all_detectors(image1_np, image2_np, json_file):
114
  if H is not None:
115
  box_points = get_bounding_box_points(data)
116
 
117
- output_flat_img = image1_np.copy()
 
118
  cv2.polylines(output_flat_img, [np.int32(box_points)], isClosed=True, color=(0, 0, 255), thickness=5)
119
 
120
  transformed_box_points = cv2.perspectiveTransform(box_points, H)
121
 
122
- output_perspective_img = image2_np.copy()
123
  cv2.polylines(output_perspective_img, [np.int32(transformed_box_points)], isClosed=True, color=(0, 0, 255), thickness=5)
124
 
 
 
 
 
 
125
  fig, axes = plt.subplots(1, 3, figsize=(18, 6))
126
- axes[0].imshow(cv2.cvtColor(output_flat_img, cv2.COLOR_BGR2RGB))
127
  axes[0].set_title(f'Original (Flat) - {detector_type}')
128
  axes[0].axis('off')
129
 
130
- axes[1].imshow(cv2.cvtColor(image2_np, cv2.COLOR_BGR2RGB))
131
  axes[1].set_title('Original (Perspective)')
132
  axes[1].axis('off')
133
 
134
- axes[2].imshow(cv2.cvtColor(output_perspective_img, cv2.COLOR_BGR2RGB))
135
  axes[2].set_title('Projected Bounding Box')
136
  axes[2].axis('off')
137
 
138
  plt.tight_layout()
139
-
140
  file_name = f"result_{detector_type.lower()}.png"
141
  plt.savefig(file_name)
142
  plt.close(fig)
@@ -145,11 +151,11 @@ def process_and_plot_all_detectors(image1_np, image2_np, json_file):
145
  download_files[i] = file_name
146
  else:
147
  print(f"Warning: Homography matrix could not be found with {detector_type} detector. Skipping this result.")
148
- # We don't append None to the gallery_images list to avoid the error.
149
- # download_files[i] remains None, which is handled correctly by gr.File.
150
-
151
  return [gallery_images] + download_files
152
 
 
153
  iface = gr.Interface(
154
  fn=process_and_plot_all_detectors,
155
  inputs=[
 
93
  def process_and_plot_all_detectors(image1_np, image2_np, json_file):
94
  """
95
  Processes the images with all available detectors and returns image data for display and download.
96
+ Keeps original RGB colors intact.
97
  """
98
  if image1_np is None or image2_np is None:
99
  return [None] * 6
 
115
  if H is not None:
116
  box_points = get_bounding_box_points(data)
117
 
118
+ # Convert RGB → BGR for OpenCV drawing
119
+ output_flat_img = cv2.cvtColor(image1_np, cv2.COLOR_RGB2BGR)
120
  cv2.polylines(output_flat_img, [np.int32(box_points)], isClosed=True, color=(0, 0, 255), thickness=5)
121
 
122
  transformed_box_points = cv2.perspectiveTransform(box_points, H)
123
 
124
+ output_perspective_img = cv2.cvtColor(image2_np, cv2.COLOR_RGB2BGR)
125
  cv2.polylines(output_perspective_img, [np.int32(transformed_box_points)], isClosed=True, color=(0, 0, 255), thickness=5)
126
 
127
+ # Convert BGR → RGB for display
128
+ output_flat_img = cv2.cvtColor(output_flat_img, cv2.COLOR_BGR2RGB)
129
+ output_perspective_img = cv2.cvtColor(output_perspective_img, cv2.COLOR_BGR2RGB)
130
+
131
+ # Plot images side by side
132
  fig, axes = plt.subplots(1, 3, figsize=(18, 6))
133
+ axes[0].imshow(output_flat_img)
134
  axes[0].set_title(f'Original (Flat) - {detector_type}')
135
  axes[0].axis('off')
136
 
137
+ axes[1].imshow(image2_np) # original perspective image in RGB
138
  axes[1].set_title('Original (Perspective)')
139
  axes[1].axis('off')
140
 
141
+ axes[2].imshow(output_perspective_img)
142
  axes[2].set_title('Projected Bounding Box')
143
  axes[2].axis('off')
144
 
145
  plt.tight_layout()
 
146
  file_name = f"result_{detector_type.lower()}.png"
147
  plt.savefig(file_name)
148
  plt.close(fig)
 
151
  download_files[i] = file_name
152
  else:
153
  print(f"Warning: Homography matrix could not be found with {detector_type} detector. Skipping this result.")
154
+ # download_files[i] remains None
155
+
 
156
  return [gallery_images] + download_files
157
 
158
+
159
  iface = gr.Interface(
160
  fn=process_and_plot_all_detectors,
161
  inputs=[