def main(checkpoint, input_files):
    os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu()

    model_config, _, track_config = load_cfgs(checkpoint)
    track_config['log_level'] = 1
    track_config["is_video"] = False

    g = tf.Graph()
    with g.as_default():
        model = inference_wrapper.InferenceWrapper()
        restore_fn = model.build_graph_from_config(model_config, track_config,
                                                   checkpoint)
    g.finalize()

    if not osp.isdir(track_config['log_dir']):
        logging.info('Creating inference directory: %s',
                     track_config['log_dir'])
        mkdir_p(track_config['log_dir'])

    video_dirs = []
    for file_pattern in input_files.split(","):
        video_dirs.extend(glob(file_pattern))
    logging.info("Running tracking on %d videos matching %s", len(video_dirs),
                 input_files)

    gpu_options = tf.GPUOptions(allow_growth=True)
    sess_config = tf.ConfigProto(gpu_options=gpu_options)

    with tf.Session(graph=g, config=sess_config) as sess:
        restore_fn(sess)

        tracker = Tracker(model,
                          model_config=model_config,
                          track_config=track_config)

        for video_dir in video_dirs:
            if not osp.isdir(video_dir):
                logging.warning(
                    '{} is not a directory, skipping...'.format(video_dir))
                continue

            video_name = osp.basename(video_dir)
            video_log_dir = osp.join(track_config['log_dir'], video_name)
            mkdir_p(video_log_dir)

            filenames = sort_nicely(glob(video_dir + '/img/*.jpg'))
            first_line = open(video_dir + '/groundtruth_rect.txt').readline()
            bb = [int(v) for v in first_line.strip().split(',')]
            # Rectangle: [x,y,width,height]
            init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2],
                                bb[3])  # 0-index in python

            trajectory = tracker.track(sess, init_bb, filenames, video_log_dir)
            with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f:
                for region in trajectory:
                    rect_str = '{},{},{},{}\n'.format(region.x + 1,
                                                      region.y + 1,
                                                      region.width,
                                                      region.height)
                    f.write(rect_str)
示例#2
0
def main(config):
    tf.reset_default_graph()
    log_dir = config.log_dir
    if config.net_type == 'siamese':
        model = inference_wrapper.InferenceWrapper(config)
    elif config.net_type == 'cfcf':
        model = inference_cfcf.InferenceCFCF(config)
    else:
        raise ValueError('Unknown net_type: ', config.net_type)

    var_list = model.build_graph_from_config()

    if config.clear_logs and tf.gfile.Exists(log_dir):
        print('Clear all files in {}'.format(log_dir))
        try:
            tf.gfile.DeleteRecursively(log_dir) 
        except:
            print('Fail to delete {}. You probably have to kill tensorboard process.'.format(log_dir))

    seq_names = read_text(config.seq_text, dtype=np.str)
    video_dirs = [os.path.join(config.root_dir, x) for x in seq_names]

    tfconfig = tf.ConfigProto()
    tfconfig.gpu_options.allow_growth = True # almost the same as tf.InteractiveSession
    sess = tf.Session(config=tfconfig)
    sess.run(tf.global_variables_initializer())
    # restore_fn(sess)

    if osp.isdir(config.model):
        checkpoint = tf.train.latest_checkpoint(config.model)
    else:
        checkpoint = config.model

    saver = tf.train.Saver(var_list)
    saver.restore(sess, checkpoint)

    tracker = Tracker(model, config=config)

    for video_dir in video_dirs:
        if not os.path.isdir(video_dir):
            continue

        video_name = os.path.basename(video_dir)
        video_log_dir = os.path.join(log_dir, video_name)
        mkdir_p(video_log_dir)

        filenames = sort_nicely(glob.glob(video_dir + '/img/*.jpg'))
        first_line = open(video_dir + '/groundtruth_rect.txt').readline()
        bb = [int(v) for v in first_line.strip().split(',')]
        init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2], bb[3])  # 0-index in python

        trajectory = tracker.track(sess, init_bb, filenames, video_log_dir)
        with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f:
            for region in trajectory:
                rect_str = '{},{},{},{}\n'.format(region.x + 1, region.y + 1,
                                                region.width, region.height)
                f.write(rect_str)
