def drawBoxes(img, boxes, categories, names, palette, scores=None, fade=False):
def clipCoord(xy):
return np.minimum(np.maximum(np.array(xy,dtype=np.int32),0),[img.shape[1]-1, img.shape[0]-1]).tolist()
cmap = palette.getMap(list=True)
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
fontSize = 0.8
fontThickness = 1
pad=5
img=np.copy(img)
for box in range(boxes.shape[0]):
if fade and scores is not None:
iOrig = img
img=np.copy(img)
topleft = tuple(clipCoord(boxes[box][0:2]))
if categories is not None:
color = tuple(cmap[categories[box]])
else:
color = (0,0,255)
cv2.rectangle(img, topleft, tuple(clipCoord(boxes[box][2:5])), color, thickness=4)
if names:
title=names[box]
if scores is not None:
title+=": %.2f" % scores[box]
textpos=[topleft[0], topleft[1]-pad]
size = cv2.getTextSize(title, font, fontSize, fontThickness)[0]
boxTL = textpos[:]
boxTL[1] = boxTL[1] - size[1]
boxBR = list(topleft)
boxBR[0] = boxBR[0] + size[0]
cv2.rectangle(img, tuple(boxTL), tuple(boxBR), color, thickness=-1)
cv2.rectangle(img, tuple(boxTL), tuple(boxBR), color, thickness=4)
cv2.putText(img, title, tuple(textpos), font, fontSize, (255,255,255), thickness=fontThickness)
if fade and scores is not None:
img = scores[box] * img + (1.0-scores[box]) * iOrig
return img
评论列表
文章目录