Skip to content

Commit

Permalink
Re-order plots.py to class-first (ultralytics#4595)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher authored and CesarBazanAV committed Sep 29, 2021
1 parent d819458 commit 17fa388
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,8 @@ def hex2rgb(h): # rgb order (PIL)
colors = Colors() # create instance for 'from utils.plots import colors'


def hist2d(x, y, n=100):
# 2d histogram used in labels.png and evolve.png
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
return np.log(hist[xidx, yidx])


def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5):
from scipy.signal import butter, filtfilt

# https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy
def butter_lowpass(cutoff, fs, order):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
return butter(order, normal_cutoff, btype='low', analog=False)

b, a = butter_lowpass(cutoff, fs, order=order)
return filtfilt(b, a, data) # forward-backward filter


class Annotator:
# YOLOv5 PIL Annotator class
# YOLOv5 Annotator for train/val mosaics and jpgs and detect/hub inference annotations
def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=True):
assert im.data.contiguous, 'Image not contiguous. Apply np.ascontiguousarray(im) to plot_on_box() input image.'
self.pil = pil
Expand All @@ -79,9 +57,11 @@ def __init__(self, im, line_width=None, font_size=None, font='Arial.ttf', pil=Tr
f = font_size or max(round(s * 0.035), 12)
try:
self.font = ImageFont.truetype(font, size=f)
except: # download TTF
except Exception as e: # download TTF if missing
print(f'WARNING: Annotator font {font} not found: {e}')
url = "https://github.com/ultralytics/yolov5/releases/download/v1.0/" + font
torch.hub.download_url_to_file(url, font)
print(f'Annotator font successfully downloaded from {url} to {font}')
self.font = ImageFont.truetype(font, size=f)
self.fh = self.font.getsize('a')[1] - 3 # font height
else: # use cv2
Expand Down Expand Up @@ -122,6 +102,28 @@ def result(self):
return np.asarray(self.im)


def hist2d(x, y, n=100):
# 2d histogram used in labels.png and evolve.png
xedges, yedges = np.linspace(x.min(), x.max(), n), np.linspace(y.min(), y.max(), n)
hist, xedges, yedges = np.histogram2d(x, y, (xedges, yedges))
xidx = np.clip(np.digitize(x, xedges) - 1, 0, hist.shape[0] - 1)
yidx = np.clip(np.digitize(y, yedges) - 1, 0, hist.shape[1] - 1)
return np.log(hist[xidx, yidx])


def butter_lowpass_filtfilt(data, cutoff=1500, fs=50000, order=5):
from scipy.signal import butter, filtfilt

# https://stackoverflow.com/questions/28536191/how-to-filter-smooth-with-scipy-numpy
def butter_lowpass(cutoff, fs, order):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
return butter(order, normal_cutoff, btype='low', analog=False)

b, a = butter_lowpass(cutoff, fs, order=order)
return filtfilt(b, a, data) # forward-backward filter


def output_to_target(output):
# Convert model output to target format [batch_id, class_id, x, y, w, h, conf]
targets = []
Expand Down

0 comments on commit 17fa388

Please sign in to comment.