0

我已经在自定义数据集上训练了一个 Faster RCNN 模型以进行对象检测,并希望在Videos上对其进行测试。我可以在图像上测试结果,但被困在如何为视频做这件事上。

这是图像推理的代码:

cfg.MODEL.WEIGHTS = os.path.join(cfg.OUTPUT_DIR, "model_final.pth")
cfg.DATASETS.TEST = ("my_dataset_test", )
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7   # set the testing threshold for this model
predictor = DefaultPredictor(cfg)
test_metadata = MetadataCatalog.get("my_dataset_test")
from detectron2.utils.visualizer import ColorMode
import glob

for imageName in glob.glob('/content/test/*jpg'):
  im = cv2.imread(imageName)
  outputs = predictor(im)
  v = Visualizer(im[:, :, ::-1],
                metadata=test_metadata, 
                scale=0.8
                 )
  out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
  cv2_imshow(out.get_image()[:, :, ::-1])

请有人让我知道如何调整此代码以检测视频?

使用平台:谷歌 Colab

技术栈:Detectron 2、Pytorch

4

1 回答 1

0

检查这个循环:

from detectron2.utils.visualizer import ColorMode
import glob
import cv2
from google.colab.patches import cv2_imshow

cap = cv2.VideoCapture('/path/to/video')
while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        break

    outputs = predictor(im)
    v = Visualizer(im[:, :, ::-1],
    metadata=test_metadata, 
    scale=0.8
    )
    out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
    cv2_imshow(out.get_image()[:, :, ::-1])
    
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
于 2021-06-23T16:01:48.537 回答