JAYABALAMBIKA.R commited on
Commit
aad4b76
1 Parent(s): b1f55ef

Create app.py

Browse files

This is the application file for gradio

Files changed (1) hide show
  1. app.py +160 -0
app.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import supervision as sv
3
+ from ultralytics import YOLO
4
+ import os #added for cache_examples
5
+ from PIL import Image, ImageColor
6
+ import numpy as np
7
+
8
+ def load_model(img):
9
+ # Load model, get results and return detections/labels
10
+ model = YOLO('yolov8s-seg.pt')
11
+ result = model(img, verbose=False, imgsz=1280)[0]
12
+ detections = sv.Detections.from_ultralytics(result)
13
+ labels = [
14
+ f"{model.model.names[class_id]} {confidence:.2f}"
15
+ for class_id, confidence in zip(detections.class_id, detections.confidence)
16
+ ]
17
+
18
+ return detections, labels
19
+
20
+ def calculate_crop_dim(a,b):
21
+ #Calculates the crop dimensions of the image resultant
22
+ if a>b:
23
+ width= a
24
+ height = a
25
+ else:
26
+ width = b
27
+ height = b
28
+
29
+ return width, height
30
+
31
+ def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,colorlabel):
32
+
33
+ """
34
+ Function that changes the color of annotators
35
+ Args:
36
+ annotators: Icon whose color needs to be changed.
37
+ color: Chosen color with which to edit the input icon in Hex.
38
+ img: Input image is numpy matrix in BGR.
39
+ Returns:
40
+ annotators: annotated image
41
+ """
42
+
43
+
44
+
45
+ img = img[...,::-1].copy() # BGR to RGB using numpy
46
+
47
+
48
+ detections, labels = load_model(img)
49
+
50
+
51
+ if "Blur" in annotators:
52
+ # Apply Blur
53
+ blur_annotator = sv.BlurAnnotator()
54
+ img = blur_annotator.annotate(img, detections=detections)
55
+
56
+ if "BoundingBox" in annotators:
57
+ # Draw Bounding box
58
+ box_annotator = sv.BoundingBoxAnnotator(sv.Color.from_hex(str(colorbb)))
59
+ img = box_annotator.annotate(img, detections=detections)
60
+
61
+ if "Mask" in annotators:
62
+ # Draw Mask
63
+ mask_annotator = sv.MaskAnnotator(sv.Color.from_hex(str(colormask)))
64
+ img = mask_annotator.annotate(img, detections=detections)
65
+
66
+ if "Ellipse" in annotators:
67
+ # Draw ellipse
68
+ ellipse_annotator = sv.EllipseAnnotator(sv.Color.from_hex(str(colorellipse)))
69
+ img = ellipse_annotator.annotate(img, detections=detections)
70
+
71
+ if "BoxCorner" in annotators:
72
+ # Draw Box corner
73
+ corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
74
+ img = corner_annotator.annotate(img, detections=detections)
75
+
76
+
77
+ if "Circle" in annotators:
78
+ # Draw circle
79
+ circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
80
+ img = circle_annotator.annotate(img, detections=detections)
81
+
82
+ if "Label" in annotators:
83
+ # Draw Label
84
+ label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
85
+ label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
86
+ img = label_annotator.annotate(img, detections=detections, labels=labels)
87
+
88
+
89
+
90
+ #crop image for the largest possible square
91
+ res_img = Image.fromarray(img)
92
+ print(type(res_img))
93
+
94
+ x=0
95
+ y=0
96
+
97
+
98
+ print("size of the pil im=", res_img.size)
99
+ (v1,v2) = res_img.size
100
+ width, height = calculate_crop_dim(v1, v2)
101
+ print(width, height)
102
+ my_img = np.array(res_img)
103
+
104
+ crop_img = my_img[y:y+height, x:x+width]
105
+ print(type(crop_img))
106
+
107
+ return crop_img[...,::-1].copy() # BGR to RGB using numpy
108
+
109
+
110
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.purple)
111
+ .set(
112
+ button_primary_background_fill="*primary_600",
113
+ button_primary_background_fill_hover="*primary_700",
114
+ checkbox_label_background_fill_selected="*primary_600",
115
+ checkbox_background_color_selected="*primary_400",
116
+ )) as demo:
117
+ gr.Markdown("""# Supervision Annotators""")
118
+ annotators = gr.CheckboxGroup(choices=["BoundingBox", "Mask", "Ellipse", "BoxCorner", "Circle", "Label", "Blur"], value=["BoundingBox", "Mask"], label="Select Annotators:")
119
+
120
+ with gr.Accordion("**Color Picker**"):
121
+ with gr.Row():
122
+ with gr.Column():
123
+ colorbb = gr.ColorPicker(value="#A351FB",label="BoundingBox")
124
+ with gr.Column():
125
+ colormask = gr.ColorPicker(value="#A351FB",label="Mask")
126
+ with gr.Column():
127
+ colorellipse = gr.ColorPicker(value="#A351FB",label="Ellipse")
128
+ with gr.Column():
129
+ colorbc = gr.ColorPicker(value="#A351FB",label="BoxCorner")
130
+ with gr.Column():
131
+ colorcir = gr.ColorPicker(value="#A351FB",label="Circle")
132
+ with gr.Column():
133
+ colorlabel = gr.ColorPicker(value="#A351FB",label="Label")
134
+
135
+ with gr.Row():
136
+ with gr.Column():
137
+ with gr.Tab("Input image"):
138
+ image_input = gr.Image(type="numpy", show_label=False)
139
+ with gr.Column():
140
+ with gr.Tab("Result image"):
141
+ image_output = gr.Image(type="numpy", show_label=False)
142
+ image_button = gr.Button(value="Annotate it!", variant="primary")
143
+
144
+ image_button.click(annotator, inputs=[image_input,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,colorlabel], outputs=image_output)
145
+
146
+ gr.Markdown("## Image Examples")
147
+ gr.Examples(
148
+ examples=[os.path.join(os.path.abspath(''), "city.jpg"),
149
+ os.path.join(os.path.abspath(''), "household.jpg"),
150
+ os.path.join(os.path.abspath(''), "industry.jpg"),
151
+ os.path.join(os.path.abspath(''), "retail.jpg"),
152
+ os.path.join(os.path.abspath(''), "aerodefence.jpg")],
153
+ inputs=image_input,
154
+ outputs=image_output,
155
+ fn=annotator,
156
+ cache_examples=False,
157
+ )
158
+
159
+
160
+ demo.launch(debug=False)