Ejemplo n.º 1
0
    def detect(self, im, id):
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(id):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                self.model, im, None, timers=timers)

        self.logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            self.logger.info(' | {}: {:.3f}s'.format(k, v.average_time))

        imsw = vis_utils.vis_one_image_opencv(im,
                                              cls_boxes,
                                              cls_segms,
                                              cls_keyps,
                                              self.confidence,
                                              2,
                                              show_box=True,
                                              dataset=self.dummy_coco_dataset,
                                              show_class=True)
        boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format(
            cls_boxes, cls_segms, cls_keyps)
        bboxes = BBoxDetArray()
        bboxes.header = std_msgs.msg.Header()
        if boxes is not None:
            for i in range(len(boxes)):
                box = boxes[i][0:4]
                score = boxes[i][4]
                cls = self.dummy_coco_dataset.classes[classes[i]]
                if (score >= self.confidence):
                    bbox = BBox(box[0], box[1], box[2], box[3])
                    bbox_det = BBoxDet(bbox, score, cls)
                    bboxes.bboxes.append(bbox_det)
            return imsw, bboxes
Ejemplo n.º 2
0
def main(args):
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, frame, None, timers=timers)
        image = vis_utils.vis_one_image_opencv(np.array(frame),
                                               cls_boxes,
                                               cls_segms,
                                               cls_keyps,
                                               thresh=0.7,
                                               kp_thresh=2,
                                               show_box=True,
                                               show_class=True)
        cv2.imshow('camera', image)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        print("Time:", time.time() - t)
    cap.release()
    cv2.destroyAllWindows()
Ejemplo n.º 3
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.TEST.WEIGHTS = args.weights
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg()
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    for i, im_name in enumerate(im_list):
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.pdf'))
        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)')
        t1 = time.time()
        im = vis_utils.vis_one_image_opencv(im,
                                            cls_boxes,
                                            segms=cls_segms,
                                            keypoints=cls_keyps,
                                            thresh=0.7,
                                            kp_thresh=2,
                                            show_box=True,
                                            dataset=dummy_coco_dataset,
                                            show_class=True)
        # vis_utils.vis_one_image(
        #     im[:, :, ::-1],  # BGR -> RGB for visualization
        #     im_name,
        #     args.output_dir,
        #     cls_boxes,
        #     cls_segms,
        #     cls_keyps,
        #     dataset=dummy_coco_dataset,
        #     box_alpha=0.3,
        #     show_class=True,
        #     thresh=0.7,
        #     kp_thresh=2
        # )
        t2 = time.time() - t1
        print("vis time %f ms" % (t2 * 1000))
        cv2.imwrite(
            args.output_dir + '/' + im_name.split('/')[-1].split('.')[0] +
            '.jpg', im)
Ejemplo n.º 4
0
def _vis_single_frame(im, cls_boxes_i, cls_segms_i, cls_keyps_i, cls_tracks_i, thresh):
    res = vis_utils.vis_one_image_opencv(
        im, cls_boxes_i,
        segms=cls_segms_i, keypoints=cls_keyps_i,
        tracks=cls_tracks_i, thresh=thresh,
        show_box=True, show_class=False, linewidth = 1)
    if res is None:
        return im
    return res
