srinivas-mushroom's picture
Create app.py
002247f
raw
history blame
1.49 kB
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()