File size: 5,452 Bytes
0e590dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddb2631
 
 
 
 
0e590dc
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import gradio as gr
import supervision as sv
from ultralytics import YOLO
import os #added for cache_examples
from PIL import Image, ImageColor
import numpy as np

def load_model(img):
  # Load model, get results and return detections/labels
  model = YOLO('yolov8s-seg.pt')
  result = model(img, verbose=False, imgsz=1280)[0]
  detections = sv.Detections.from_ultralytics(result)
  labels = [
            f"{model.model.names[class_id]} {confidence:.2f}"
            for class_id, confidence in zip(detections.class_id, detections.confidence)
           ]

  return detections, labels

def calculate_crop_dim(a,b):
  #Calculates the crop dimensions of the image resultant
  if a>b:
    width= a
    height = a
  else:
    width = b
    height = b

  return width, height

def annotator(img,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,colorlabel):

    """
    Function that changes the color of annotators
    Args:
        annotators: Icon whose color needs to be changed.
        color: Chosen color with which to edit the input icon in Hex.
        img: Input image is numpy matrix in BGR.
    Returns:
        annotators: annotated image
    """



    img = img[...,::-1].copy() # BGR to RGB using numpy


    detections, labels = load_model(img)


    if "Blur" in annotators:
        # Apply Blur
        blur_annotator = sv.BlurAnnotator()
        img = blur_annotator.annotate(img, detections=detections)

    if "BoundingBox" in annotators:
        # Draw Bounding box
        box_annotator = sv.BoundingBoxAnnotator(sv.Color.from_hex(str(colorbb)))
        img = box_annotator.annotate(img, detections=detections)

    if "Mask" in annotators:
        # Draw Mask
        mask_annotator = sv.MaskAnnotator(sv.Color.from_hex(str(colormask)))
        img = mask_annotator.annotate(img, detections=detections)

    if "Ellipse" in annotators:
        # Draw ellipse
        ellipse_annotator = sv.EllipseAnnotator(sv.Color.from_hex(str(colorellipse)))
        img = ellipse_annotator.annotate(img, detections=detections)

    if "BoxCorner" in annotators:
        # Draw Box corner
        corner_annotator = sv.BoxCornerAnnotator(sv.Color.from_hex(str(colorbc)))
        img = corner_annotator.annotate(img, detections=detections)


    if "Circle" in annotators: 
        # Draw circle
        circle_annotator = sv.CircleAnnotator(sv.Color.from_hex(str(colorcir)))
        img = circle_annotator.annotate(img, detections=detections)

    if "Label" in annotators:
        # Draw Label
        label_annotator = sv.LabelAnnotator(text_position=sv.Position.CENTER)
        label_annotator = sv.LabelAnnotator(sv.Color.from_hex(str(colorlabel)))
        img = label_annotator.annotate(img, detections=detections, labels=labels)



    #crop image for the largest possible square
    res_img = Image.fromarray(img)
    print(type(res_img))

    x=0
    y=0


    print("size of the pil im=", res_img.size)
    (v1,v2) = res_img.size
    width, height = calculate_crop_dim(v1, v2)
    print(width, height)
    my_img = np.array(res_img)

    crop_img = my_img[y:y+height, x:x+width]
    print(type(crop_img))

    return crop_img[...,::-1].copy() # BGR to RGB using numpy


with gr.Blocks(theme=gr.themes.Soft(primary_hue=gr.themes.colors.purple)
    .set(
      button_primary_background_fill="*primary_600",
      button_primary_background_fill_hover="*primary_700",
      checkbox_label_background_fill_selected="*primary_600",
      checkbox_background_color_selected="*primary_400",
    )) as demo:
    gr.Markdown("""# Image Annotator""")
    annotators = gr.CheckboxGroup(choices=["BoundingBox", "Mask", "Ellipse", "BoxCorner", "Circle", "Label", "Blur"], value=["BoundingBox", "Mask"], label="Select Annotators:")

    with gr.Accordion("**Color Picker**"):
      with gr.Row():
        with gr.Column():
          colorbb = gr.ColorPicker(value="#A351FB",label="BoundingBox")
        with gr.Column():
          colormask = gr.ColorPicker(value="#A351FB",label="Mask")
        with gr.Column():
          colorellipse = gr.ColorPicker(value="#A351FB",label="Ellipse")
        with gr.Column():
          colorbc = gr.ColorPicker(value="#A351FB",label="BoxCorner")
        with gr.Column():
          colorcir = gr.ColorPicker(value="#A351FB",label="Circle")
        with gr.Column():
          colorlabel = gr.ColorPicker(value="#A351FB",label="Label")
    
    with gr.Row():
      with gr.Column():
        with gr.Tab("Input image"):
          image_input = gr.Image(type="numpy", show_label=False)
      with gr.Column():
        with gr.Tab("Result image"):
          image_output = gr.Image(type="numpy", show_label=False)
    image_button = gr.Button(value="Annotate it!", variant="primary")

    image_button.click(annotator, inputs=[image_input,annotators,colorbb,colormask,colorellipse,colorbc,colorcir,colorlabel], outputs=image_output)

    gr.Markdown("## Image Examples")
    gr.Examples(
        examples=[os.path.join(os.path.abspath(''), "image/city.jpg"), 
                  os.path.join(os.path.abspath(''), "image/household.jpg"), 
                  os.path.join(os.path.abspath(''), "image/industry.jpg"),
                  os.path.join(os.path.abspath(''), "image/retail.jpg"),
                  os.path.join(os.path.abspath(''), "image/aerodefence.jpg")],
        inputs=image_input,
        outputs=image_output,
        fn=annotator,
        cache_examples=False,
    )


demo.launch(debug=False)