Beispiel #1
0
def cache_cfg_urls():
    """Download URLs in the config, cache them locally, and rewrite cfg to make
    use of the locally cached file.
    """
    __C.TRAIN.WEIGHTS = cache_url(__C.TRAIN.WEIGHTS, __C.DOWNLOAD_CACHE)
    __C.TEST.WEIGHTS = cache_url(__C.TEST.WEIGHTS, __C.DOWNLOAD_CACHE)
    __C.TRAIN.PROPOSAL_FILES = tuple(
        cache_url(f, __C.DOWNLOAD_CACHE) for f in __C.TRAIN.PROPOSAL_FILES
    )
    __C.TEST.PROPOSAL_FILES = tuple(
        cache_url(f, __C.DOWNLOAD_CACHE) for f in __C.TEST.PROPOSAL_FILES
    )
def generate_predicitions_from_frames(images, config_file, weights):
    """Generator yields inferred boxes and keypoints for each image in a provided iterable of images
    Args:
        images: iterable images
        config_file: Detectron configuration file
        weights: pretrained weights
    Returns:
        yields i, im, cls_boxes, cls_segms, cls_keyps
    """
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(config_file)
    cfg.NUM_GPUS = 1
    weights = cache_url(weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(weights)
    for i, im in enumerate(images):
        logger.info("Processing frame {}".format(i))
        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)"
            )
        yield i, im, cls_boxes, cls_segms, cls_keyps
Beispiel #3
0
def check_args(args):
    assert ((args.rpn_pkl is not None and args.rpn_cfg is not None)
            or (args.rpn_pkl is None and args.rpn_cfg is None))
    if args.rpn_pkl is not None:
        args.rpn_pkl = cache_url(args.rpn_pkl, cfg.DOWNLOAD_CACHE)
        assert os.path.exists(args.rpn_pkl)
        assert os.path.exists(args.rpn_cfg)
    if args.models_to_run is not None:
        assert len(args.models_to_run) % 2 == 0
        for i, model_file in enumerate(args.models_to_run):
            if len(model_file) > 0:
                if i % 2 == 0:
                    model_file = cache_url(model_file, cfg.DOWNLOAD_CACHE)
                    args.models_to_run[i] = model_file
                assert os.path.exists(model_file), \
                    '\'{}\' does not exist'.format(model_file)
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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    pool = mp.Pool(mp.cpu_count())
    func = partial(process_image, model, args)
    pool.map(func, [im_name for im_name in im_list])
    pool.close()
    pool.join()
Beispiel #5
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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        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):
        output_image_dir = os.path.join(args.output_dir, 'images')
        out_name = os.path.join(
            output_image_dir,
            '{}'.format(os.path.basename(im_name) + '.' + args.output_ext))
        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)')

        # vis_utils.vis_one_image(
        #    im[:, :, ::-1],  # BGR -> RGB for visualization
        #    im_name,
        #    output_image_dir,
        #    cls_boxes,
        #    cls_segms,
        #    cls_keyps,
        #    dataset=dummy_coco_dataset,
        #    box_alpha=0.3,
        #    show_class=True,
        #    thresh=0.7,
        #    kp_thresh=2,
        #    ext=args.output_ext,
        #    out_when_no_box=args.out_when_no_box
        #)

        _write_to_txt(cls_boxes, cls_segms, cls_keyps, im_name,
                      dummy_coco_dataset)
