pytorchobject-detectionfaster-rcnndetectron

Detectron2 code not showing anything in object detection


I am using the code described in this article for running inference (object detection) on an image using a trained model.

# import some common detectron2 utilities
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import cv2

from detectron2.data.detection_utils import read_image


WINDOW_NAME = "COCO detections"
#im = cv2.imread("sample.jpg")  # this also shows the same result
im = read_image("sample.jpg", format="BGR")

# Create config
cfg = get_cfg()
cfg.merge_from_file("C:/Users/preet/detectron_repo/configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
cfg.MODEL.WEIGHTS = "detectron2://COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x/139173657/model_final_68b088.pkl"

# Create predictor
predictor = DefaultPredictor(cfg)

# Make prediction
outputs = predictor(im)

v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
cv2.imshow(WINDOW_NAME, v.get_image()[:, :, ::-1])

But the window that pops out shows "not responding" and I don't see anything in that window. Moreover, I get the following warning:

UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at  C:\cb\pytorch_1000000000000\work\aten\src\ATen\native\TensorShape.cpp:2228.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]

How to resolve this?


Solution

  • After digging out, I found out that I missed this line at the end:

    cv2.waitKey(0)
    

    That's why everything was working perfectly but the window wasn't responding.