glenn-jocher
commited on
Commit
•
dbbc6b5
1
Parent(s):
e5e5ebc
Re-order `plots.py` to class-first (#4595)
Browse files- utils/plots.py +26 -24
utils/plots.py
CHANGED
@@ -45,30 +45,8 @@ class Colors:
|
|
45 |
colors = Colors() # create instance for 'from utils.plots import colors'
|
46 |
|
47 |
|
48 |
-
def hist2d(x, y, n=100):
|
49 |
-
# 2d histogram used in labels.png and evolve.png
|
50 |
-
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
|
51 |
-
hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
|
52 |
-
xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
|
53 |
-
yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
|
54 |
-
return np.log(hist[xidx, yidx])
|
55 |
-
|
56 |
-
|
57 |
-
def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5):
|
58 |
-
from scipy.signal import butter, filtfilt
|
59 |
-
|
60 |
-
# https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy
|
61 |
-
def butter_lowpass(cutoff, fs, order):
|
62 |
-
nyq = 0.5 * fs
|
63 |
-
normal_cutoff = cutoff / nyq
|
64 |
-
return butter(order, normal_cutoff, btype='low', analog=False)
|
65 |
-
|
66 |
-
b, a = butter_lowpass(cutoff, fs, order=order)
|
67 |
-
return filtfilt(b, a, data) # forward-backward filter
|
68 |
-
|
69 |
-
|
70 |
class Annotator:
|
71 |
-
# YOLOv5
|
72 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
|
73 |
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to plot_on_box() input image.'
|
74 |
self.pil = pil
|
@@ -79,9 +57,11 @@ class Annotator:
|
|
79 |
f = font_size or max(round(s * 0.035), 12)
|
80 |
try:
|
81 |
self.font = ImageFont.truetype(font, size=f)
|
82 |
-
except: # download TTF
|
|
|
83 |
url = "https://github.com/ultralytics/yolov5/releases/download/v1.0/" + font
|
84 |
torch.hub.download_url_to_file(url, font)
|
|
|
85 |
self.font = ImageFont.truetype(font, size=f)
|
86 |
self.fh = self.font.getsize('a')[1] - 3 # font height
|
87 |
else: # use cv2
|
@@ -122,6 +102,28 @@ class Annotator:
|
|
122 |
return np.asarray(self.im)
|
123 |
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
def output_to_target(output):
|
126 |
# Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
|
127 |
targets = []
|
|
|
45 |
colors = Colors() # create instance for 'from utils.plots import colors'
|
46 |
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
class Annotator:
|
49 |
+
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
|
50 |
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
|
51 |
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to plot_on_box() input image.'
|
52 |
self.pil = pil
|
|
|
57 |
f = font_size or max(round(s * 0.035), 12)
|
58 |
try:
|
59 |
self.font = ImageFont.truetype(font, size=f)
|
60 |
+
except Exception as e: # download TTF if missing
|
61 |
+
print(f'WARNING: Annotator font {font} not found: {e}')
|
62 |
url = "https://github.com/ultralytics/yolov5/releases/download/v1.0/" + font
|
63 |
torch.hub.download_url_to_file(url, font)
|
64 |
+
print(f'Annotator font successfully downloaded from {url} to {font}')
|
65 |
self.font = ImageFont.truetype(font, size=f)
|
66 |
self.fh = self.font.getsize('a')[1] - 3 # font height
|
67 |
else: # use cv2
|
|
|
102 |
return np.asarray(self.im)
|
103 |
|
104 |
|
105 |
+
def hist2d(x, y, n=100):
|
106 |
+
# 2d histogram used in labels.png and evolve.png
|
107 |
+
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
|
108 |
+
hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
|
109 |
+
xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
|
110 |
+
yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
|
111 |
+
return np.log(hist[xidx, yidx])
|
112 |
+
|
113 |
+
|
114 |
+
def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5):
|
115 |
+
from scipy.signal import butter, filtfilt
|
116 |
+
|
117 |
+
# https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy
|
118 |
+
def butter_lowpass(cutoff, fs, order):
|
119 |
+
nyq = 0.5 * fs
|
120 |
+
normal_cutoff = cutoff / nyq
|
121 |
+
return butter(order, normal_cutoff, btype='low', analog=False)
|
122 |
+
|
123 |
+
b, a = butter_lowpass(cutoff, fs, order=order)
|
124 |
+
return filtfilt(b, a, data) # forward-backward filter
|
125 |
+
|
126 |
+
|
127 |
def output_to_target(output):
|
128 |
# Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
|
129 |
targets = []
|