srinivas-mushroom commited on
Commit
002247f
1 Parent(s): f7ab6b7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw
3
+ import json
4
+
5
+ # Define the function that will annotate the image
6
+ def annotate_image(input_image, annotations):
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
+ # Draw any existing annotations on the image
12
+ for annotation in annotations:
13
+ x1, y1, x2, y2 = annotation['box']
14
+ draw.rectangle([x1, y1, x2, y2], outline='red', width=2)
15
+
16
+ # Return the annotated image as a numpy array
17
+ return img
18
+
19
+ # Define the function that will save the annotations to a file
20
+ def save_annotations(annotations):
21
+ with open('annotations.json', 'w') as f:
22
+ json.dump(annotations, f)
23
+
24
+ # Define the Gradio interface
25
+ inputs = [
26
+ gr.inputs.Image(type='numpy', label='Input Image'),
27
+ gr.inputs.Textbox(type='json', label='Annotations', default='[]')
28
+ ]
29
+
30
+ def predict(input_image, annotations):
31
+ # Parse the annotations from the input string
32
+ annotations = json.loads(annotations)
33
+
34
+ # Annotate the image
35
+ output_image = annotate_image(input_image, annotations)
36
+
37
+ # Save the annotations to a file
38
+ save_annotations(annotations)
39
+
40
+ # Return the annotated image as a numpy array
41
+ return output_image
42
+
43
+ outputs = [
44
+ gr.outputs.Image(type='numpy', label='Output Image')
45
+ ]
46
+
47
+ gr.Interface(predict, inputs, outputs, title='Image Annotator',
48
+ description='Annotate images using bounding boxes').launch()