如何使用Facebook的detectron2模型检测天空?

时间:2020-04-02 01:07:59

标签: python facebook image-processing model sky

我一直打算将Facebook的detectron2模型用于自定义目的。我需要从图片中检测天空,并且仅根据图片中是否包含天空来获得是或否的输出。但是现在我不明白如何获得这个特定的输出。我可以检测到图片中的物体(包括天空),但是我不知道如何修改输出,因为它只能告诉它是否找到了“天空”类。

这是我的完整代码:

from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
import numpy as np
import cv2
import os
im = cv2.imread("ToTest.jpg")

# Create config
cfg = get_cfg()
cfg.merge_from_file("https://github.com/facebookresearch/detectron2/blob/master/configs/COCO-PanopticSegmentation/panoptic_fpn_R_50_1x.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
cfg.MODEL.DEVICE = "cpu"
cfg.MODEL.WEIGHTS = "https://dl.fbaipublicfiles.com/detectron2/COCO-PanopticSegmentation/panoptic_fpn_R_50_1x/139514544/model_final_dbfeb4.pkl"

# Create predictor
predictor = DefaultPredictor(cfg)


# Make prediction
outputs = predictor(im)
#print(outputs)

print(outputs["instances"].pred_classes)
print(outputs["instances"].pred_boxes)

# We can use `Visualizer` to draw the predictions on the image.
panoptic_seg, segments_info = predictor(im)["panoptic_seg"]
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
v = v.draw_panoptic_seg_predictions(panoptic_seg.to("cpu"), segments_info)

directory = r"directory"
filename = 'savedOutput.jpg'
cv2.imwrite(filename, v.get_image()[:,:,::-1])

最后,我只发现这段代码告诉了一些有关编辑输出的内容,但是我根本听不懂。也许有人可以帮我这个忙。

images = ImageList.from_tensors(...)  # preprocessed input tensor
model = build_model(cfg)
features = model.backbone(images.tensor)
proposals, _ = model.proposal_generator(images, features)
instances = model.roi_heads._forward_box(features, proposals)
mask_features = [features[f] for f in model.roi_heads.in_features]
mask_features = model.roi_heads.mask_pooler(mask_features, [x.pred_boxes for x in instances])

0 个答案:

没有答案