|
import cv2 |
|
import numpy as np |
|
import gradio as gr |
|
|
|
from pydantic import BaseModel |
|
|
|
|
|
|
|
color = (0, 255, 0) |
|
marker_type = 1 |
|
|
|
with gr.Blocks() as demo: |
|
with gr.Row(): |
|
gr.Markdown('''# Annotate Points!π |
|
Upload an image and click to annotate points on it. |
|
''') |
|
|
|
|
|
with gr.Tab(label='Image'): |
|
with gr.Row(): |
|
with gr.Column(): |
|
|
|
original_image = gr.State(value=None) |
|
input_image = gr.Image(type="numpy", label="Upload Image") |
|
|
|
|
|
selected_points = gr.State([]) |
|
with gr.Row(): |
|
gr.Markdown('Click on the image to select points.') |
|
undo_button = gr.Button('Undo point') |
|
|
|
|
|
with gr.Tab(label='Image+Points'): |
|
output_image = gr.Image(type='numpy') |
|
|
|
|
|
|
|
def store_img(img): |
|
return img, [] |
|
|
|
|
|
input_image.upload(store_img, [input_image], [original_image, selected_points]) |
|
|
|
|
|
|
|
def get_point(img, sel_pix, evt: gr.SelectData): |
|
sel_pix.append(evt.index) |
|
|
|
|
|
for point in sel_pix: |
|
cv2.drawMarker(img, point, color, markerType=marker_type, markerSize=80, thickness=20) |
|
return img if isinstance(img, np.ndarray) else np.array(img) |
|
|
|
|
|
input_image.select(get_point, [input_image, selected_points], [input_image]) |
|
|
|
|
|
|
|
def undo_points(orig_img, sel_pix): |
|
temp = orig_img.copy() |
|
if len(sel_pix) != 0: |
|
sel_pix.pop() |
|
for point in sel_pix: |
|
cv2.drawMarker(temp, point, color, markerType=marker_type, markerSize=20, thickness=5) |
|
return temp if isinstance(temp, np.ndarray) else np.array(temp) |
|
|
|
|
|
undo_button.click(undo_points, [original_image, selected_points], [input_image]) |
|
|
|
|
|
demo.queue().launch(inbrowser=True) |
|
|