ankanpy commited on
Commit
e50d8db
·
1 Parent(s): 68f3403

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -27
app.py CHANGED
@@ -7,7 +7,7 @@ from pathlib import Path
7
  from billboard import apply_homography_and_warp
8
 
9
 
10
- # Define a function to select points on the image (similar to your `get_point_interface`)
11
  def get_point_interface(original_frame, points, evt: gr.SelectData):
12
  x, y = evt.index
13
  if points is None:
@@ -16,14 +16,14 @@ def get_point_interface(original_frame, points, evt: gr.SelectData):
16
  points.append((x, y))
17
 
18
  # Draw the points and lines on the image
19
- image = original_frame.copy()
20
  for pt in points:
21
- cv2.circle(image, pt, 5, (255, 0, 0), -1)
22
  if len(points) > 1:
23
  for i in range(len(points) - 1):
24
- cv2.line(image, points[i], points[i + 1], (255, 0, 0), 2)
25
 
26
- return image, points
27
 
28
 
29
  # Function to process and apply homography (this will call your existing function)
@@ -44,25 +44,39 @@ def process_images(source_image, dest_image, roi_points):
44
 
45
 
46
  # Gradio interface setup
47
- with gr.Blocks(title="Homography Warping App") as demo:
48
- gr.Markdown("# Homography Warping App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  gr.Markdown(
50
  "Upload two images (source and destination), select the four points for ROI on the destination image, and click 'Process Images' to warp the source image onto the destination."
51
  )
52
-
53
- with gr.Row(equal_height=True):
54
- source_image_input = gr.Image(label="Upload Source Image")
55
- dest_image_input = gr.Image(label="Upload Destination Image")
56
-
57
- with gr.Column():
58
- dest_image_roi = gr.Image(label="Click to select ROI points on destination image")
59
- original_frame_state = gr.State(None)
60
- selected_points = gr.State([])
61
- clear_points_button = gr.Button("Clear Points")
62
- process_button = gr.Button("Process Images")
63
-
64
  with gr.Row():
65
- output_image = gr.Image(label="Wrapped Image")
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  examples = [
68
  ["./images/Apollo-8-Launch.png", "./images/times_square.jpg"],
@@ -71,29 +85,27 @@ with gr.Blocks(title="Homography Warping App") as demo:
71
  with gr.Row():
72
  gr.Examples(
73
  examples=examples,
74
- inputs=[source_image_input, dest_image_roi],
75
  label="Load Example Images",
76
  )
77
 
78
  dest_image_input.upload(
79
- lambda img: img, inputs=dest_image_input, outputs=dest_image_roi # Simply return the uploaded image
80
  )
81
 
82
  # Adding the ROI point selection functionality
83
- dest_image_roi.select(
84
- get_point_interface, inputs=[dest_image_roi, selected_points], outputs=[dest_image_roi, selected_points]
85
- )
86
 
87
  # Clear the selected points
88
  clear_points_button.click(
89
  fn=lambda original_frame: (original_frame, []),
90
  inputs=dest_image_input,
91
- outputs=[dest_image_roi, selected_points],
92
  )
93
 
94
  # Callback for processing the images
95
  process_button.click(
96
- process_images, inputs=[source_image_input, dest_image_roi, selected_points], outputs=output_image
97
  )
98
 
99
 
 
7
  from billboard import apply_homography_and_warp
8
 
9
 
10
+ # Function to select points on the image
11
  def get_point_interface(original_frame, points, evt: gr.SelectData):
12
  x, y = evt.index
13
  if points is None:
 
16
  points.append((x, y))
17
 
18
  # Draw the points and lines on the image
19
+ roi_image = original_frame.copy()
20
  for pt in points:
21
+ cv2.circle(roi_image, pt, 5, (255, 0, 0), -1)
22
  if len(points) > 1:
23
  for i in range(len(points) - 1):
24
+ cv2.line(roi_image, points[i], points[i + 1], (255, 0, 0), 2)
25
 
26
+ return roi_image, points
27
 
28
 
29
  # Function to process and apply homography (this will call your existing function)
 
44
 
45
 
46
  # Gradio interface setup
47
+ with gr.Blocks(title="Homography Warping App", theme=gr.themes.Soft()) as demo:
48
+
49
+ gr.HTML(
50
+ """
51
+ <h1 style='text-align: center'>
52
+ Homography Warping App
53
+ </h1>
54
+ """
55
+ )
56
+ gr.HTML(
57
+ """
58
+ <h3 style='text-align: center'>
59
+ <a href='https://opencv.org/university/' target='_blank'>OpenCV Courses</a> | <a href='https://github.com/OpenCV-University' target='_blank'>Github</a>
60
+ </h3>
61
+ """
62
+ )
63
  gr.Markdown(
64
  "Upload two images (source and destination), select the four points for ROI on the destination image, and click 'Process Images' to warp the source image onto the destination."
65
  )
66
+
 
 
 
 
 
 
 
 
 
 
 
67
  with gr.Row():
68
+ with gr.Column(scale=1):
69
+ with gr.Column():
70
+ source_image_input = gr.Image(label="Upload Source Image")
71
+ dest_image_input = gr.Image(label="Upload Destination Image")
72
+ with gr.Column(scale=1):
73
+ dest_image_copy = gr.Image(label="Select ROI points on destination image")
74
+ original_frame_state = gr.State(None)
75
+ selected_points = gr.State([])
76
+ clear_points_button = gr.Button("Clear Points")
77
+ process_button = gr.Button("Process Images")
78
+ with gr.Column(scale=1):
79
+ output_image = gr.Image(label="Wrapped Image")
80
 
81
  examples = [
82
  ["./images/Apollo-8-Launch.png", "./images/times_square.jpg"],
 
85
  with gr.Row():
86
  gr.Examples(
87
  examples=examples,
88
+ inputs=[source_image_input, dest_image_input],
89
  label="Load Example Images",
90
  )
91
 
92
  dest_image_input.upload(
93
+ lambda img:img, inputs=dest_image_input, outputs=dest_image_copy # Simply return the uploaded image
94
  )
95
 
96
  # Adding the ROI point selection functionality
97
+ dest_image_copy.select(get_point_interface, inputs=[dest_image_copy, selected_points], outputs=[dest_image_copy, selected_points])
 
 
98
 
99
  # Clear the selected points
100
  clear_points_button.click(
101
  fn=lambda original_frame: (original_frame, []),
102
  inputs=dest_image_input,
103
+ outputs=[dest_image_input, selected_points],
104
  )
105
 
106
  # Callback for processing the images
107
  process_button.click(
108
+ process_images, inputs=[source_image_input, dest_image_input, selected_points], outputs=output_image
109
  )
110
 
111