Ejemplo n.º 5
0
def test(model, dataset, start_ind, end_ind, logger):
    all_boxes = []
    all_segms = []
    all_parss = []
    all_pscores = []
    num_img = cfg.TEST.IMS_PER_GPU
    with torch.no_grad():
        for i in range(start_ind, end_ind, num_img):
            logger.iter_tic()
            logger.data_tic()
            ims = []
            image_ids = []
            for j in range(i, i + num_img):
                if j == end_ind:
                    break
                im = dataset.pull_image(j)
                ims.append(im)
                image_ids.append(j)
            logger.data_toc()
            logger.infer_tic()
            result, features = rcnn_test.im_detect_bbox(model, ims)
            if cfg.MODEL.MASK_ON:
                result = rcnn_test.im_detect_mask(model, result, features)
            if cfg.MODEL.PARSING_ON:
                result = rcnn_test.im_detect_parsing(model, result, features)
            logger.infer_toc()
            logger.post_tic()
            eval_results, ims_results = post_processing(
                result, image_ids, dataset)
            box_results, seg_results, par_results, par_score = eval_results
            ims_dets, ims_labels, ims_segs, ims_pars = ims_results
            if cfg.VIS.ENABLED:
                for k, im in enumerate(ims):
                    if len(ims_dets) == 0:
                        break
                    im_name = dataset.get_img_info(image_ids[k])['file_name']
                    vis_im = vis_utils.vis_one_image_opencv(
                        im,
                        cfg,
                        ims_dets[k],
                        ims_labels[k],
                        segms=ims_segs[k],
                        parsing=ims_pars[k],
                        dataset=dataset,
                    )
                    cv2.imwrite(
                        os.path.join(cfg.CKPT, 'vis', '{}'.format(im_name)),
                        vis_im)
            all_boxes += box_results
            all_segms += seg_results
            all_parss += par_results
            all_pscores += par_score
            logger.post_toc()
            logger.iter_toc()
            logger.log_stats(i, start_ind, end_ind, len(dataset))

    return all_boxes, all_segms, all_parss, all_pscores
def _vis_single_frame(im, cls_boxes_i, cls_segms_i, cls_keyps_i, cls_tracks_i, thresh):
    res = vis_utils.vis_one_image_opencv(
        im, cls_boxes_i,
        segms=cls_segms_i, keypoints=cls_keyps_i,
        tracks=cls_tracks_i, thresh=thresh,
        show_box=True, show_class=False, linewidth=3)
    if res is None:
        return im
    return res
Ejemplo n.º 7
0
def vis(im_name, im, cls_boxes, cls_segms, cls_keyps):
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    out_dir = None

    loaded = vis_utils.vis_one_image_opencv(im,
                                            cls_boxes,
                                            segms=cls_segms,
                                            keypoints=cls_keyps,
                                            thresh=0.9,
                                            kp_thresh=2,
                                            show_box=False,
                                            dataset=None,
                                            show_class=False)
    misc.imsave("loaded.png", loaded)
Ejemplo n.º 8
0
def test(model, dataset, start_ind, end_ind, all_hooks):
    total_num_images = len(dataset)
    all_boxes = []

    num_img = cfg.TEST.IMS_PER_GPU
    with torch.no_grad():
        for i in range(start_ind, end_ind, num_img):
            all_hooks.iter_tic()

            all_hooks.data_tic()
            ims = []
            image_ids = []
            for j in range(i, i + num_img):
                if j == end_ind:
                    break
                ims.append(dataset.pull_image(j))
                image_ids.append(j)
            all_hooks.data_toc()

            all_hooks.infer_tic()
            result, features = rcnn_test.im_detect_bbox(model, ims)
            if cfg.MODEL.OPLD_ON:
                result = rcnn_test.im_detect_opld(model, result, features)
            all_hooks.infer_toc()

            all_hooks.post_tic()
            eval_results, ims_results = post_processing(
                result, image_ids, dataset)
            box_results = eval_results
            ims_dets, ims_labels = ims_results
            if cfg.VIS.ENABLED:
                for k, im in enumerate(ims):
                    if len(ims_dets) == 0:
                        break
                    im_name = dataset.get_img_info(image_ids[k])['file_name']
                    vis_im = vis_utils.vis_one_image_opencv(
                        im,
                        cfg.VIS,
                        boxes=ims_dets[k],
                        classes=ims_labels[k],
                        dataset=dataset)
                    cv2.imwrite(
                        os.path.join(cfg.CKPT, 'vis', '{}'.format(im_name)),
                        vis_im)
            all_boxes += box_results
            all_hooks.post_toc()
            all_hooks.iter_toc()
            all_hooks.log_stats(i, start_ind, end_ind, total_num_images)

    return all_boxes
Ejemplo n.º 9
0
    def draw_on_image(self,
                      image,
                      boxes,
                      segments,
                      keypoints,
                      threshold=THRESHOLD):
        if isinstance(image, str):
            image = cv2.imread(image)

        result = vis_utils.vis_one_image_opencv(im=image,
                                                boxes=boxes,
                                                keypoints=keypoints,
                                                segms=segments,
                                                thresh=threshold,
                                                dataset=self.__dataset,
                                                show_class=False,
                                                show_box=True)

        return result
