Spaces:
Runtime error
Runtime error
File size: 1,490 Bytes
002247f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import gradio as gr
from PIL import Image, ImageDraw
import json
# Define the function that will annotate the image
def annotate_image(input_image, annotations):
# Load the image and create a drawing context
img = Image.fromarray(input_image.astype('uint8'), 'RGB')
draw = ImageDraw.Draw(img)
# Draw any existing annotations on the image
for annotation in annotations:
x1, y1, x2, y2 = annotation['box']
draw.rectangle([x1, y1, x2, y2], outline='red', width=2)
# Return the annotated image as a numpy array
return img
# Define the function that will save the annotations to a file
def save_annotations(annotations):
with open('annotations.json', 'w') as f:
json.dump(annotations, f)
# Define the Gradio interface
inputs = [
gr.inputs.Image(type='numpy', label='Input Image'),
gr.inputs.Textbox(type='json', label='Annotations', default='[]')
]
def predict(input_image, annotations):
# Parse the annotations from the input string
annotations = json.loads(annotations)
# Annotate the image
output_image = annotate_image(input_image, annotations)
# Save the annotations to a file
save_annotations(annotations)
# Return the annotated image as a numpy array
return output_image
outputs = [
gr.outputs.Image(type='numpy', label='Output Image')
]
gr.Interface(predict, inputs, outputs, title='Image Annotator',
description='Annotate images using bounding boxes').launch()
|