def draw_boxs(img:Union[str, np.ndarray],
boxes:np.ndarray,
labels:np.ndarray = None,
label_name:Dict[int, str]=None,
font_scale:int = 1,
line_thickness:int = 1):
"""
:param img: img path or img date, shape: [h, w ,c]
:param boxes: [xmin, ymin, xmax, ymax], shape: [n, 4],
:param labels: shape: [n]
:param label_name: dict, {index, name}
:param font_scale: int
:param line_thickness: int
:return:
"""
if labels is not None:
assert boxes.shape[0] == labels.shape[0]
if isinstance(img, str):
img = cv2.imread(img)
for i in range(boxes.shape[0]):
color = (0, 255, 0)
if labels is not None:
label = labels[i]
color = [int(i * (label ** 2 - label)) % 255 for i in (15, 127, 7)]
print(color)
if label_name is not None:
if label in label_name:
label = label_name[label]
(text_w, text_h), _ = cv2.getTextSize('{}'.format(label), cv2.FONT_HERSHEY_COMPLEX_SMALL,
font_scale, font_scale)
zero = np.zeros((img.shape), dtype=np.uint8)
background = cv2.rectangle(zero, (boxes[i][0], boxes[i][3]-text_h),
(boxes[i][0]+text_w, boxes[i][3]), color, -1)
img = cv2.addWeighted(img, 1, background, 1, 1)
cv2.putText(img, '{}'.format(label), (boxes[i][0], boxes[i][3]),
cv2.FONT_HERSHEY_COMPLEX_SMALL, font_scale, (0, 0, 0), font_scale)
cv2.rectangle(img, (boxes[i][0], boxes[i][1]), (boxes[i][2], boxes[i][3]), color, line_thickness)
return img