-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
101 lines (89 loc) · 2.45 KB
/
plot.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
from contextlib import contextmanager
from typing import Any, Callable, Dict, Iterable, List, Optional, TypeVar, Union # NOQA
import cv2
import matplotlib.pyplot as plt
import numpy as np
def cv_put_box_with_text(
image: np.ndarray,
box_ltrd: Iterable[float],
# Rectangle params
rec_color=(255, 255, 255), # White
rec_thickness=4,
# Text params
text=None,
text_size=0.6,
text_color=None,
text_thickness=2,
text_position="left_down",
text_font_face=cv2.FONT_HERSHEY_SIMPLEX,
text_line_type=cv2.LINE_8,
) -> np.ndarray:
"""
Overwrites in place
"""
l, t, r, d = map(int, box_ltrd)
cv2.rectangle(
image, (l, t), (r, d), color=rec_color, thickness=rec_thickness
)
fontFace = cv2.FONT_HERSHEY_SIMPLEX
if text:
if text_color is None:
text_color = rec_color
# Text Positioning
retval, baseline = cv2.getTextSize(
text, fontFace, text_size, text_thickness
)
if text_position == "left_below":
text_pos = (l, d + retval[1])
elif text_position == "left_down":
text_pos = (l, d - 5)
elif text_position == "left_up":
text_pos = (l, t - 5)
elif text_position == "right_down":
text_pos = (r - retval[0], d - 5)
elif text_position == "right_up":
text_pos = (r - retval[0], t - 5)
else:
raise ValueError("Wrong text position")
cv2.putText(
image,
text,
text_pos,
fontFace=text_font_face,
fontScale=text_size,
color=text_color,
thickness=text_thickness,
lineType=text_line_type,
)
return image
def cv_put_text(
image,
text,
text_pos,
text_size=0.6,
text_color=(255, 255, 255),
text_thickness=2,
text_position="left_down",
):
fontFace = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(
image,
text,
text_pos,
fontFace=fontFace,
fontScale=text_size,
color=text_color,
thickness=text_thickness,
)
def plot_close(f, legend_list=[], impath=None):
if impath:
f.savefig(impath, bbox_extra_artists=legend_list, bbox_inches="tight")
else:
plt.show()
plt.close(f)
@contextmanager
def plt_backend(backend_name):
original = plt.get_backend()
plt.switch_backend(backend_name)
yield
plt.switch_backend(original)