Beispiel #6
0
 def __init__(self, cfg_path, weights_path=""):
     logger = logging.getLogger(__name__)
     merge_cfg_from_file(args.cfg)
     cfg.NUM_GPUS = 1
     weights = cache_url(weights_path, cfg.DOWNLOAD_CACHE)
     assert_and_infer_cfg(cache_urls=False)
     self.model = infer_engine.initialize_model_from_cfg(weights)
     self.dummy_coco_dataset = dummy_datasets.get_coco_dataset()
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 4
    args.weights = cache_url(args.weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    # model = infer_engine.initialize_model_from_cfg(args.weights,gpu_id=3)
    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]
    len_str = str(len(os.listdir(args.im_or_folder)))
    for i, im_name in enumerate(im_list):
        print('~~~~~~~~~~~~~~~~~' + str(i) + '/' + len_str +
              "~~~~~~~~~~~~~~~~~~~~~")
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.jpg'))
        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(  #此处输出的cls_boxes包含类别信息
                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)')
        # ipdb.set_trace()
        # print(cls_boxes)
        # print(cls_segms)
        # print(cls_keyps)
        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.5,
            kp_thresh=2,
            ext='jpg')
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    cam = cv2.VideoCapture(0)

    # Set Texture
    Tex_Atlas = cv2.imread(
        'DensePoseData/demo_data/texture_from_SURREAL.png')[:, :, ::-1] / 255.0
    #Tex_Atlas = cv2.imread('DensePoseData/demo_data/texture_atlas_200.png')[:,:,::-1]
    TextureIm = np.zeros([24, 200, 200, 3])
    #
    for i in range(4):
        for j in range(6):
            TextureIm[(6 * i +
                       j), :, :, :] = Tex_Atlas[(200 * j):(200 * j + 200),
                                                (200 * i):(200 * i + 200), :]

    while True:
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        retval, im = cam.read()
        #imsmall = cv2.resize(im, None, fx=0.5, fy=0.5)

        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=None)
        iuvout, indsout = vis_utils.vis_webcam(im,
                                               cls_boxes,
                                               cls_segms,
                                               cls_keyps,
                                               cls_bodys,
                                               thresh=0.9,
                                               kp_thresh=2,
                                               dataset=dummy_coco_dataset,
                                               show_class=True)

        iuvout, indsout = vis_utils.vis_webcam(
            im,
            boxes=cls_boxes,
            segms=cls_segms,
            keypoints=cls_keyps,
            body_uv=cls_bodys,
            thresh=0.9,
            kp_thresh=2,
            dpi=200,
            box_alpha=0.3,
            dataset=dummy_coco_dataset,
            show_class=True,
        )
        #cv2.imshow('input', im)
        texout = TransferTexturePure(TextureIm, im, iuvout)
        cv2.imshow('output', texout)
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    submit_result = []
    result_file_name = 'detectron_val_result.txt'

    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)')

        result = vis_utils.vis_one_image_bbox(
            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)
        if result:
            submit_result.extend(result)
        logger.info('Image {}.'.format(i))

    # Write file
    with open(result_file_name, 'wb') as result_file:
        for item in submit_result:
            result_file.write("%s\n" % item)

    logger.info(
        'The result file has been written in {}.'.format(result_file_name))
Beispiel #10
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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        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) + '.' + args.output_ext)
        )
        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)'
            )

        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=args.thresh,
            kp_thresh=args.kp_thresh,
            ext=args.output_ext,
            out_when_no_box=args.out_when_no_box
        )
Beispiel #11
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(cache_urls=False)
    model = model_engine.initialize_model_from_cfg(args.weights)
    start = timeit.default_timer()

    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]

    # extract bboxes from bottom-up attention model
    image_bboxes = {}
    if args.bbox_file is not None:
        image_bboxes = extract_bboxes(args.bbox_file)

    count = 0
    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    for i, im_name in enumerate(im_list):
        im_base_name = os.path.basename(im_name)
        image_id = int(im_base_name.split(".")[0].split("_")[-1])  # for COCO
        if image_id % args.total_group == args.group_id:
            bbox = image_bboxes[image_id] if image_id in image_bboxes else None
            im = cv2.imread(im_name)
            if im is not None:
                outfile = os.path.join(args.output_dir,
                                       im_base_name.replace('jpg', 'npy'))
                lock_folder = outfile.replace('npy', 'lock')
                if not os.path.exists(lock_folder) and os.path.exists(outfile):
                    continue
                if not os.path.exists(lock_folder):
                    os.makedirs(lock_folder)

                result = get_detections_from_im(cfg,
                                                model,
                                                im,
                                                image_id,
                                                args.feat_name,
                                                args.min_bboxes,
                                                args.max_bboxes,
                                                bboxes=bbox)

                np.save(outfile, result)
                os.rmdir(lock_folder)

            count += 1

            if count % 100 == 0:
                end = timeit.default_timer()
                epoch_time = end - start
                print('process {:d} images after {:.1f} s'.format(
                    count, epoch_time))