示例#3
0
def run_SA_Siam(seq, rp, bSaveImage, epoch=30):
    iter_ckpt = epoch * 6650 - 1
    checkpoint_appearance_path = CHECKPOINT_APPEARANCE.format(
        iter_ckpt=iter_ckpt)
    logging.info('Evaluating {}...'.format(checkpoint_appearance_path))
    checkpoint_semantic_path = CHECKPOINT_SEMANTIC.format(iter_ckpt=iter_ckpt)
    logging.info('Evaluating {}...'.format(checkpoint_semantic_path))

    # Read configurations from json
    model_config, _, track_config = load_cfgs(CHECKPOINT_SA_SIAM)

    track_config['log_level'] = 0  # Skip verbose logging for speed

    # Build the inference graph.
    g = tf.Graph()
    with g.as_default():
        model = inference_wrapper.InferenceWrapper()
        model.build_model(model_config, track_config)
        saver_loader_semantic = get_saver('',
                                          removes=[':0', '_semantic'],
                                          excepts=['appearance', 'State'])
        saver_loader_appearance = get_saver('',
                                            removes=[':0', '_appearance'],
                                            excepts=['semantic', 'State'])
    g.finalize()

    gpu_options = tf.GPUOptions(allow_growth=True)
    sess_config = tf.ConfigProto(gpu_options=gpu_options)

    with tf.Session(graph=g, config=sess_config) as sess:
        # Load the model from checkpoint.
        # restore_fn(sess)
        saver_loader_semantic.restore(sess, checkpoint_semantic_path)
        saver_loader_appearance.restore(sess, checkpoint_appearance_path)

        tracker = Tracker(model, model_config, track_config)

        tic = time.clock()
        frames = seq.s_frames
        init_rect = seq.init_rect
        x, y, width, height = init_rect  # OTB format
        init_bb = Rectangle(x - 1, y - 1, width, height)

        trajectory_py = tracker.track(sess, init_bb, frames)
        trajectory = [
            Rectangle(val.x + 1, val.y + 1, val.width, val.height)
            for val in trajectory_py
        ]  # x, y add one to match OTB format
        duration = time.clock() - tic

        result = dict()
        result['res'] = trajectory
        result['type'] = 'rect'
        result['fps'] = round(seq.len / duration, 3)
        return result
def main(checkpoint, input_files):
  os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu()

  model_config, _, track_config = load_cfgs(checkpoint)
  track_config['log_level'] = 1

  g = tf.Graph()
  with g.as_default():
    model = inference_wrapper.InferenceWrapper()
    restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint)
  g.finalize()

  if not osp.isdir(track_config['log_dir']):
    logging.info('Creating inference directory: %s', track_config['log_dir'])
    mkdir_p(track_config['log_dir'])

  video_dirs = []
  for file_pattern in input_files.split(","):
    video_dirs.extend(glob(file_pattern))
  logging.info("Running tracking on %d videos matching %s", len(video_dirs), input_files)

  gpu_options = tf.GPUOptions(allow_growth=True)
  sess_config = tf.ConfigProto(gpu_options=gpu_options)

  with tf.Session(graph=g, config=sess_config) as sess:
    restore_fn(sess)

    tracker = Tracker(model, model_config=model_config, track_config=track_config)

    for video_dir in video_dirs:
      if not osp.isdir(video_dir):
        logging.warning('{} is not a directory, skipping...'.format(video_dir))
        continue

      video_name = osp.basename(video_dir)
      video_log_dir = osp.join(track_config['log_dir'], video_name)
      mkdir_p(video_log_dir)

      filenames = sort_nicely(glob(video_dir + '/img/*.jpg'))
      first_line = open(video_dir + '/groundtruth_rect.txt').readline()
      bb = [int(v) for v in first_line.strip().split(',')]
      init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2], bb[3])  # 0-index in python

      trajectory = tracker.track(sess, init_bb, filenames, video_log_dir)
      with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f:
        for region in trajectory:
          rect_str = '{},{},{},{}\n'.format(region.x + 1, region.y + 1,
                                            region.width, region.height)
          f.write(rect_str)
示例#5
0
def run_MFST(seq, rp, bSaveImage):
  checkpoint_path = CHECKPOINT
  logging.info('Evaluating {}...'.format(checkpoint_path))

  # Read configurations from json
  model_config, _, track_config = load_cfgs(checkpoint_path)

  track_config['log_level'] = 0  # Skip verbose logging for speed

  # Build the inference graph.
  g = tf.Graph()
  with g.as_default():
    model = inference_wrapper.InferenceWrapper()
    restore_fn = model.build_graph_from_config(model_config, track_config, checkpoint_path)
  #g.finalize()

  gpu_options = tf.GPUOptions(allow_growth=True)
  sess_config = tf.ConfigProto(gpu_options=gpu_options)

  with tf.Session(graph=g, config=sess_config) as sess:
    ## used for initializing alexnet parameters
    init_global = tf.global_variables_initializer()
    sess.run(init_global)
    
    ## global initalizer must be run before restore
    # Load the model from checkpoint.
    restore_fn(sess)

    tracker = Tracker(model, model_config, track_config)

    tic = time.clock()
    frames = seq.s_frames
    init_rect = seq.init_rect
    x, y, width, height = init_rect  # OTB format
    init_bb = Rectangle(x - 1, y - 1, width, height)

    trajectory_py = tracker.track(sess, init_bb, frames)
    trajectory = [Rectangle(val.x + 1, val.y + 1, val.width, val.height) for val in
                  trajectory_py]  # x, y add one to match OTB format
    duration = time.clock() - tic

    result = dict()
    result['res'] = trajectory
    result['type'] = 'rect'
    result['fps'] = round(seq.len / duration, 3)
    return result