Ejemplo n.º 10
0
def detectron_vizualize(config,
                        frame: FrameType,
                        cls_boxes: bytes,
                        cls_segms: bytes,
                        cls_keyps: bytes) -> FrameType:
        cls_boxes = pickle.loads(cls_boxes)
        cls_segms = pickle.loads(cls_segms)
        cls_keyps = pickle.loads(cls_keyps)

        vis_im = vis_utils.vis_one_image_opencv(
            frame[:, :, ::1],  # BGR -> RGB for visualization
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_datasets.get_coco_dataset(),
            show_class=True,
            thresh=0.7,
            kp_thresh=2)

        return vis_im
Ejemplo n.º 11
0
def draw_bbox_mask_pose(im,
                        boxes,
                        segms=None,
                        keypoints=None,
                        thresh=0.7,
                        kp_thresh=2,
                        show_box=False,
                        dataset=None,
                        show_class=False):
    vis_result = vis_utils.vis_one_image_opencv(
        im[:, :, ::-1],  # BGR -> RGB for visualization
        boxes,
        segms=segms,
        keypoints=keypoints,
        thresh=thresh,
        kp_thresh=kp_thresh,
        show_box=show_box,
        dataset=dataset,
        show_class=show_class)

    return vis_result
def _vis_single_frame(im,
                      cls_boxes_i,
                      cls_segms_i,
                      cls_keyps_i,
                      cls_tracks_i,
                      thresh,
                      show_box=False,
                      show_class=False,
                      show_id=False,
                      show_conf=False):
    """
    input:
    :param im: input image as narray
    :param cls_boxes_i: a [N by 5] list containing bounding boxes detected in the image in the form of [x0, y0, x1, y1, conf]
    :param cls_segms_i: segments in the image
    :param cls_keyps_i: a [N by (4, 17)] keypoints detected in the image with the for of nparray[X,Y,scoreX,scoreY]
    :param cls_tracks_i: a list containing the track id for each bounding box
    :param thresh: floating number defining the threshold of confidence level for showing bounding boxes
    :param show_box: boolian parameter to decide showing bounding boxes
    :param show_class: boolian parameter to decide showing class type
    :param show_id: boolian parameter to decide showing track id
    :param show_conf: boolian parameter to decide showing confidence level
    :return:
    image with visualizations
    """
    res = vis_utils.vis_one_image_opencv(im,
                                         cls_boxes_i,
                                         segms=cls_segms_i,
                                         keypoints=cls_keyps_i,
                                         tracks=cls_tracks_i,
                                         thresh=thresh,
                                         kp_thresh=2.3,
                                         show_box=show_box,
                                         show_class=show_class,
                                         show_id=show_id,
                                         show_conf=show_conf,
                                         linewidth=4)
    if res is None:
        return im
    return res