Beispiel #12
0
def infer(par,thresh):
    logger = logging.getLogger(__name__)
    merge_cfg_from_file(par['config_path'])
    cfg.NUM_GPUS = 1
    par['weight_path'] = cache_url(par['weight_path'], cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(par['weight_path'])

    if os.path.isdir(par['input_img_path']):
        im_list = glob.iglob(par['input_img_path'] + '/*.jpg')
    else:
        im_list = [par['input_img_path']] 

    count = 0
    t_total = 0
    np.set_printoptions(suppress=True) #numpy不以科学计数法输出
    for i, im_name in enumerate(im_list):# i为计数,im_name为图像路径
        out_name = os.path.join(
            par['output_xml_path'], '{}'.format(os.path.basename(im_name.rstrip(".jpg")) + '.xml')
        )
        #logger.info('Processing {} -> {}'.format(im_name, out_name))
        im = cv2.imread(im_name)
        w = float(im.shape[1])
        h = float(im.shape[0])
        #开始计时
        timers = defaultdict(Timer)
        t_start = time.time()
        with c2_utils.NamedCudaScope(par['gpu_id']):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers
            )
        boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format(cls_boxes, cls_segms, cls_keyps)
        if boxes is not None:
            boxes = np.array(boxes)
            # 坐标归一化 ↓
            # boxes[:,0:4] = boxes[:,0:4]/np.array([col,row,col,row])
            # boxes = np.maximum(boxes,0)
            # boxes = np.minimum(boxes,1)
            classes_ = np.array(classes,dtype=int)
            classes_temp = classes_.reshape(1,-1)
            classes = np.transpose(classes_temp)
            res = np.hstack((classes,boxes))  # res中,第一列为类别,2~5列为坐标,第六列为分数
            res = res[res[:,-1]>thresh]
        else:
            res = []
        #结束计时
        t_end = time.time()
        t_total = t_total + (t_end-t_start)
        count = count + 1
        make_xml_file(par,res,w,h,out_name)
    print("Average detection time:",int(1000*t_total/count),"ms/img")
Beispiel #13
0
def check_args(args):
    assert (
        (args.rpn_pkl is not None and args.rpn_cfg is not None) or
        (args.rpn_pkl is None and args.rpn_cfg is None)
    )
    if args.rpn_pkl is not None:
        args.rpn_pkl = cache_url(args.rpn_pkl, cfg.DOWNLOAD_CACHE)
        assert os.path.exists(args.rpn_pkl)
        assert os.path.exists(args.rpn_cfg)
    if args.models_to_run is not None:
        assert len(args.models_to_run) % 2 == 0
        for i, model_file in enumerate(args.models_to_run):
            if len(model_file) > 0:
                if i % 2 == 0:
                    model_file = cache_url(model_file, cfg.DOWNLOAD_CACHE)
                    args.models_to_run[i] = model_file
                assert os.path.exists(model_file), \
                    '\'{}\' does not exist'.format(model_file)
Beispiel #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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    '''
    add onboard cam support 
    '''
    cam = cv2.VideoCapture("nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720,format=(string)I420, framerate=(fraction)30/1 ! nvvidconv flip-method=0 ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
    if cam.isOpened():
        print("camara open succeded")
        im_name = "Detection"
        while True:
            _, im = cam.read();
            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))
            '''
            vis_utils.vis_cam_image(
                im[:, :, ::-1],  # BGR -> RGB for visualization
                im_name,
                cls_boxes,
                cls_segms,
                cls_keyps,
                dataset=dummy_coco_dataset,
                box_alpha=0.3,
                show_class=True,
                thresh=0.7,
                kp_thresh=2
            )
            """
            key=cv2.waitKey(10)
            if key == 27: # Check for ESC key
                cv2.destroyAllWindows()
                break ;
            """
    else:
        print("camera open failed")
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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    logger.info(
        ' \ Note: inference on the first image will be slower than the '
        'rest (caches and auto-tuning need to warm up)'
    )

    while True:
        reply = {}
        #  Wait for next request from client
        im,extra = zmqa.recv(socket)
        if extra is not None and 'fname' in extra:
            print("Received request %s" % extra)
            reply['fname']=extra['fname']
        else:
            print("Received request %s" % extra)
            reply['fname'] = 'input.jpg'
        # set file name
        im_name=reply['fname']
        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))

        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)
        )
        masks=do_one_image_opencv(
            im,  # BGR -> RGB for visualization
            cls_boxes,
            cls_segms,
            cls_keyps,
            thresh=0.7,
            reply=reply
        )
        zmqa.send(socket, masks,extra=reply)
