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)
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)
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)
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
def __init__( self, debug=0, checkpoint='Logs/SiamFC/track_model_checkpoints/SiamFC-3s-color-pretrained' ): os.environ['CUDA_VISIBLE_DEVICES'] = auto_select_gpu() # run only on cpu # os.environ['CUDA_VISIBLE_DEVICES'] = '-1' model_config, _, track_config = load_cfgs(checkpoint) track_config['log_level'] = debug 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']) gpu_options = tf.GPUOptions(allow_growth=True) sess_config = tf.ConfigProto(gpu_options=gpu_options) sess = tf.Session(graph=g, config=sess_config) # sess.run(tf.global_variables_initializer()) restore_fn(sess) tracker = Tracker(model, model_config=model_config, track_config=track_config) video_name = "demo" video_log_dir = osp.join(track_config['log_dir'], video_name) rmdir(video_log_dir) mkdir_p(video_log_dir) self.tracker = tracker self.sess = sess self.video_log_dir = video_log_dir self.graph = g
def build_tracker(tracker_config): # Build the inference graph. g = tf.Graph() with g.as_default(): model = inference_wrapper.InferenceWrapper() restore_fn = model.build_graph_from_config( tracker_config['model_config'], tracker_config['track_config'], tracker_config['checkpoint_path']) g.finalize() gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=tracker_config['gpu_fraction']) sess_config = tf.ConfigProto(gpu_options=gpu_options) sess = tf.Session(graph=g, config=sess_config) # Load the model from checkpoint. restore_fn(sess) tracker = Tracker(model, model_config=tracker_config['model_config'], track_config=tracker_config['track_config']) return sess, tracker
selection = handle.region() # 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)
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) handle = vot.VOT("rectangle") selection = handle.region() imagefile = handle.frame() tracker.track_vot_init(sess, selection, imagefile) while True: imagefile = handle.frame() if not imagefile: break region = tracker.track_vot(sess, imagefile) handle.report(region)
def main(_): # load model model_config, _, track_config = load_cfgs(CHECKPOINT) track_config["log_level"] = 0 track_config["is_video"] = True 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 os.path.isdir(track_config['log_dir']): tf.logging.info('Creating inference directory: %s', track_config['log_dir']) mkdir_p(track_config['log_dir']) 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) video_name = os.path.basename(FLAGS.video_path) video_log_dir = os.path.join(track_config["log_dir"], video_name) mkdir_p(video_log_dir) if str(FLAGS.video_path) in ["0", "1"]: # read from camera video_path = int(FLAGS.video_path) with_camera = True else: # read from video video_path = glob(os.path.join(FLAGS.video_path, "*.mp4"))[0] with_camera = False video_capture = cv2.VideoCapture(video_path) bb = [-1, -1, -1, -1] cv2.namedWindow("template") cv2.setMouseCallback("template", draw_init_box, bb) trajectory = [] f_count = 0 f_rate = 0 start_time = time.time() while True: # capture frame by frame ret_, frame = video_capture.read() if ret_ == False: continue f_width, f_height = [ int(a) for a in FLAGS.video_resolution.split("*") ] try: o_frame = cv2.resize(frame, (f_width, f_height), interpolation=cv2.INTER_CUBIC) except: break i_frame = cv2.cvtColor(o_frame, cv2.COLOR_BGR2RGB) # cv2.imwrite("test.jpg",o_frame) # pdb.set_trace() if f_count == 0: # initialize the tracker # wait for drawing init box while True: init_frame = o_frame.copy() cv2.imshow("template", init_frame) k = cv2.waitKey(0) if k == 32: # space cx = int((bb[0] + bb[2]) / 2) cy = int((bb[1] + bb[3]) / 2) w = int(bb[2] - bb[0]) h = int(bb[3] - bb[1]) # Rectangle: [x,y,width,height] init_bb = Rectangle(cx - 1, cy - 1, w, h) # 0-index in python draw_box(init_frame, init_bb, "exemplar") break first_box = convert_bbox_format(init_bb, "center-based") bbox_feed = [ first_box.y, first_box.x, first_box.height, first_box.width ] input_feed = [i_frame, bbox_feed] frame2crop_scale = tracker.siamese_model.initialize( sess, input_feed) # Storing target state original_target_height = first_box.height original_target_width = first_box.width search_center = np.array([ get_center(tracker.x_image_size), get_center(tracker.x_image_size) ]) current_target_state = TargetState( bbox=first_box, search_pos=search_center, scale_idx=int(get_center(tracker.num_scales))) # setup initialized params current_param = { "original_target_width": original_target_width, "original_target_height": original_target_height, "search_center": search_center, "current_target_state": current_target_state } bbox, current_param = tracker.track_frame(sess, i_frame, current_param, video_log_dir) # add overlays end_time = time.time() f_rate = int(1 / (end_time - start_time)) start_time = time.time() draw_box(o_frame, bbox) cv2.putText(o_frame, str(f_rate) + "fps", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), thickness=2, lineType=2) trajectory.append(bbox) f_count += 1 cv2.imshow("Real-time Ouput", o_frame) cv2.imshow("template", init_frame) # if f_count > 30: # cv2.imwrite("test.jpg",o_frame) # pdb.set_trace() if cv2.waitKey(1) & 0xFF == ord("q"): cv2.imwrite("./assets/instance.jpg", o_frame) cv2.imwrite("./assets/exemplar.jpg", init_frame) break video_capture.release() cv2.destroyAllWindows() # save track results # pdb.set_trace() with open(os.path.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)
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()