Spaces:
Runtime error
Runtime error
Commit
·
24236d0
1
Parent(s):
af2e4f1
Update app.py
Browse files
app.py
CHANGED
|
@@ -88,3 +88,64 @@ def annotate_images(images):
|
|
| 88 |
draw_canvas(canvas, state['image'], state['annotations'])
|
| 89 |
|
| 90 |
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
draw_canvas(canvas, state['image'], state['annotations'])
|
| 89 |
|
| 90 |
#
|
| 91 |
+
|
| 92 |
+
# Define the draw annotation function
|
| 93 |
+
def draw_annotation(canvas, start_x, start_y, end_x, end_y, annotation_type):
|
| 94 |
+
canvas.clear()
|
| 95 |
+
draw_canvas(canvas, state['image'], state['annotations'])
|
| 96 |
+
width = np.abs(start_x - end_x)
|
| 97 |
+
height = np.abs(start_y - end_y)
|
| 98 |
+
if annotation_type == 'rect':
|
| 99 |
+
canvas.draw_rect(start_x, start_y, width, height, stroke_color='red')
|
| 100 |
+
elif annotation_type == 'circle':
|
| 101 |
+
radius = np.sqrt(np.power(width, 2) + np.power(height, 2)) / 2
|
| 102 |
+
center_x, center_y = start_x + width / 2, start_y + height / 2
|
| 103 |
+
canvas.draw_circle(center_x, center_y, radius, stroke_color='red')
|
| 104 |
+
|
| 105 |
+
# Define the annotation type dropdown event handler
|
| 106 |
+
def annotation_type_changed(value):
|
| 107 |
+
state['annotation_type'] = value
|
| 108 |
+
|
| 109 |
+
# Define the download annotations button click event handler
|
| 110 |
+
def download_annotations_clicked():
|
| 111 |
+
# Define the csv headers
|
| 112 |
+
headers = ['x', 'y', 'width', 'height', 'type']
|
| 113 |
+
|
| 114 |
+
# Define the csv data
|
| 115 |
+
rows = [[str(annotation.x), str(annotation.y), str(annotation.width), str(annotation.height), annotation.type]
|
| 116 |
+
for annotation in state['annotations']]
|
| 117 |
+
|
| 118 |
+
# Create the csv string
|
| 119 |
+
csv_string = StringIO()
|
| 120 |
+
csv_writer = csv.writer(csv_string)
|
| 121 |
+
csv_writer.writerow(headers)
|
| 122 |
+
for row in rows:
|
| 123 |
+
csv_writer.writerow(row)
|
| 124 |
+
|
| 125 |
+
# Download the csv file
|
| 126 |
+
b64_csv = base64.b64encode(csv_string.getvalue().encode()).decode()
|
| 127 |
+
href = f'data:text/csv;base64,{b64_csv}'
|
| 128 |
+
download_link = f'<a href="{href}" download="annotations.csv">Download Annotations CSV</a>'
|
| 129 |
+
gr.Interface.show(download_link)
|
| 130 |
+
|
| 131 |
+
# Define the interface components
|
| 132 |
+
image = gr.inputs.Image(label='Image')
|
| 133 |
+
annotation_type = gr.inputs.Dropdown(ANNOTATION_TYPES, label='Annotation Type', default=ANNOTATION_TYPES[0], onchange=annotation_type_changed)
|
| 134 |
+
download_annotations = gr.outputs.Button(label='Download Annotations', type='button', onclick=download_annotations_clicked)
|
| 135 |
+
canvas = gr.outputs.Canvas(draw_event_handlers={
|
| 136 |
+
'mousedown': canvas_mousedown,
|
| 137 |
+
'mousemove': canvas_mousemove,
|
| 138 |
+
'mouseup': canvas_mouseup
|
| 139 |
+
})
|
| 140 |
+
|
| 141 |
+
# Define the interface function
|
| 142 |
+
def annotate_images(images):
|
| 143 |
+
state['image'] = images[0]
|
| 144 |
+
draw_canvas(canvas, state['image'], state['annotations'])
|
| 145 |
+
return canvas, annotation_type, download_annotations
|
| 146 |
+
|
| 147 |
+
# Create the interface
|
| 148 |
+
interface = gr.Interface(annotate_images, inputs=image, outputs=[canvas, annotation_type, download_annotations], capture_session=True)
|
| 149 |
+
|
| 150 |
+
return interface
|
| 151 |
+
|