In detectron 2 how could i get detected object class name and the bounding box x,y coordinates?
and print it like print(obj_name,x,y)
In detectron 2 how could i get detected object class name and the bounding box x,y coordinates?
and print it like print(obj_name,x,y)
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)
You can extract those in the model inference's outputs, for example:
predictor = DefaultPredictor(cfg)
outputs = predictor(im)
“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