Spaces:
Runtime error
Runtime error
Commit
·
af2e4f1
1
Parent(s):
002247f
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,90 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Define the
|
| 6 |
-
|
| 7 |
-
# Load the image and create a drawing context
|
| 8 |
-
img = Image.fromarray(input_image.astype('uint8'), 'RGB')
|
| 9 |
-
draw = ImageDraw.Draw(img)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Define the
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Define the
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
]
|
| 46 |
|
| 47 |
-
|
| 48 |
-
description='Annotate images using bounding boxes').launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import csv
|
| 3 |
+
from io import StringIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
import base64
|
| 7 |
|
| 8 |
+
# Define the annotation types
|
| 9 |
+
ANNOTATION_TYPES = ['rect', 'circle']
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Define the Annotation class
|
| 12 |
+
class Annotation:
|
| 13 |
+
def __init__(self, x, y, width, height, annotation_type):
|
| 14 |
+
self.x = x
|
| 15 |
+
self.y = y
|
| 16 |
+
self.width = width
|
| 17 |
+
self.height = height
|
| 18 |
+
self.type = annotation_type
|
| 19 |
|
| 20 |
+
# Define the Gradio interface
|
| 21 |
+
def annotate_images(images):
|
| 22 |
+
# Define the canvas size
|
| 23 |
+
canvas_size = (600, 600)
|
| 24 |
|
| 25 |
+
# Define the initial state
|
| 26 |
+
state = {
|
| 27 |
+
'image': None,
|
| 28 |
+
'annotations': [],
|
| 29 |
+
'annotation_type': ANNOTATION_TYPES[0],
|
| 30 |
+
'start_point': None
|
| 31 |
+
}
|
| 32 |
|
| 33 |
+
# Define the canvas drawing function
|
| 34 |
+
def draw_canvas(canvas, image_data, annotations):
|
| 35 |
+
# Convert the image data to a PIL Image object
|
| 36 |
+
image = Image.fromarray(image_data)
|
| 37 |
+
|
| 38 |
+
# Resize the image to fit the canvas
|
| 39 |
+
image = image.resize(canvas_size)
|
| 40 |
+
|
| 41 |
+
# Draw the image on the canvas
|
| 42 |
+
canvas.draw_image(image, (canvas_size[0]/2, canvas_size[1]/2))
|
| 43 |
+
|
| 44 |
+
# Draw the annotations on the canvas
|
| 45 |
+
for annotation in annotations:
|
| 46 |
+
x, y, width, height = annotation.x, annotation.y, annotation.width, annotation.height
|
| 47 |
+
if annotation.type == 'rect':
|
| 48 |
+
canvas.draw_rect(x, y, width, height, stroke_color='red')
|
| 49 |
+
elif annotation.type == 'circle':
|
| 50 |
+
radius = np.sqrt(np.power(width, 2) + np.power(height, 2)) / 2
|
| 51 |
+
center_x, center_y = x + width / 2, y + height / 2
|
| 52 |
+
canvas.draw_circle(center_x, center_y, radius, stroke_color='red')
|
| 53 |
+
|
| 54 |
+
# Define the canvas mousedown event handler
|
| 55 |
+
def canvas_mousedown(canvas, x, y):
|
| 56 |
+
state['start_point'] = (x, y)
|
| 57 |
+
|
| 58 |
+
# Define the canvas mousemove event handler
|
| 59 |
+
def canvas_mousemove(canvas, x, y):
|
| 60 |
+
if state['start_point'] is not None:
|
| 61 |
+
start_x, start_y = state['start_point']
|
| 62 |
+
end_x, end_y = x, y
|
| 63 |
+
annotation_type = state['annotation_type']
|
| 64 |
+
draw_annotation(canvas, start_x, start_y, end_x, end_y, annotation_type)
|
| 65 |
|
| 66 |
+
# Define the canvas mouseup event handler
|
| 67 |
+
def canvas_mouseup(canvas, x, y):
|
| 68 |
+
if state['start_point'] is not None:
|
| 69 |
+
start_x, start_y = state['start_point']
|
| 70 |
+
end_x, end_y = x, y
|
| 71 |
+
annotation_type = state['annotation_type']
|
| 72 |
+
add_annotation(start_x, start_y, end_x, end_y, annotation_type)
|
| 73 |
+
state['start_point'] = None
|
| 74 |
|
| 75 |
+
# Define the add annotation function
|
| 76 |
+
def add_annotation(start_x, start_y, end_x, end_y, annotation_type):
|
| 77 |
+
# Calculate the width and height of the annotation
|
| 78 |
+
width = np.abs(start_x - end_x)
|
| 79 |
+
height = np.abs(start_y - end_y)
|
| 80 |
|
| 81 |
+
# Create the annotation object
|
| 82 |
+
annotation = Annotation(start_x, start_y, width, height, annotation_type)
|
| 83 |
|
| 84 |
+
# Add the annotation to the array
|
| 85 |
+
state['annotations'].append(annotation)
|
| 86 |
|
| 87 |
+
# Redraw the canvas
|
| 88 |
+
draw_canvas(canvas, state['image'], state['annotations'])
|
|
|
|
| 89 |
|
| 90 |
+
#
|
|
|