Ejemplo n.º 13
0
def main():
    """main function"""

    if not torch.cuda.is_available():
        sys.exit("Need a CUDA device to run the code.")

    args = parse_args()
    print('Called with args:')
    print(args)

    assert args.image_dir or args.images
    assert bool(args.image_dir) ^ bool(args.images)

    print('load cfg from file: {}'.format(args.cfg_file))
    cfg_from_file(args.cfg_file)

    if args.set_cfgs is not None:
        cfg_from_list(args.set_cfgs)

    assert bool(args.load_ckpt) ^ bool(args.load_detectron), \
        'Exactly one of --load_ckpt and --load_detectron should be specified.'
    cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False  # Don't need to load imagenet pretrained weights
    assert_and_infer_cfg()

    maskRCNN = Generalized_RCNN()

    if args.cuda:
        maskRCNN.cuda()

    if args.load_ckpt:
        load_name = args.load_ckpt
        print("loading checkpoint %s" % (load_name))
        checkpoint = torch.load(load_name,
                                map_location=lambda storage, loc: storage)
        net_utils.load_ckpt(maskRCNN, checkpoint['model'])

    if args.load_detectron:
        print("loading detectron weights %s" % args.load_detectron)
        load_detectron_weight(maskRCNN, args.load_detectron)

    maskRCNN = mynn.DataParallel(maskRCNN,
                                 cpu_keywords=['im_info', 'roidb'],
                                 minibatch=True,
                                 device_ids=[0])  # only support single GPU

    maskRCNN.eval()
    if args.image_dir:
        imglist = misc_utils.get_imagelist_from_dir(args.image_dir)
    else:
        imglist = args.images
    num_images = len(imglist)
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    for i, im_name in enumerate(imglist):

        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.png'))
        print('Processing {} -> {}'.format(im_name, out_name))

        im = cv2.imread(im_name)
        assert im is not None

        timers = defaultdict(Timer)

        cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN,
                                                        im,
                                                        timers=timers)

        im = vis_utils.vis_one_image_opencv(im,
                                            cls_boxes,
                                            cls_segms,
                                            cls_keyps,
                                            thresh=0.5,
                                            show_box=True,
                                            show_class=True)
        cv2.imwrite(out_name, im)

    if args.merge_pdfs and num_images > 1:
        merge_out_path = '{}/results.pdf'.format(args.output_dir)
        if os.path.exists(merge_out_path):
            os.remove(merge_out_path)
        command = "pdfunite {}/*.pdf {}".format(args.output_dir,
                                                merge_out_path)
        subprocess.call(command, shell=True)
Ejemplo n.º 14
0
def main(args):

    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)

    cfg.NUM_GPUS = 1
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)

    assert_and_infer_cfg()

    model = infer_engine.initialize_model_from_cfg(args.weights)

    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.video_name):
        print("video_name", args.video_name)
    else:
        print("video is not existing")

    cap = cv2.VideoCapture(args.video_name)

    while cap.isOpened():

        ret, frame = cap.read()

        if not ret:
            break

        frame = cv2.resize(frame, dsize=(1280, 720))

        timers = defaultdict(Timer)
        t1 = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, frame, None, timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t1))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))

        frame = vis_utils.vis_one_image_opencv(frame,
                                               cls_boxes,
                                               segms=cls_segms,
                                               keypoints=cls_keyps,
                                               thresh=0.8,
                                               kp_thresh=2,
                                               show_box=True,
                                               dataset=dummy_coco_dataset,
                                               show_class=True)
        t2 = time.time()
        durr = float(t2 - t1)
        fps = 1.0 / durr
        cv2.putText(frame, "fps:%.3f" % fps, (20, 20), 4, 0.5, (0, 255, 0), 1,
                    cv2.LINE_AA)
        cv2.imshow('Detection', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()
Ejemplo n.º 15
0
def predict_dataset(project,
                    out_dir="/tmp/predictions/",
                    visualize=False,
                    visualize_dataset="ade",
                    randomize=False):
    if visualize:
        vis_dir = os.path.join(out_dir, "vis")
        if visualize_dataset == "ade":
            dummy_dataset = dummy_datasets.get_ade_dataset()
        else:
            dummy_dataset = dummy_datasets.get_coco_dataset()

    config = projects.get_config(args.project)
    img_dir = config["images"]
    pkl_dir = os.path.join(out_dir, "pkl")

    im_list = [line.rstrip() for line in open(config["im_list"], 'r')]
    if args.randomize:
        # Shuffle image list
        random.seed(3)
        random.shuffle(im_list)

    for i, im_name in enumerate(im_list):
        img_path = os.path.join(img_dir, im_name)
        img_basename = os.path.splitext(im_name)[0]
        pkl_path = os.path.join(pkl_dir, img_basename + '.pkl')
        if os.path.exists(pkl_path):
            print("Already done")
            continue

        logger.info('Processing {} -> {}'.format(im_name, vis_path))
        logger.info('{}/{}'.format(i, len(im_list)))

        # Predict
        im = cv2.imread(img_path)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)')

        # Save prediction pickle
        pkl_obj = (cls_boxes, cls_segms, cls_keyps)
        if not os.path.isdir(os.path.dirname(pkl_path)):
            os.makedirs(os.path.dirname(pkl_path))
        pickle.dump(pkl_obj, open(pkl_path, "wb"))

        if visualize:
            vis_path = os.path.join(vis_dir, img_basename + '.png')
            if not os.path.isdir(os.path.dirname(vis_path)):
                os.makedirs(os.path.dirname(vis_path))

            vis_image = vis_utils.vis_one_image_opencv(im[:, :, ::-1],
                                                       cls_boxes,
                                                       cls_segms,
                                                       cls_keyps,
                                                       thresh=0,
                                                       kp_thresh=2,
                                                       dataset=dummy_dataset,
                                                       show_box=True,
                                                       show_class=True)
            cv2.imwrite(vis_path, vis_image)
