File size: 1,251 Bytes
08aa404
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2


def draw_text(img, text,
          font=cv2.FONT_HERSHEY_PLAIN,
          x=0,
          y=0,
          font_scale=1,
          font_thickness=2,
          text_color=(0, 255, 0),
          text_color_bg=(0, 0, 0)
          ):

    text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
    text_w, text_h = text_size
    cv2.rectangle(img, (x, y), (x + text_w, y + text_h), text_color_bg, -1)
    cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)

    return img


def add_bboxes(img, bboxes, **kwargs):
    label = "dog"
    font_thickness=2
    for idx, box in enumerate(bboxes):
        x1, y1 = int(box[0]), int(box[1])
        x2, y2 = int(box[2]), int(box[3])
        if "scores" in kwargs.keys():
            score = list(kwargs["scores"])[idx]
            label = "{0:.2f}%".format(score*100)
            font_thickness=1
        img = cv2.rectangle(img, (x1, y1), (x2, y2), (36,255,12), 2)
        img = draw_text(img, label, x=x1, y=y1-10, font_thickness=font_thickness)
    return img


def reformat_bbox(coord):
    """
    Converts XYWH to XYXY
    """
    x1, y1 = coord[0], coord[1]
    x2, y2 = x1 + coord[2], y1 + coord[3]
    return [x1, y1, x2, y2]