如何让X服务器在detectron2中显示图像?

我正在关注detectron2入门教程,以使用其机器学习模型之一来检测图像中的对象。我正在使用AWS机器学习AMI和jupyter笔记本进行此操作。 该模型成功检测到图像中的一个对象,但是openCV似乎无法正常工作,因为我的控制台没有输出。我得到的错误是无法连接到X服务器。

Reading a file from 'Detectron2 Model Zoo'
[07/12 20:03:32 detectron2]: input1.jpg: detected 1 instances in 0.45s : 
cannot connect to X server /home/ubuntu/anaconda3/lib/python3.7/multiprocessing/semaphore_tracker.py:144: UserWarning: semaphore_tracker: There appear to be 1 leaked semaphores to clean up at shutdown len(cache)) ubuntu@ip-10-0-0-225:~/detectron2/detectron2/demo$

这是我用来运行模型的命令:

python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
--input input1.jpg input2.jpg \
--opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

这是demo.py文件:

# Copyright (c) Facebook,Inc. and its affiliates. All Rights Reserved
import argparse
import glob
import multiprocessing as mp
import os
import time
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

from predictor import VisualizationDemo

# constants
WINDOW_NAME = "COCO detections"


def setup_cfg(args):
    # load config from file and command-line arguments
    cfg = get_cfg()
    cfg.merge_from_file(args.config_file)
    cfg.merge_from_list(args.opts)
    # Set score_threshold for builtin models
    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 models")
    parser.add_argument(
        "--config-file",default="configs/quick_schedules/mask_rcnn_R_50_FPN_inference_acc_test.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="+",help="A list of space separated input images; "
        "or a single glob pattern such as 'directory/*.jpg'",)
    parser.add_argument(
        "--output",help="A file or directory to save output visualizations. "
        "If not given,will show output in an OpenCV window.",)

    parser.add_argument(
        "--confidence-threshold",type=float,default=0.5,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=[],nargs=argparse.REMAINDER,)
    return parser


if __name__ == "__main__":
    mp.set_start_method("spawn",force=True)
    args = get_parser().parse_args()
    setup_logger(name="fvcore")
    logger = setup_logger()
    logger.info("Arguments: " + str(args))

    cfg = setup_cfg(args)

    demo = VisualizationDemo(cfg)

    if args.input:
        if len(args.input) == 1:
            args.input = glob.glob(os.path.expanduser(args.input[0]))
            assert args.input,"The input path(s) was not found"
        for path in tqdm.tqdm(args.input,disable=not args.output):
            # use PIL,to be consistent with evaluation
            img = read_image(path,format="BGR")
            start_time = time.time()
            predictions,visualized_output = demo.run_on_image(img)
            logger.info(
                "{}: {} in {:.2f}s".format(
                    path,"detected {} instances".format(len(predictions["instances"]))
                    if "instances" in predictions
                    else "finished",time.time() - start_time,)
            )

            if args.output:
                if os.path.isdir(args.output):
                    assert os.path.isdir(args.output),args.output
                    out_filename = os.path.join(args.output,os.path.basename(path))
                else:
                    assert len(args.input) == 1,"Please specify a directory with args.output"
                    out_filename = args.output
                visualized_output.save(out_filename)
            else:
                cv2.namedwindow(WINDOW_NAME,cv2.WINDOW_NORMAL)
                cv2.imshow(WINDOW_NAME,visualized_output.get_image()[:,:,::-1])
                if cv2.waitKey(0) == 27:
                    break  # esc to quit
  

    
iCMS 回答:如何让X服务器在detectron2中显示图像?

  1. 我重新安装了 QT

apt-get install python3-pyqt5

之后,我试试这个

apt-get install pyqt5-dev-tools 和 apt-get install qttools5-dev-tools

  1. 然后我安装 xvfb 并尝试这个

Xvfb :1 -screen 0 1920x1080x24+32 -fbdir /var/tmp &

并设置显示

导出显示=:1

  1. 重启

  2. 再次运行你的代码

本文链接:https://www.f2er.com/1949920.html

大家都在问