def show_flow(image, succ_frames, flows): global frames frames = [image] + succ_frames for ind in range(len(succ_frames)): im = succ_frames[ind] flow = flows[ind] for i in range(20): for j in range(20): x = i * 10 + 5 y = j * 10 + 5 fx = flow[i, j, 0] fy = flow[i, j, 1] cv2.circle(im, (x, y), 1, (0, 255, 0), -1) cv2.line(im, (x, y), (int(x - fx), int(y - fy)), (255, 0, 0)) videofig(100, redraw_fn, play_fps=30)
def show_track(self, idx): mask_track = self.label == idx img_files = self.fn[mask_track] bbox = self.data[mask_track, 2:6] frames = self.data[mask_track, 0] viss = self.data[mask_track, 8] def redraw_fn(f, axes): img = imread(img_files[f]) x, y, w, h = tuple(bbox[f, :]) w = w - x h = h - y frame = frames[f] vis = viss[f] if not redraw_fn.initialized: im = axes.imshow(img, animated=True) bb = Rectangle( (x, y), w, h, fill=False, # remove background edgecolor="red") t1 = axes.text(0, 0, '[f:{} vis:{:1.2f}]'.format(frame, vis), bbox=dict(facecolor='red', alpha=0.5)) axes.add_patch(bb) redraw_fn.im = im redraw_fn.bb1 = bb redraw_fn.t1 = t1 redraw_fn.initialized = True else: redraw_fn.im.set_array(img) redraw_fn.bb1.set_xy((x, y)) redraw_fn.bb1.set_width(w) redraw_fn.bb1.set_height(h) redraw_fn.t1.set_text('[f:{} vis:{:1.2f}]'.format(frame, vis)) redraw_fn.initialized = False videofig(len(img_files), redraw_fn, play_fps=30)
def display_via_videofig(images, title): """Display an animated sequence of images in a new window.""" from videofig import videofig # redraw_fn draw frame f in a image sequence def redraw_fn(i, axes): if not redraw_fn.initialized: redraw_fn.im = axes.imshow(images[i], animated=True) redraw_fn.initialized = True else: redraw_fn.im.set_array(images[i]) redraw_fn.initialized = False return videofig(len(images), redraw_fn, play_fps=30)
def show_track(idx, data_set): """ visualize a track for sanity check Args: idx(int): track index 434 total tracks currently data_set(MOT16_train_dataset): Returns: you will see """ root = data_set.root gt = data_set.gt sample = data_set.train_samples img_id = data_set.img_id mot_train_seq = data_set.mot_train_seq if idx + 1 > len(img_id): idx = len(img_id) elif idx < 0: idx = len(img_id) + idx # working with samples img_id = img_id[idx] vid_id = img_id[0][0] - 1 track_id = img_id[0][1] frame_num = img_id[:, 2] sample = sample[idx] # working with gt gt = gt[vid_id] mask_track = gt['track_id'] == track_id track_gt = gt[mask_track][['frame_num', 'x', 'y', 'w', 'h']] # find intersection. it is a pain in the ass working with structured array! frame_num_gt = track_gt['frame_num'] mask_intersect = np.isin(frame_num_gt, frame_num, assume_unique=True) track_gt = track_gt[mask_intersect][['x', 'y', 'w', 'h']] # align initial center sample[0][0] = track_gt[0][0] sample[0][1] = track_gt[0][1] # cumsum sample[:, 0:2] = np.cumsum(sample[:, 0:2], axis=0) print("playing vid {}".format(mot_train_seq[vid_id])) img_files = generate_img_fn(root, mot_train_seq, img_id) # track_gt[0] # tuple(sample[0].tolist()) def redraw_fn(f, axes): img = imread(img_files[f]) x1, y1, w1, h1 = track_gt[f] x2, y2, w2, h2 = tuple(sample[f].tolist()) x1 = int(x1 - w1 / 2) x2 = int(x2 - w2 / 2) y1 = int(y1 - h1 / 2) y2 = int(y2 - h2 / 2) if not redraw_fn.initialized: im = axes.imshow(img, animated=True) bb1 = Rectangle( (x1, y1), w1, h1, fill=False, # remove background edgecolor="red", gid='gt') bb2 = Rectangle( (x2, y2), w2, h2, fill=False, # remove background edgecolor="blue", gid='sample') t1 = axes.text(0, 0, '[{} {}]'.format(x1, y1), bbox=dict(facecolor='red', alpha=0.5)) t2 = axes.text(300, 0, '[{} {}]'.format(x2, y2), bbox=dict(facecolor='blue', alpha=0.5)) axes.add_patch(bb1) axes.add_patch(bb2) redraw_fn.im = im redraw_fn.bb1 = bb1 redraw_fn.bb2 = bb2 redraw_fn.t1 = t1 redraw_fn.t2 = t2 redraw_fn.initialized = True else: redraw_fn.im.set_array(img) redraw_fn.bb1.set_xy((x1, y1)) redraw_fn.bb1.set_width(w1) redraw_fn.bb1.set_height(h1) redraw_fn.t1.set_text('[{} {}]'.format(x1, y1)) redraw_fn.bb2.set_xy((x2, y2)) redraw_fn.bb2.set_width(w2) redraw_fn.bb2.set_height(h2) redraw_fn.t2.set_text('[{} {}]'.format(x2, y2)) # redraw_fn.t2.set_y(y2) redraw_fn.initialized = False videofig(len(img_files), redraw_fn, play_fps=30)
redraw_fn.img_handle = ax.imshow(img) box_artist = Rectangle((x, y), w, h, fill=False, # remove background lw=2, edgecolor="red") ax.add_patch(box_artist) redraw_fn.box_handle = box_artist redraw_fn.last_time = time.time() redraw_fn.text_handle = ax.text(0., 1 - 0.05, 'Resolution {}x{}, FPS: {:.2f}'.format(img.shape[1], img.shape[0], 0), transform=ax.transAxes, color='yellow', size=12) redraw_fn.initialized = True else: redraw_fn.img_handle.set_array(img) redraw_fn.box_handle.set_xy((x, y)) redraw_fn.box_handle.set_width(w) redraw_fn.box_handle.set_height(h) current_time = time.time() redraw_fn.text_handle.set_text('Resolution {}x{}, FPS: {:.2f}'.format(img.shape[1], img.shape[0], 1 / (current_time - redraw_fn.last_time))) redraw_fn.last_time = current_time redraw_fn.initialized = False if not SAVE_PLOTS: videofig(NUM_IMAGES, redraw_fn, play_fps=PLAY_FPS) else: videofig(NUM_IMAGES, redraw_fn, play_fps=PLAY_FPS, save_dir='example2_save')
import numpy as np from videofig import videofig SAVE_PLOTS = False class Redraw(object): def __init__(self, amp=3000, f0=3): self.initialized = False self.amp = amp self.f0 = f0 def draw(self, f, ax): amp = float(f) / self.amp f0 = self.f0 t = np.arange(0.0, 1.0, 0.001) s = amp * np.sin(2 * np.pi * f0 * t) if not self.initialized: self.l, = ax.plot(t, s, lw=2, color='red') self.initialized = True else: self.l.set_ydata(s) def redraw_fn(f, axes): redraw_fn.sub.draw(f, axes) redraw_fn.sub = Redraw(3000, 1) if not SAVE_PLOTS: videofig(100, redraw_fn) else: videofig(100, redraw_fn, save_dir='example1_save')
for axis in ['top','bottom','left','right']: ax.spines[axis].set_linewidth(2.5) ax.spines[axis].set_color(self.color) ax.set_yticks([]) ax.set_xticks([]) self.im = ax.imshow(self.data[0]) self.initialized = True else: self.im.set_data(X) self.f0 += 1 def redraw_fn(f, axes): for i in range(len(redraw_fn.sub)): redraw_fn.sub[i].draw(f, axes[i]) data = [] for i in range(N_rows * N_cols): image = torch.round(reparameterization_bernoulli(P_adv, temperature=prob_net.temperature)) assert ((image >= 0.0) & (image <= 1.0)).all() pred = get_prediction(prob_net, image, "non_prob") store_image = torch.clamp(torch.sum(image, 1), 0.0, 1.0) assert ((store_image == 0.0) | (store_image == 1.0)).all() data.append((store_image,pred)) redraw_fn.sub = [Redraw(el[0],el[1],target) for el in data] + [Redraw(torch.clamp(torch.sum(P0,1),0.0,1.0),model_pred,target)] videofig( num_frames=100, play_fps=50, redraw_func=redraw_fn, grid_specs={'nrows': N_rows+1, 'ncols': N_cols})
# Read arguments parser = argparse.ArgumentParser( description='Scrobble through driving images.') parser.add_argument('driving_log', type=str, default='', help='Path to driving_log.csv.') args = parser.parse_args() # Read raw CSV csv_data = read_sim_logs([args.driving_log]) # Show as a video def redraw_fn(f, axes): csv_row = f + 1 img_file = csv_data[f]['img_center'] img = imread(img_file) if not redraw_fn.initialized: redraw_fn.im = axes.imshow(img, animated=True) axes.set_title(csv_row) redraw_fn.initialized = True else: redraw_fn.im.set_array(img) axes.set_title(csv_row) redraw_fn.initialized = False videofig(len(csv_data), redraw_fn, play_fps=30)
# ax.set_facecolor(self.ax_color) self.l.set_ydata(s) def redraw_fn(f, axes): redraw_fn.sub1.draw(f, axes[0]) redraw_fn.sub2.draw(f, axes[1]) redraw_fn.sub3.draw(f, axes[2]) redraw_fn.sub1 = Redraw(5000, 2, 'xkcd:salmon') redraw_fn.sub2 = Redraw(7000, 10, 'xkcd:sea green') redraw_fn.sub3 = Redraw(3000, 1, 'xkcd:sky blue') if not SAVE_PLOTS: videofig(100, redraw_fn, grid_specs={ 'nrows': 2, 'ncols': 2 }, layout_specs=['[0, 0]', '[0, 1]', '[1, :]']) else: videofig(100, redraw_fn, grid_specs={ 'nrows': 2, 'ncols': 2 }, layout_specs=['[0, 0]', '[0, 1]', '[1, :]'], save_dir='example3_save')