Ankan Ghosh
commited on
Upload 4 files
Browse files- app.py +101 -0
- images/Apollo-8-Launch.png +0 -0
- images/times_square.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# Import the existing homography and warping function from your existing script
|
| 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:
|
| 14 |
+
points = []
|
| 15 |
+
points = points.copy() # Make a copy to avoid modifying in-place
|
| 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)
|
| 30 |
+
def process_images(source_image, dest_image, roi_points):
|
| 31 |
+
# Ensure that exactly four points are selected
|
| 32 |
+
if len(roi_points) != 4:
|
| 33 |
+
return "Error: Please select exactly four points."
|
| 34 |
+
|
| 35 |
+
# Convert images to correct format (if not already in uint8)
|
| 36 |
+
img_src = np.array(source_image)
|
| 37 |
+
img_dst = np.array(dest_image)
|
| 38 |
+
|
| 39 |
+
# Apply homography and get the warped image
|
| 40 |
+
result_img = apply_homography_and_warp(img_src, img_dst, np.array(roi_points))
|
| 41 |
+
|
| 42 |
+
# Return the processed image
|
| 43 |
+
return result_img
|
| 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"],
|
| 69 |
+
]
|
| 70 |
+
|
| 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 |
+
|
| 100 |
+
# Launch the Gradio app
|
| 101 |
+
demo.launch()
|
images/Apollo-8-Launch.png
ADDED
|
images/times_square.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
opencv-python==4.10.0.84
|
| 2 |
+
gradio==5.5.0
|