Beispiel #16
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()


    # load match scene
    match_scene_dict = pickle.load(open('{}/pkl/match_scene_intervals_dict.pkl'.format(args.data_dir), 'rb'))
    HW_foreground = set()
    JZ_foreground = set()
    for match in match_scene_dict['HW_foreground']:
        for fid in range(match[1], match[2]):
            HW_foreground.add(fid)
    for match in match_scene_dict['JZ_foreground']:
        for fid in range(match[1], match[2]):
            JZ_foreground.add(fid)

    # detect frame
    cap = cv2.VideoCapture('{}/videos/{}'.format(args.data_dir, args.input_video))
    result_list = []
    fid = 0
    ret = True

    # create dir
    for player in ['HW', 'JZ']:
        dir = '{data_dir}/image/img_{player}'.format(data_dir=args.data_dir, player=player)
        if not os.path.exists(dir):
            os.makedirs(dir)
        dir = '{data_dir}/image/densepose_{player}'.format(data_dir=args.data_dir, player=player)
        if not os.path.exists(dir):
            os.makedirs(dir)

    vid = 65
    while ret:
        if fid % 1000 == 0:
            print("Inferring frame %d" % fid)
        ret, image = cap.read()
        if fid in HW_foreground or fid in JZ_foreground:
            player = 'HW' if fid in HW_foreground else 'JZ'
            img_path = '{data_dir}/image/img_{player}/img_{vid}_{fid}_{player}.jpg'.format(data_dir=args.data_dir, player=player, vid=vid, fid=fid)
            pose_path = '{data_dir}/image/densepose_{player}/densepose_{vid}_{fid}_{player}.jpg'.format(data_dir=args.data_dir, player=player, vid=vid, fid=fid)

            result_one_image = infer_one_frame(image, model, img_path, pose_path)
            result_one_image['foreground'] = player
            result_one_image['fid'] = fid
            result_list.append(result_one_image)
        fid += 1
        # if len(result_list) > 100:
        #     break

    pickle.dump(result_list, open('{}/pkl/result.pkl'.format(args.data_dir), 'wb'), protocol=2)
Beispiel #17
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(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):
        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):
        vid = im_name.split("/")[1]
        output_dir = args.output_dir + vid
        try:
            os.mkdir(output_dir)
        except OSError:
            pass
        out_name = os.path.join(
            output_dir, '{}'.format(os.path.basename(im_name) + '.pdf'))
        if os.path.exists(out_name):
            continue

        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, cls_bodys = 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)')

        vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            cls_bodys,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2)
Beispiel #18
0
def get_model():
    workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])
    merge_cfg_from_file(
        '/home/bryan/code/detectron/configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml'
    )
    cfg.NUM_GPUS = 1
    assert_and_infer_cfg(cache_urls=False)
    DOWNLOAD_CACHE = '/tmp/detectron-download-cache'
    weights_url = 'https://s3-us-west-2.amazonaws.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl'
    weights = cache_url(weights_url, DOWNLOAD_CACHE)
    return infer_engine.initialize_model_from_cfg(weights)