Ejemplo n.º 16
0
    vis_utils.vis_one_image(
        im[:, :, ::-1],  # BGR -> RGB for visualization
        args.output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2)
    im = vis_utils.vis_one_image_opencv(im,
                                        cls_boxes,
                                        cls_segms,
                                        cls_keyps,
                                        dataset=dummy_coco_dataset,
                                        thresh=0.7,
                                        show_class=True)
    classxy = vis_utils.vis_one_image_opencv2(im,
                                              cls_boxes,
                                              cls_segms,
                                              cls_keyps,
                                              dataset=dummy_coco_dataset,
                                              thresh=0.7,
                                              show_class=True)

    if type(classxy[0][0]) == np.ndarray:
        pass
    else:
        #print ("***********************IMIM****IMIM")
Ejemplo n.º 17
0
def main(args):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    # get the weight path
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    if os.path.isdir(args.im_or_folder):
        # glob.iglob will generate a iterator that has same elements as that in glob.glob
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    for i, im_name in enumerate(im_list):
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.pdf'))
        logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        logger.info('Inference time: {:.3f}s'.format(time.time() - t))
        for k, v in timers.items():
            logger.info(' | {}: {:.3f}s'.format(k, v.average_time))
        if i == 0:
            logger.info(
                ' \ Note: inference on the first image will be slower than the '
                'rest (caches and auto-tuning need to warm up)')

        # save as pdf using matplotlib.pyplot

        proc_im = vis_utils.vis_one_image_opencv(im,
                                                 cls_boxes,
                                                 dataset=dummy_coco_dataset,
                                                 thresh=0.7,
                                                 show_box=True,
                                                 show_class=True)

        cv2.imshow('img', proc_im)
        cv2.waitKey(0)
        """
        vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2
        )
        """
    cv2.destroyWindow('img')
def main(args):
    
    """A dummy COCO dataset that includes only the 'classes' field."""
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    
    ''''load initial Detectron config system'''
    cfg_orig = yaml.load(yaml.dump(cfg))
    
    print("video is :",args.video_name)
    cap = cv2.VideoCapture(args.video_name)
            
    while cap.isOpened():
        
        ret, frame = cap.read()

        if not ret:
            break
        
        t1 = time.time()
        frame = cv2.resize(frame,dsize=(1280,720))  

        if args.rpn_pkl is not None:
            
            proposal_boxes, _proposal_scores = get_rpn_box_proposals(frame, args)
            
            workspace.ResetWorkspace()
            
        else:
            proposal_boxes = None

        cls_boxes, cls_segms, cls_keyps = None, None, None
        
        for i in range(0, len(args.models_to_run), 2):
            pkl = args.models_to_run[i]
            yml = args.models_to_run[i + 1]
            
            cfg.immutable(False)
            
            '''load initial global Detectron config system'''
            merge_cfg_from_cfg(cfg_orig)
            
            """Load a yaml config file and merge it into the global config."""
            merge_cfg_from_file(yml)
            
            if len(pkl) > 0:
                weights_file = pkl
            else:
                weights_file = cfg.TEST.WEIGHTS
                
            '''Number of GPUs to use'''    
            cfg.NUM_GPUS = 1
            
            assert_and_infer_cfg()
            
            '''Initialize a model from the global cfg.'''
            model = model_engine.initialize_model_from_cfg(weights_file)
            
            with c2_utils.NamedCudaScope(0):
                '''Inference detecting all'''
                cls_boxes_, cls_segms_, cls_keyps_ = model_engine.im_detect_all(model, frame, proposal_boxes)
                
            cls_boxes = cls_boxes_ if cls_boxes_ is not None else cls_boxes
            cls_segms = cls_segms_ if cls_segms_ is not None else cls_segms
            cls_keyps = cls_keyps_ if cls_keyps_ is not None else cls_keyps
            workspace.ResetWorkspace()
            
        """Constructs a numpy array with the detections visualized."""    
        frame = vis_utils.vis_one_image_opencv(
                               frame, 
                               cls_boxes, 
                               segms=cls_segms, 
                               keypoints=cls_keyps, 
                               thresh=0.8, 
                               kp_thresh=2,
                               show_box=True, 
                               dataset=dummy_coco_dataset, 
                               show_class=True)
                               
                               
        t2 = time.time()
        durr = float(t2-t1)
        fps = 1.0 / durr
        cv2.putText(frame,"fps:%.3f"%fps,(20,20),4, 0.5, (0, 255, 0), 1, cv2.LINE_AA)
        cv2.imshow('Detection', frame)
       
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
       
    cap.release()    
    cv2.destroyAllWindows()
