0

In detectron 2 how could i get detected object class name and the bounding box x,y coordinates? enter image description here

and print it like print(obj_name,x,y)

2 Answers2

4

This code prints the name and coordinates for each detected object.

predictor = DefaultPredictor(config)
outputs = predictor(image)
instances = outputs["instances"]
detected_class_indexes = instances.pred_classes
prediction_boxes = instances.pred_boxes

metadata = MetadataCatalog.get(config.DATASETS.TRAIN[0])
class_catalog = metadata.thing_classes

for idx, coordinates in enumerate(prediction_boxes):
    class_index = detected_class_indexes[idx]
    class_name = class_catalog[class_index]
    print(class_name, coordinates)
flomaster
  • 1,563
  • 16
  • 16
  • Hey, the thing_classes has to be explicitly set. Is there a way to access the class names present in dataset itself without having to manually set it first? – Ayush Chaurasia Oct 14 '21 at 15:31
  • Hi , how to extract the segmented drawing boundary coordinate which instead of the box coordinate ? – akunyer Feb 08 '22 at 15:28
1

You can extract those in the model inference's outputs, for example:

predictor = DefaultPredictor(cfg)
outputs = predictor(im)
  • outputs[“instances”]: Instances object with the following fields:

“pred_boxes”: Boxes object storing N boxes, one for each detected instance.

“pred_classes”: Tensor, a vector of N labels in range [0, num_categories).

Ref: https://detectron2.readthedocs.io/en/latest/tutorials/models.html