Beispiel #19
0
def main(args):
    #ros initialization
    rospy.init_node('get_image', anonymous=True)

    args.im_or_folder = get_image()

    while not rospy.is_shutdown():
        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)

        assert not cfg.MODEL.RPN_ONLY, \
            'RPN models are not supported'
        assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
            'Models that require precomputed proposals are not supported'

        model = infer_engine.initialize_model_from_cfg(args.weights)
        dummy_coco_dataset = dummy_datasets.get_coco_dataset()
        # set im list to be the ros image
        im = args.im_or_folder

        timers = defaultdict(Timer)
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        # calls the method that performs actual detection and inference
        fig = vis_utils.vis_one_image_opencv(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            show_class=True,
            thresh=args.thresh,
            kp_thresh=args.kp_thresh,
        )

        # img is rgb, convert to opencv's default bgr
        img = cv2.cvtColor(fig, cv2.COLOR_RGB2BGR)

        image_publisher = rospy.Publisher('detectron_output',
                                          Image,
                                          queue_size=10)
        brdg = CvBridge()
        image_publisher.publish(brdg.cv2_to_imgmsg(img, "bgr8"))
        try:
            start_time = time.time()
            print(time.time() - start_time)
            main(args)
        except KeyboardInterrupt:
            print("shutting down")
            cv2.destroyAllWindows()
Beispiel #20
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    print("capturing video " + args.input)
    cap = cv2.VideoCapture(args.input)
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    # pdb.set_trace()
    grab = 1
    if (cap.isOpened() == False):
        print("Error opening video stream or file")
        exit
    while (cap.isOpened() and grab <= total_frames):
        grab += 1
        ret_val, im = cap.read()
        #skips intermediate frames
        #if grab%2 !=0:
        #    continue
        #uncomment to resize image
        #im = cv2.resize(im, (int(1280/1),int(720/1)))
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                model, im, None, timers=timers)
        output_name = 'frame' + str(grab).zfill(4) + '.mp4'
        print("| Analysed frame {0} / {1}  in {2}ms".format(
            grab, total_frames, int(1000. * (time.time() - t))))
        #print('\t | Inference time: {:.3f}s'.format(time.time() - t))
        #for k, v in timers.items():
        #    print('\t | {}: {:.3f}s'.format(k, v.average_time))
        ret = vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            output_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            cls_bodys,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=False,
            thresh=0.7,
            kp_thresh=2)

    cap.release()
    cv2.destroyAllWindows()
Beispiel #21
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    h5py_file_path = ('/media/hdd1/tanya/open-pose/'
                      'paired_filenames512_image_keypoints512_main.h5')
    hf = h5py.File(h5py_file_path, 'r')

    IUV_image_list = []


    for i in range(len(hf['images'])):
        im = hf['images'][i]
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = 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)'
            )

        IUV_image = vis_utils.vis_one_image(
                im[:, :, ::-1],  # BGR -> RGB for visualization
                'dummy',
                args.output_dir,
                cls_boxes,
                cls_segms,
                cls_keyps,
                cls_bodys,
                dataset=dummy_coco_dataset,
                box_alpha=0.3,
                show_class=True,
                thresh=0.7,
                kp_thresh=2)
        IUV_image_list.append(IUV_image)
    IUV_images_final = np.stack(IUV_image_list, 0)
    with h5py.File('./paired_filenames512_image_keypoints_withIUV.h5', 'w') as f:
        f.create_dataset('images', data=np.array(hf['images']))
        f.create_dataset('keypoints', data=np.array(hf['keypoints']))
        f.create_dataset('IUV', data=IUV_images_final)
Beispiel #22
0
 def setup(self):
     c2_utils.import_detectron_ops()
     cv2.ocl.setUseOpenCL(False)
     merge_cfg_from_file(
         '/DensePose/configs/DensePose_ResNet101_FPN_s1x-e2e.yaml')
     cfg.NUM_GPUS = 1
     weights = cache_url(
         'https://s3.amazonaws.com/densepose/DensePose_ResNet101_FPN_s1x-e2e.pkl',
         cfg.DOWNLOAD_CACHE)
     assert_and_infer_cfg(cache_urls=False)
     model = infer_engine.initialize_model_from_cfg(weights, 1)
     dummy_coco_dataset = dummy_datasets.get_coco_dataset()
     return model
