Yolov5 根据自己的需要更改 预测框box和蒙版mask的颜色

2023-07-29,,

1、首先找到项目中 utils/plots.py 文件,打开该代码

将原来的 Colors类注释掉(或删掉),改成如下Colors类

class Colors:
def __init__(self):
pass def __call__(self, n, bgr=True):
# 根据 n 的值返回颜色,理论上可以返回任意颜色,自己加即可
if n == 1:
return (0, 0, 255) # 返回红色的(B,G,R)值
elif n == 0:
return (0, 255, 0) # 返回绿色的(B,G,R)值
else:
raise ValueError("n 只能取 0 或 1") # class Colors:
# # Ultralytics color palette https://ultralytics.com/
# def __init__(self):
# # hex = matplotlib.colors.TABLEAU_COLORS.values()
# hexs = ('FF3838', 'FF9D97', 'FF701F', 'FFB21D', 'CFD231', '48F90A', '92CC17', '3DDB86', '1A9334', '00D4BB',
# '2C99A8', '00C2FF', '344593', '6473FF', '0018EC', '8438FF', '520085', 'CB38FF', 'FF95C8', 'FF37C7')
# self.palette = [self.hex2rgb(f'#{c}') for c in hexs]
# self.n = len(self.palette)
#
# def __call__(self, i, bgr=False):
# c = self.palette[int(i) % self.n]
# return (c[2], c[1], c[0]) if bgr else c
#
# @staticmethod
# def hex2rgb(h): # rgb order (PIL)
# return tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)) 2、打开 predict.py ,可以直接搜索 colors,找到下列两行代码
```python
# 原代码 第一个我是在175行,第二个在190行,可能版本不一样在不同的行,根据自己的代码找一下
annotator.masks(masks, colors=[colors(x, True) for x in det[:, 5]],im_gpu=None if retina_masks else im[i])
annotator.box_label(xyxy, label, color=colors(c, True))
# 更改后的代码
annotator.masks(masks, colors=[colors(1, True)], im_gpu=None if retina_masks else im[i]) # 蒙版 mask ,我这里设为红色
annotator.box_label(xyxy, label, color=colors(0, True)) # 预测框,我这里设为绿色 3、完成,运行 predict.py 即可

Yolov5 根据自己的需要更改 预测框box和蒙版mask的颜色的相关教程结束。

《Yolov5 根据自己的需要更改 预测框box和蒙版mask的颜色.doc》

下载本文的Word格式文档,以方便收藏与打印。