-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.py
110 lines (99 loc) · 3.1 KB
/
render.py
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
import numpy as np
import cv2
from math import sqrt
_LINE_THICKNESS_SCALING = 500.0
np.random.seed(0)
RAND_COLORS = np.random.randint(50, 255, (64, 3), "int") # used for class visu
RAND_COLORS[0] = [220, 220, 220]
def render_box(img, box, color=(200, 200, 200)):
"""
Render a box. Calculates scaling and thickness automatically.
:param img: image to render into
:param box: (x1, y1, x2, y2) - box coordinates
:param color: (b, g, r) - box color
:return: updated image
"""
x1, y1, x2, y2 = box
thickness = int(
round(
(img.shape[0] * img.shape[1])
/ (_LINE_THICKNESS_SCALING * _LINE_THICKNESS_SCALING)
)
)
thickness = max(1, thickness)
img = cv2.rectangle(
img,
(int(x1), int(y1)),
(int(x2), int(y2)),
color,
thickness=thickness
)
return img
def render_filled_box(img, box, color=(200, 200, 200)):
"""
Render a box. Calculates scaling and thickness automatically.
:param img: image to render into
:param box: (x1, y1, x2, y2) - box coordinates
:param color: (b, g, r) - box color
:return: updated image
"""
x1, y1, x2, y2 = box
img = cv2.rectangle(
img,
(int(x1), int(y1)),
(int(x2), int(y2)),
color,
thickness=cv2.FILLED
)
return img
_TEXT_THICKNESS_SCALING = 700.0
_TEXT_SCALING = 520.0
def get_text_size(img, text, normalised_scaling=1.0):
"""
Get calculated text size (as box width and height)
:param img: image reference, used to determine appropriate text scaling
:param text: text to display
:param normalised_scaling: additional normalised scaling. Default 1.0.
:return: (width, height) - width and height of text box
"""
thickness = int(
round(
(img.shape[0] * img.shape[1])
/ (_TEXT_THICKNESS_SCALING * _TEXT_THICKNESS_SCALING)
)
* normalised_scaling
)
thickness = max(1, thickness)
scaling = img.shape[0] / _TEXT_SCALING * normalised_scaling
return cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, scaling, thickness)[0]
def render_text(img, text, pos, color=(200, 200, 200), normalised_scaling=1.0):
"""
Render a text into the image. Calculates scaling and thickness automatically.
:param img: image to render into
:param text: text to display
:param pos: (x, y) - upper left coordinates of render position
:param color: (b, g, r) - text color
:param normalised_scaling: additional normalised scaling. Default 1.0.
:return: updated image
"""
x, y = pos
thickness = int(
round(
(img.shape[0] * img.shape[1])
/ (_TEXT_THICKNESS_SCALING * _TEXT_THICKNESS_SCALING)
)
* normalised_scaling
)
thickness = max(1, thickness)
scaling = img.shape[0] / _TEXT_SCALING * normalised_scaling
size = get_text_size(img, text, normalised_scaling)
cv2.putText(
img,
text,
(int(x), int(y + size[1])),
cv2.FONT_HERSHEY_SIMPLEX,
scaling,
color,
thickness=thickness,
)
return img