Beispiel #23
0
def main(args):
    logger = logging.getLogger(__name__)

    pdb.set_trace()
    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)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        im_list = glob.iglob(args.im_or_folder + '/*.' + args.image_ext)
    else:
        im_list = [args.im_or_folder]

    print(im_list)

    for i, im_name in enumerate(im_list):
        im_basename = os.path.basename(im_name)
        out_name = os.path.join(
            args.output_dir, '{}'.format(im_basename + '.' + args.output_ext))
        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)

            result_file = os.path.join(args.output_dir,
                                       im_basename + '_bbox.pkl')
            with open(result_file, 'wb') as f:
                boxes, _, _, _ = vis_utils.convert_from_cls_format(
                    cls_boxes, cls_segms, cls_keyps)
                print(boxes.shape)
                pickle.dump(cls_boxes, f)

        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)')
Beispiel #24
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    zmq_video = args.source == "zmq"
    frameId = 0

    if zmq_video:
        context = zmq.Context()
        socket = context.socket(zmq.REP)
        socket.bind("tcp://*:7001")
    else:
        # From virtual camera video and its associated timestamp file on Drive PX2,e.g."./lane/videofilepath.h264"
        cap = cv2.VideoCapture(args.source)

    while True:
        if zmq_video:
            try:
                message = socket.recv()
                print("Received message length:" + str(len(message)) +
                      " type:" + str(type(message)))
                socket.send("ok")
                position = message.find(ZMQ_SEPER, 0, 100)
                frameId = message[:position]
                message = message[position + len(ZMQ_SEPER):]
                img_np = np.fromstring(message, np.uint8)
                img_np = img_np.reshape((400, 1400, 3))
                print("nparr type:" + str(type(img_np)) + " shape:" +
                      str(img_np.shape))
                ret = True
            except KeyboardInterrupt:
                print("interrupt received, stopping...")
                socket.close()
                context.term()
                ret = False
                cap.release()
        else:
            ret, img_np = cap.read()
            frameId += 1

        # read completely or raise exception
        if not ret:
            print("cannot get frame")
            break

        hanle_frame(args, frameId, img_np, logger, model, dummy_coco_dataset)
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    dataset_name = cfg.TEST.DATASETS[0]
    dummy_coco_dataset = JsonDataset(dataset_name)
    # dummy_coco_dataset = dummy_datasets.get_paris_dataset()

    vid_dir = '/coco/paris_dataset/PARIS_demo.mp4'
    cap = cv2.VideoCapture(vid_dir)
    ret, im = cap.read()
    count = 0
    while ret:
        im_name = str(count)
        out_name = os.path.join(args.output_dir,
                                '{}'.format(str(count) + '.jpg'))
        logger.info('Processing frame -> {}'.format(count))
        timers = defaultdict(Timer)
        t = time.time()
        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, cls_bodys = 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))

        vis_im = vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            im_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            cls_bodys,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=0.7,
            kp_thresh=2)
        # cv2.imshow('frame', vis_im)
        # cv2.waitKey(10)

        ret, im = cap.read()
        count += 1
    cap.release()
    cv2.destroyAllWindows()
Beispiel #26
0
def setup():
    global dummy_coco_dataset
    cfg_file = 'configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml'
    weights = 'https://dl.fbaipublicfiles.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl'
    merge_cfg_from_file(cfg_file)
    cfg.NUM_GPUS = 1
    weights = cache_url(weights, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'
    model = infer_engine.initialize_model_from_cfg(weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()
    return model
Beispiel #27
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(cache_urls=False)
    model = infer_engine.initialize_model_from_cfg(args.weights)
    output = open(args.output_file, "w+")
    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):
        logger.info('Processing {}'.format(i))
        output.write(im_name[-40:] + "\n")
        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)')
        thresh = 0.2
        if isinstance(cls_boxes, list):
            boxes, classes = convert_from_cls_format(cls_boxes)

        if type(boxes) == np.ndarray:
            areas = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
            sorted_inds = np.argsort(-areas)

            for i in sorted_inds:
                bbox = boxes[i, :4]
                score = boxes[i, -1]
                if score < thresh:
                    continue
                else:
                    output.write(
                        str(bbox[0]) + " " + str(bbox[1]) + " " +
                        str(bbox[2]) + " " + str(bbox[3]) + " " + str(score) +
                        "\n")
    output.close()
