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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -36
app.py CHANGED
@@ -1,48 +1,90 @@
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()
 
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
+ #