File size: 948 Bytes
3b175de |
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 |
from matplotlib import cm
def get_pre_define_colors(num_classes, cmap_set: list[str] = None, in_rgb: bool = True, is_float: bool = False):
if cmap_set is None:
cmap_set = ['tab20', 'tab20b', 'tab20c']
colors = []
for cmap in cmap_set:
colors.extend(list(cm.get_cmap(cmap).colors))
if in_rgb:
new_colors = []
for color in colors:
if is_float:
new_colors.append(tuple(color))
else:
new_colors.append(tuple(int(x * 255) for x in color))
colors = new_colors
if num_classes > len(colors):
print(f"WARNING: {num_classes} classes are requested, but only {len(colors)} colors are available. Predefined colors will be reused.")
colors *= num_classes // len(colors) + 1
else:
colors = colors[:num_classes]
return colors
def xyxy_to_xywh(box):
return [box[0], box[1], box[2] - box[0], box[3] - box[1]] |