Search code examples
pythoncomputer-visionimage-segmentationdetectron

How to make inference on multiple images, with detectron2 and DefaultPredictor


I have trained the model, now i would like to use it to detect objects in many images. I saw that the defaultpredictor allows you to detect only on an image, what can I do?

I am really new to this world. The approach I tried was to use a for loop but it doesn't work. Are there any other methods?

%cd /kaggle/working/detectron2
import glob
cfg.MODEL.WEIGHTS = os.path.join("/kaggle/working/detectron2/output", "model_final.pth") # path to the model we trained
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.0001 # set a testing threshold
pred = DefaultPredictor(cfg)
os.chdir("/kaggle/working/detectron2/images")
for img in glob.glob('.jpg'):
    inputs = cv2.imread(img)
    outputs = pred(inputs)
    print(outputs)

Solution

  • Ok, i solved in this way:

    %cd /kaggle/working/detectron2
    import glob
    cfg.MODEL.WEIGHTS = os.path.join("/kaggle/working/detectron2/output", "model_final.pth")   # path to the model we trained
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.0001   # set a testing threshold
    pred = DefaultPredictor(cfg)
    for img in glob.glob('/kaggle/working/detectron2/images/*.jpg'):
        inputs = cv2.imread(img)
        outputs = pred(inputs)
        print(outputs)
    

    i deleted os.chdir()