2

I am learning detectron2 and practicing it using pokemonster data. So, I follow the detectron2 tutorial (here : https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5)

And also a detector is developed and below image can be detected.

enter image description here

But, in this image, red circle is class name. I want to plot only bounding box(green color) with the exception of red circle (class name). Now below is my code for visualization. What code should be modified? Thank you.

import glob
for imageName in sorted(glob.glob(os.path.join(test_path, '*.jpg'))):
  im = cv2.imread(imageName)
  outputs = predictor(im)
  v = Visualizer(im[:, :, ::-1],
                metadata=train_metadata,
                scale=0.8
                 )
  out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
  boxes = v._convert_boxes(outputs["instances"].pred_boxes.to('cpu'))
  for box in boxes:
    box = (round(box[0]), round(box[1]), round(box[2]) - round(box[0]), round(box[3] - box[1]))
    out = v.draw_text(f"{box[2:4]}", (box[0], box[1]))
  cv2_imshow(out.get_image()[:, :, ::-1])
Seunghyeon
  • 103
  • 1
  • 2
  • 10

3 Answers3

4

The class Visualizer contains all the methods inlcuding draw_box(). You want to draw all the boxes to your Visualizer object and finally return a VisImage

v = Visualizer(
        im[:, :, ::-1], 
        metadata=train_metadata, 
        scale=0.8,
        )
for box in outputs["instances"].pred_boxes.to('cpu'):
    v.draw_box(box)
    v.draw_text(str(box[:2].numpy()), tuple(box[:2].numpy()))
v = v.get_output()
img =  v.get_image()[:, :, ::-1]
cv2_imshow(img)
Alexander Riedel
  • 1,329
  • 1
  • 7
  • 14
0

There is a function to draw binary mask, but there is no function to draw mask on the colored image. How to draw mask on the original input color image.

output = predictor(input_image)        
V = Visualizer(input_image[:, :, ::-1], 
MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale = 1.2)
mask=output["instances"].get("pred_masks")
mask=mask.to("cpu")
V.draw_instance_predictions(output["instances"].to("cpu"))
for m in mask:
    V.draw_mask(m)

V = V.get_output()
cv2.imwrite("frame%d.jpg" % count, V.get_image()[:, :, ::-1]) 

Thank you

0

Here is a working version of boxes and masks:

v = Visualizer(im[:, :, ::-1],
               metadata=train_metadata, 
               scale=0.8,
)
# Boxes
for box in outputs["instances"].pred_boxes.to('cpu'):
    v.draw_box(box)    
# Masks
for mask in outputs["instances"].pred_masks.to('cpu'):
    v.draw_soft_mask(mask)
v = v.get_output()
img =  v.get_image()[:, :, ::-1]
cv2_imshow(img)
leenremm
  • 1,083
  • 13
  • 19