如何使用 detectron2 和 DefaultPredictor 对多个图像进行推理

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

我已经训练了模型,现在我想用它来检测许多图像中的对象。我看到 defaultpredictor 只允许你检测图像,我该怎么办?

我对这个世界真的很陌生。我尝试的方法是使用 for 循环,但它不起作用。还有其他方法吗?

%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)

好的,我是这样解决的:

%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)

我删除了os.chdir()