我修改了original [gluoncv.utils.viz.bbox]
以打印类标签。但是,为什么不遍历所有bbox?最后,我要实现的是计算已识别对象的数量。
以下是我尝试的代码。
def Test(img, bboxes, scores=None, labels=None, thresh=0.5,
class_names=None, colors=None, ax=None,
reverse_rgb=False, absolute_coordinates=True):
from matplotlib import pyplot as plt
if labels is not None and not len(bboxes) == len(labels):
raise ValueError('The length of labels and bboxes mismatch, {} vs {}'
.format(len(labels), len(bboxes)))
if scores is not None and not len(bboxes) == len(scores):
raise ValueError('The length of scores and bboxes mismatch, {} vs {}'
.format(len(scores), len(bboxes)))
if isinstance(bboxes, mx.nd.NDArray):
bboxes = bboxes.asnumpy()
if isinstance(labels, mx.nd.NDArray):
labels = labels.asnumpy()
if isinstance(scores, mx.nd.NDArray):
scores = scores.asnumpy()
for i, bbox in enumerate(bboxes):
if scores is not None and scores.flat[i] < thresh:
continue
if labels is not None and labels.flat[i] < 0:
continue
cls_id = int(labels.flat[i]) if labels is not None else -1
if class_names is not None and cls_id < len(class_names):
class_name = class_names[cls_id]
else:
class_name = str(cls_id) if cls_id >= 0 else ''
score = '{:.3f}'.format(scores.flat[i]) if scores is not None else ''
if class_name or score:
t = '{:s} {:s}'.format(class_name, score)
return t
box_ids, scores, bboxes = net(x)
ab = Test(orig_img, bboxes[0], scores[0], box_ids[0], class_names=net.classes)
print(ab)