|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
from pathlib import Path |
|
|
|
|
|
from billboard import apply_homography_and_warp |
|
|
|
|
|
|
|
def get_point_interface(original_frame, points, evt: gr.SelectData): |
|
x, y = evt.index |
|
if points is None: |
|
points = [] |
|
points = points.copy() |
|
points.append((x, y)) |
|
|
|
|
|
image = original_frame.copy() |
|
for pt in points: |
|
cv2.circle(image, pt, 5, (255, 0, 0), -1) |
|
if len(points) > 1: |
|
for i in range(len(points) - 1): |
|
cv2.line(image, points[i], points[i + 1], (255, 0, 0), 2) |
|
|
|
return image, points |
|
|
|
|
|
|
|
def process_images(source_image, dest_image, roi_points): |
|
|
|
if len(roi_points) != 4: |
|
return "Error: Please select exactly four points." |
|
|
|
|
|
img_src = np.array(source_image) |
|
img_dst = np.array(dest_image) |
|
|
|
|
|
result_img = apply_homography_and_warp(img_src, img_dst, np.array(roi_points)) |
|
|
|
|
|
return result_img |
|
|
|
|
|
|
|
with gr.Blocks(title="Homography Warping App") as demo: |
|
gr.Markdown("# Homography Warping App") |
|
gr.Markdown( |
|
"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." |
|
) |
|
|
|
with gr.Row(equal_height=True): |
|
source_image_input = gr.Image(label="Upload Source Image") |
|
dest_image_input = gr.Image(label="Upload Destination Image") |
|
|
|
with gr.Column(): |
|
dest_image_roi = gr.Image(label="Click to select ROI points on destination image") |
|
original_frame_state = gr.State(None) |
|
selected_points = gr.State([]) |
|
clear_points_button = gr.Button("Clear Points") |
|
process_button = gr.Button("Process Images") |
|
|
|
with gr.Row(): |
|
output_image = gr.Image(label="Wrapped Image") |
|
|
|
examples = [ |
|
["./images/Apollo-8-Launch.png", "./images/times_square.jpg"], |
|
] |
|
|
|
with gr.Row(): |
|
gr.Examples( |
|
examples=examples, |
|
inputs=[source_image_input, dest_image_roi], |
|
label="Load Example Images", |
|
) |
|
|
|
dest_image_input.upload( |
|
lambda img: img, inputs=dest_image_input, outputs=dest_image_roi |
|
) |
|
|
|
|
|
dest_image_roi.select( |
|
get_point_interface, inputs=[dest_image_roi, selected_points], outputs=[dest_image_roi, selected_points] |
|
) |
|
|
|
|
|
clear_points_button.click( |
|
fn=lambda original_frame: (original_frame, []), |
|
inputs=dest_image_input, |
|
outputs=[dest_image_roi, selected_points], |
|
) |
|
|
|
|
|
process_button.click( |
|
process_images, inputs=[source_image_input, dest_image_roi, selected_points], outputs=output_image |
|
) |
|
|
|
|
|
|
|
demo.launch() |
|
|