def main(args):

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    # get the weight path
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    #dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    img_list = os.listdir(args.img_dir)
    img_list = sorted(img_list)
    print("image dir: %s" % args.img_dir)
    video_len = len(img_list)
    fps = 25
    width = 640
    height = 512

    #video_file = args.video_file
    #print("video: %s" % video_file)
    #cap = cv2.VideoCapture(video_file)
    #video_len = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    #fps = int(cap.get(cv2.CAP_PROP_FPS))
    #width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    #height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    terminate, is_paused = False, False

    idx = 0
    # I have to compute fourcc in advance, because there will be error
    # Expected single character string for argument 'c1', I have't figure out how to solve it
    #fourcc = cv2.VideoWriter_fourcc(*"mp4v")
    # XVID: 1145656920
    # mp4v: 1983148141
    fourcc = 1983148141
    #video_basename = os.path.basename(video_file)
    #video_name, video_ext = os.path.splitext(video_basename)
    #output_name = video_name + '_processed' + video_ext
    output_name = 'downtown_night_new.mp4'
    output_dir = args.output_dir
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    output_file = os.path.join(output_dir, output_name)
    video_writer = cv2.VideoWriter(output_file, fourcc, fps, (width, height))

    while not terminate:
        print("processing: %.4f%%" % ((idx + 1) * 100.0 / video_len))
        #print("debug")

        if not is_paused:
            img_path = os.path.join(args.img_dir, img_list[idx])
            frame = cv2.imread(img_path)
            idx += 1
            with c2_utils.NamedCudaScope(0):
                cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                    model, frame, None, None)

        new_frame = vis_utils.vis_one_image_opencv(frame,
                                                   cls_boxes,
                                                   thresh=0.7,
                                                   show_box=True)
        video_writer.write(new_frame)
        if idx == video_len:
            terminate = True
        """
        cv2.imshow('image', new_frame)
        key = cv2.waitKey(1)

        if key & 255 == 27:  # ESC
            print("terminating")
            terminate = True
        elif key & 255 == 32:  # ' '
            print("toggeling pause: " + str(not is_paused))
            is_paused = not is_paused
        elif key & 255 == 115:  # 's'
            print("stepping")
            ret, frame = cap.read()
            if not ret:
                break
            with c2_utils.NamedCudaScope(0):
                cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                    model, frame, None, None
                )
            is_paused = True
        """

    video_writer.release()
Ejemplo n.º 20
0
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms, ###############cls_segms######################
            cls_keyps,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2
        )
 '''
        vis_utils.vis_one_image_opencv(
            im[:, :, ::-1], 
            cls_boxes, 
            cls_segms, 
            cls_keyps, 
            thresh=0.7, 
            kp_thresh=2,
            show_box=False, 
            dataset=dummy_coco_dataset, 
            show_class=True
        )


if __name__ == '__main__':
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    utils.logging.setup_logging(__name__)
    args = parse_args()
    main(args)