示例#6
0
# PROCESS INIT FRAME #
imgfile = handle.frame()
if not imgfile:
    sys.exit(0)

logger.info(imgfile)

init_left, init_top, init_w, init_h = selection[:]

# Feed minx, miny, maxx, maxy #
init_bbox = [init_left, init_top, init_left + init_w, init_top + init_h]

init_im = io.imread(imgfile)
tracker = Tracker(model, init_im, init_bbox, TRACK_CONFIG['use_gpu'])

while True:
    imgfile = handle.frame()
    logger.info(imgfile)
    if not imgfile:
        handle.quit()
        break
    im = io.imread(imgfile)
    est_bbox = tracker.track(im)
    #logger.info("est_bbox: ", est_bbox)
    w, h = est_bbox[2] - est_bbox[0], est_bbox[3] - est_bbox[1]
    minx, miny = est_bbox[0], est_bbox[1]
    selection = vot.Rectangle(minx, miny, w, h)
    #logger.info(selection)
    handle.report(selection)
示例#7
0
def main(checkpoint, input_files):
    os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu()

    model_config, _, track_config = load_cfgs(checkpoint)
    track_config['log_level'] = 1

    g = tf.Graph()
    with g.as_default():
        model = inference_wrapper.InferenceWrapper()
        restore_fn = model.build_graph_from_config(model_config, track_config,
                                                   checkpoint)
    g.finalize()

    if not osp.isdir(track_config['log_dir']):
        logging.info('Creating inference directory: %s',
                     track_config['log_dir'])
        mkdir_p(track_config['log_dir'])

    video_dirs = []
    for file_pattern in input_files.split(","):
        video_dirs.extend(glob(file_pattern))
    logging.info("Running tracking on %d videos matching %s", len(video_dirs),
                 input_files)

    gpu_options = tf.GPUOptions(allow_growth=True)
    sess_config = tf.ConfigProto(gpu_options=gpu_options)

    with tf.Session(graph=g, config=sess_config) as sess:
        restore_fn(sess)

        tracker = Tracker(model,
                          model_config=model_config,
                          track_config=track_config)

        for video_dir in video_dirs:
            if not osp.isdir(video_dir):
                logging.warning(
                    '{} is not a directory, skipping...'.format(video_dir))
                continue

            video_name = osp.basename(video_dir)
            video_log_dir = osp.join(track_config['log_dir'], video_name)
            mkdir_p(video_log_dir)

            filenames = sort_nicely(
                glob(video_dir + '/img/*.jpg') +
                glob(video_dir + '/img/*.png'))
            first_line = open(video_dir + '/groundtruth_rect.txt').readline()
            bb = [int(v) for v in first_line.strip().split(',')]
            init_bb = Rectangle(bb[0] - 1, bb[1] - 1, bb[2],
                                bb[3])  # 0-index in python

            trajectory = tracker.track(sess, init_bb, filenames, video_log_dir)
            with open(osp.join(video_log_dir, 'track_rect.txt'), 'w') as f:
                for region in trajectory:
                    rect_str = '{},{},{},{}\n'.format(region.x + 1,
                                                      region.y + 1,
                                                      region.width,
                                                      region.height)
                    f.write(rect_str)

            with open(osp.join(video_log_dir, 'bboxes.json'), 'r') as f:
                data = json.load(f)

            final_output = {}
            for i, fname in enumerate(data.keys()):
                img = np.array(Image.open(fname).convert('RGB'))
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                #print(img,img.shape)
                bboxes = data[fname]
                bboxes = list(
                    map(
                        lambda x: list(
                            map(lambda y: float(y),
                                x.strip().split(','))), bboxes))
                arr = []
                for x, y, w, h in bboxes:
                    ymin, xmin, ymax, xmax = int(y), int(x), int(y +
                                                                 h), int(x + w)
                    img = cv2.rectangle(img, (xmin, ymin), (xmax, ymax),
                                        (0, 255, 0, 255), 2)
                    arr.append([ymin, xmin, ymax, xmax])
                final_output[fname] = arr
                name = osp.basename(fname)
                name = osp.splitext(name)[0]

                W, H, _ = img.shape
                cv2.imshow("Pic", cv2.resize(img, (W // 2, H // 2)))
                cv2.waitKey(0)

                out_folder = osp.join(video_log_dir, "Outputs")
                mkdir_p(out_folder)
                cv2.imwrite(osp.join(out_folder, f"{name}_bbox.png"), img)

            with open(osp.join(out_folder, "output.json"), "w") as f:
                json.dump(final_output, f, indent=4)

            cv2.destroyAllWindows()