# inputs input_image = r'..\images\opencv.png' input_video = r'..\videos\ps3-4-a\ps3-4-a.mp4' output_video = r'..\videos\ps3-4-a\test.mp4' num_features = 5000 num_matches = 2000 ransac_thresh = 10.0 # median_size = 11 # sobel_size = 3 # edge_thresh = 40 # file io img = cv2.imread(input_image) fps = ear.get_fps(input_video) gen = ear.generate_frames(input_video) # initialize a = next(gen) rows, cols = a.shape[:2] # bbox = ear.get_initial_bbox(img, a, median_size, sobel_size, edge_thresh) bbox = np.array([[134.0, 108.0], [447.0, 109.0], [133.0, 250.0], [446.0, 256.0]]) Ha = ear.get_initial_homography(img, bbox) with ear.VideoWriter(output_video, fps, (cols, rows)) as writer: # write initial frame frame = ear.project_image(img, a, Ha) writer.write(frame)
def main(params): # file io if not os.path.isfile(params['input_video']): raise FileNotFoundError('no such input video: %s' % params['input_video']) img = cv2.imread(params['input_image']) fps = ear.get_fps(params['input_video']) gen = ear.generate_frames(params['input_video']) output_dir = os.path.dirname(params['output_prefix']) if not os.path.isdir(output_dir): os.makedirs(output_dir) _, ext = os.path.splitext(params['input_video']) output_video = params['output_prefix'] + ext # initialize a = next(gen) rows, cols = a.shape[:2] bbox = ear.get_initial_bbox(img, a, params['median_size'], params['sobel_size'], params['edge_thresh']) Ha = ear.get_initial_homography(img, bbox) # allocate frame cache frames = np.empty((params['frame_step'], rows, cols, 3), dtype=np.uint8) with ear.VideoWriter(output_video, fps, (cols, rows)) as writer: msg = 'writing to %s...' % output_video print(msg) # write initial frame frame = ear.project_image(img, a, Ha) writer.write(frame) for frame_count, b in enumerate(gen, 1): if params['verbose']: print('on frame %i' % frame_count) i = (frame_count - 1) % params['frame_step'] # pocket frame frames[i] = b if i != params['frame_step'] - 1: continue # update cumulative homography Hab = ear.get_homography(a, b, params['num_features'], params['num_matches'], params['ransac_thresh'], num_anms=params.get('num_anms')) Hb = ear.update_homography(Ha, Hab) # compute partial homographies homographies = ear.interpolate_homographies(Ha, Hb, params['frame_step'], method='direct') for b, Hb in zip(frames, homographies): # write frames frame = ear.project_image(img, b, Hb, copy=True) writer.write(frame) # update a a[:] = b Ha[:] = Hb if 'num_frames' in params and frame_count > params['num_frames']: break print(msg + 'done!')
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import matplotlib.pyplot as plt import context import ear if __name__ == '__main__': fname = r'..\videos\ps3-4-a\ps3-4-a.mp4' gen = ear.generate_frames(fname) for frame in gen: break ear.easy_imshow(frame, num=1)