vis_bboxes.py
Go to the documentation of this file.
1 import six
2 import cv2
3 
4 
5 def voc_colormap(nlabels):
6  colors = []
7  for i in six.moves.range(nlabels):
8  r, g, b = 0, 0, 0
9  for j in range(8):
10  if i & (1 << 0):
11  r |= 1 << (7 - j)
12  if i & (1 << 1):
13  g |= 1 << (7 - j)
14  if i & (1 << 2):
15  b |= 1 << (7 - j)
16  i >>= 3
17  colors.append([r, g, b])
18  return colors
19 
20 
21 def vis_bboxes(img, bboxes, labels,
22  font_scale=0.8,
23  thickness=1,
24  font_face=cv2.FONT_HERSHEY_SIMPLEX,
25  text_color=(255, 255, 255),
26  max_label_num=1024):
27  """Visualize bounding boxes inside image.
28 
29  """
30  if len(bboxes) != len(labels):
31  raise ValueError("len(bboxes) and len(labels) should be same "
32  "we get len(bboxes):{}, len(lables):{}"
33  .format(len(bboxes), len(labels)))
34  colormap = voc_colormap(max_label_num)
35 
36  CV_AA = 16 # for anti-alias
37  for bbox, label in zip(bboxes, labels):
38  color = colormap[label % max_label_num]
39  x1, y1, w, h = bbox
40  x2 = x1 + w
41  y2 = y1 + h
42  x1, y1, x2, y2 = map(int, [x1, y1, x2, y2])
43  cv2.rectangle(img, (x1, y1), (x2, y2), color, 2, CV_AA)
44 
45  label_name = str(label)
46  img_bbox = img[y1:y2, x1:]
47 
48  text = label_name
49  size, baseline = cv2.getTextSize(
50  text, font_face, font_scale, thickness)
51  cv2.rectangle(
52  img_bbox, (0, 0), (size[0], size[1] + baseline),
53  color=color, thickness=-1)
54  cv2.putText(img_bbox, text, (0, size[1]),
55  font_face, font_scale, text_color, thickness)
56  return img
def vis_bboxes(img, bboxes, labels, font_scale=0.8, thickness=1, font_face=cv2.FONT_HERSHEY_SIMPLEX, text_color=(255, 255, 255), max_label_num=1024)
Definition: vis_bboxes.py:26


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27