Beispiel #28
0
def inference(cfg_path, weights, img_pillow, output_dir):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(cfg_path)
    #print( "cfg : ", cfg )
    assert_and_infer_cfg(cache_urls=False, make_immutable=False)

    cfg.NUM_GPUS = 1
    weights = cache_url(weights, cfg.DOWNLOAD_CACHE)
    model = infer_engine.initialize_model_from_cfg(weights)
    dummy_coco_dataset = dummy_datasets.get_coco_dataset()

    im_name = "test"
    #img_cv = cv2.imread(im_name)
    img_np = np.asarray(img_pillow)
    img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)

    timers = defaultdict(Timer)
    t = time.time()
    with c2_utils.NamedCudaScope(0):
        cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
            model, img_cv, 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))

    vis_utils.vis_one_image(
        img_cv[:, :, ::-1],  # BGR -> RGB for visualization
        im_name,
        output_dir,
        cls_boxes,
        cls_segms,
        cls_keyps,
        cls_bodys,
        dataset=dummy_coco_dataset,
        box_alpha=0.3,
        show_class=True,
        thresh=0.7,
        kp_thresh=2)

    IUV_SaveName = os.path.basename(im_name).split('.')[0] + '_IUV.png'
    INDS_SaveName = os.path.basename(im_name).split('.')[0] + '_INDS.png'
    iuv_pillow = Image.open(os.path.join(output_dir,
                                         '{}'.format(IUV_SaveName)))
    inds_pillow = Image.open(
        os.path.join(output_dir, '{}'.format(INDS_SaveName)))
    return iuv_pillow, inds_pillow
Beispiel #29
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)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        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, 'detection_' + '{}'.format(os.path.basename(im_name))
        )
        print('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
            )
        #print('Inference time: {:.3f}s'.format(time.time() - t))
        #for k, v in timers.items():
        #    print(' | {}: {:.3f}s'.format(k, v.average_time))
        vis_utils.vis_one_image(
            im[:, :, ::-1],  # BGR -> RGB for visualization
            out_name,
            args.output_dir,
            cls_boxes,
            cls_segms,
            cls_keyps,
            dataset=dummy_coco_dataset,
            box_alpha=0.3,
            show_class=True,
            thresh=args.thresh,
            kp_thresh=args.kp_thresh,
            out_when_no_box=args.out_when_no_box
        )
Beispiel #30
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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        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):
        #if i > 10:
        #    continue
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name)[:-len(args.image_ext)] + 'mat')
        )
        
        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
            )
        boxes, segms, keyps, classes = vis_utils.convert_from_cls_format(cls_boxes, cls_segms, cls_keyps)
        if boxes is None:
            continue

        segms = vis_utils.mask_util.decode(segms)

        valid_inds = np.greater(boxes[:, 4], 0.5)
        boxes = boxes[valid_inds, :]
        segms = segms[:, :, valid_inds]
        classes = np.array(classes)[valid_inds]
        class_names = np.asarray(([coco_to_pascal_name(coco_classes[c-1]) for c in classes]), dtype='object')

        sio.savemat(out_name, {'masks': segms, 'boxes': boxes, 'classes': class_names});
    def __init__(self, score_threshold):

        self.score_threshold = score_threshold

        workspace.GlobalInit(['caffe2', '--caffe2_log_level=0'])

        merge_cfg_from_file(config_path)
        cfg.NUM_GPUS = 1
        weights_path = cache_url(weights_url, cfg.DOWNLOAD_CACHE)
        assert_and_infer_cfg(cache_urls=False)

        assert not cfg.MODEL.RPN_ONLY, 'RPN models are not supported'
        assert not cfg.TEST.PRECOMPUTED_PROPOSALS, 'Models that require precomputed proposals are not supported'

        self.model = infer_engine.initialize_model_from_cfg(weights_path)
        self.dummy_coco_dataset = dummy_datasets.get_coco_dataset()
