| import cv2 |
| import gradio as gr |
| import numpy as np |
|
|
| |
| arrows = [] |
| start_point = None |
|
|
|
|
| |
| def draw_arrow(image, click_coords): |
| global start_point, arrows |
|
|
| |
| img_array = np.array(image, dtype=np.uint8) |
|
|
| |
| current_point = (int(click_coords[0]), int(click_coords[1])) |
|
|
| |
| if start_point is None: |
| start_point = current_point |
| return img_array |
|
|
| |
| end_point = current_point |
| arrows.append((start_point, end_point)) |
|
|
| |
| start_point = None |
|
|
| |
| for arrow in arrows: |
| start, end = arrow |
| color = (0, 255, 0) |
| thickness = 3 |
|
|
| |
| cv2.arrowedLine(img_array, start, end, color, thickness) |
|
|
| return img_array |
|
|
|
|
| |
| def reset_canvas(): |
| global arrows, start_point |
| arrows = [] |
| start_point = None |
| return load_image() |
|
|
|
|
| |
| def load_image(): |
| img = np.ones((400, 400, 3), dtype=np.uint8) * 255 |
| return img |
|
|
|
|
| |
| def interactive_arrow_interface(): |
| with gr.Blocks() as demo: |
| image_input = gr.Image(value=load_image(), interactive=True, |
| label="Click to specify the arrow's start and end points") |
| output_image = gr.Image(label="Image with Arrows") |
| reset_button = gr.Button("Reset") |
|
|
| |
| def handle_click(image, evt: gr.SelectData): |
| print(f"Click coordinates: {evt.index}") |
| |
| updated_image = draw_arrow(image, (evt.index[0], evt.index[1])) |
| return updated_image |
|
|
| image_input.select(handle_click, [image_input], output_image) |
|
|
| |
| reset_button.click(fn=reset_canvas, inputs=None, outputs=image_input) |
|
|
| return demo |
|
|
|
|
| |
| interactive_arrow_interface().launch(inbrowser=True) |
|
|