| |
| import argparse |
| import glob |
| import multiprocessing as mp |
| import numpy as np |
| import os |
| import tempfile |
| import time |
| import warnings |
| import cv2 |
| import tqdm |
|
|
| from detectron2.config import get_cfg |
| from detectron2.data.detection_utils import read_image |
| from detectron2.utils.logger import setup_logger |
|
|
| import sys |
| sys.path.append('.') |
| from VLPart.vlpart.config import add_vlpart_config |
|
|
| from VLPart.demo.predictor import VisualizationDemo |
|
|
|
|
| |
| WINDOW_NAME = "image demo" |
|
|
|
|
| def setup_cfg(args): |
| |
| cfg = get_cfg() |
| add_vlpart_config(cfg) |
| cfg.merge_from_file(args.config_file) |
| cfg.merge_from_list(args.opts) |
| |
| cfg.MODEL.RETINANET.SCORE_THRESH_TEST = args.confidence_threshold |
| cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = args.confidence_threshold |
| cfg.MODEL.PANOPTIC_FPN.COMBINE.INSTANCES_CONFIDENCE_THRESH = args.confidence_threshold |
| cfg.freeze() |
| return cfg |
|
|
|
|
| def get_parser(): |
| parser = argparse.ArgumentParser(description="Detectron2 demo for builtin configs") |
| parser.add_argument( |
| "--config-file", |
| default="VLPart/configs/joint/swinbase_cascade_lvis_paco_pascalpart_partimagenet.yaml", |
| metavar="FILE", |
| help="path to config file", |
| ) |
| parser.add_argument("--webcam", action="store_true", help="Take inputs from webcam.") |
| parser.add_argument("--video-input", help="Path to video file.") |
| parser.add_argument( |
| "--input", |
| nargs="+", |
| default='', |
| help="A list of space separated input images; " |
| "or a single glob pattern such as 'directory/*.jpg'", |
| ) |
| parser.add_argument( |
| "--output", |
| default='', |
| help="A file or directory to save output visualizations. " |
| "If not given, will show output in an OpenCV window.", |
| ) |
| parser.add_argument( |
| "--vocabulary", |
| default="custom", |
| choices=['pascal_part', 'partimagenet', 'paco', |
| 'voc', 'coco', 'lvis', |
| 'pascal_part_voc', 'lvis_paco', 'custom'], |
| help="", |
| ) |
| parser.add_argument( |
| "--custom_vocabulary", |
| default="", |
| help="", |
| ) |
| parser.add_argument( |
| "--confidence-threshold", |
| type=float, |
| default=0.7, |
| help="Minimum score for instance predictions to be shown", |
| ) |
|
|
| parser.add_argument( |
| "--opts", |
| help="Modify config options using the command-line 'KEY VALUE' pairs", |
| default=['MODEL.WEIGHTS', "/data/VLPart/ckpts/swinbase_cascade_lvis_paco_pascalpart_partimagenet.pth", "VIS.BOX", False], |
| nargs=argparse.REMAINDER, |
| ) |
| return parser |
|
|
| def build_vlpart_model(custom_vocabulary): |
|
|
| mp.set_start_method("spawn", force=True) |
| args = get_parser().parse_args() |
| args.custom_vocabulary = custom_vocabulary |
| setup_logger(name="fvcore") |
| logger = setup_logger() |
| logger.info("Arguments: " + str(args)) |
|
|
| cfg = setup_cfg(args) |
| model = VisualizationDemo(cfg, args) |
|
|
| return model |
|
|