Beispiel #32
0
 def __init__(self, args):
     self.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(cache_urls=False)
     assert not cfg.MODEL.RPN_ONLY, \
         'RPN models are not supported'
     assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
         'Models that require precomputed proposals are not supported'
     self.model = infer_engine.initialize_model_from_cfg(args.weights)
     self.coco_dataset = get_coco_dataset()
     self.image_srv = rospy.Service('infer_image', MaskInfer,
                                    self.infer_image)
     self.thresh = args.thresh
     self.dir = args.dir
def main(args):
    logger = logging.getLogger(__name__)

    merge_cfg_from_file(args.cfg)
    cfg.NUM_GPUS = 1
    if "mot-classes" in args.opts:
        dummy_dataset = dummy_datasets.get_mot_dataset()
        cfg.NUM_CLASSES = 14
    else:
        dummy_dataset = dummy_datasets.get_coco_dataset()
        cfg.NUM_CLASSES = 81
    for i, weights_file in enumerate(args.weights_list):
        args.weights_list[i] = cache_url(weights_file, cfg.DOWNLOAD_CACHE)
    assert_and_infer_cfg(cache_urls=False)
    logger.info('Testing with config:')
    logger.info(pprint.pformat(cfg))

    preffix_list = args.preffix_list if len(args.preffix_list) \
        else [""] * len(args.weights_list)
    model = infer_engine.initialize_mixed_model_from_cfg(
        args.weights_list, preffix_list=preffix_list)
    # Initialize tracking accumulator
    tracking = Tracking(args.thresh, cfg.TRCNN.MAX_BACK_TRACK)
    vis = {
        "output-dir": args.output_dir,
        "dummy-dataset": dummy_dataset,
        "show-class": "show-class" in args.opts,
        "show-track": "show-track" in args.opts,
        "thresh": args.thresh,
        "kp-thresh": args.kp_thresh,
        "track-thresh": args.track_thresh,
        "n-colors": args.n_colors,
    }
    # Load proposals if specified
    if args.proposals is not None:
        proposals = pickle.load(open(args.proposals, 'r'))
    else:
        proposals = None
    # Run inference
    infer_track_sequence(model,
                         args.im_dir,
                         tracking,
                         vis=vis,
                         det_file=args.output_file,
                         proposals=proposals,
                         mot=("all-dets" not in args.opts))
def main(args):
    glob_keypoints = []

    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(cache_urls=False)

    assert not cfg.MODEL.RPN_ONLY, \
        'RPN models are not supported'
    assert not cfg.TEST.PRECOMPUTED_PROPOSALS, \
        'Models that require precomputed proposals are not supported'

    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):
        im_list = glob.iglob(args.im_or_folder + '/*' + '.png')
    else:
        im_list = [args.im_or_folder]
    im_list = sorted(im_list)
    for i, im_name in enumerate(im_list):
        out_name = os.path.join(
            args.output_dir, '{}'.format(os.path.basename(im_name) + '.' + args.output_ext)
        )
        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)'
            )

        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=args.thresh,
            kp_thresh=args.kp_thresh,
            ext=args.output_ext,
            out_when_no_box=args.out_when_no_box
        )

        cls_boxes_np = np.asarray(cls_boxes)
        cls_boxes_prob = cls_boxes_np[1][:,4]
        idx_max_prob = np.argmax(cls_boxes_prob)

        cls_keyps_max_prob = cls_keyps[1][idx_max_prob]
        pose_x_y_prob_after_softmax = cls_keyps_max_prob[[0,1,3]]
        glob_keypoints.append(np.transpose(pose_x_y_prob_after_softmax))

    dictionarry_keypoints={'S1': {'Directions 1' : np.asarray([glob_keypoints])}}
    metadata = {'layout_name': 'h36m', 'num_joints': 17, 'keypoints_symmetry': [[4, 5, 6, 11, 12, 13], [1, 2, 3, 14, 15, 16]]}
    #np.savez(os.path.join('/home/narvis/Dev/VideoPose3D/data', "data_2d_detections.npz"), metadata=metadata, positions_2d=dictionarry_keypoints)
    np.savez(os.path.join(args.output_dir, "data_2d_detections.npz"), metadata=metadata, positions_2d=dictionarry_keypoints)