def __init__(self, directory, file_name): """Initialise variables and start stabilising""" self.directory = directory self.file = file_name self.new_file = file_name[:-4] self.stabiliser = VidStab() self.stabilise_video()
class VideoStabiliser: """ Class used to stabilise the recorded video for more optimal matching. """ def __init__(self, directory, file_name): """ Initialise variables and call the function to stabilise the specified video. :param directory: the directory where the video file to stabilise is located :param file_name: the mp4 video file's name """ self.directory = directory self.file = file_name self.new_file = file_name[:-4] self.stabiliser = VidStab() self.stabilise_video() @make_spin(Box1, "Stabilising video...") def stabilise_video(self): """ Stabilises a mp4 video and outputs the result as an avi file in the same directory. :return: """ self.stabiliser.stabilize( input_path="{}{}".format(self.directory, self.file), output_path="{}/stable-{}.avi".format(self.directory, self.new_file), border_type="reflect") print("\nVideo stabilised!")
def test_trajectory_transform_values(self): # input_vid = 'https://s3.amazonaws.com/python-vidstab/ostrich.mp4' input_vid = local_vid base_url = 'https://s3.amazonaws.com/python-vidstab' stabilizer = VidStab() for window in [15, 30, 60]: stabilizer.gen_transforms(input_path=input_vid, smoothing_window=window) transform_file = '{}/ostrich_transforms_{}.pickle'.format( base_url, window) trajectory_file = '{}/ostrich_trajectory_{}.pickle'.format( base_url, window) smooth_trajectory_file = '{}/ostrich_smooth_trajectory_{}.pickle'.format( base_url, window) with urlopen(transform_file) as f: expected_transforms = pickle.load(f) with urlopen(trajectory_file) as f: expected_trajectory = pickle.load(f) with urlopen(smooth_trajectory_file) as f: expected_smooth_trajectory = pickle.load(f) self.assertTrue( np.allclose(stabilizer.transforms, expected_transforms)) self.assertTrue( np.allclose(stabilizer.trajectory, expected_trajectory)) self.assertTrue( np.allclose(stabilizer.smoothed_trajectory, expected_smooth_trajectory))
def test_trajectory_transform_values(): for window in [15, 30, 60]: stabilizer = VidStab(processing_max_dim=float('inf')) stabilizer.stabilize(input_path=OSTRICH_VIDEO, output_path='stable.avi', smoothing_window=window) pickle_test_transforms(stabilizer, 'pickled_transforms') check_transforms(stabilizer, is_cv4=imutils.is_cv4())
def test_kp_options(self): stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False) self.assertFalse(stabilizer.kp_detector.getNonmaxSuppression(), 'FAST kp non-max suppression flag') self.assertEqual(stabilizer.kp_detector.getThreshold(), 42, 'FAST kp custom threshold') with self.assertRaises(TypeError) as err: VidStab(kp_method='FAST', fake='fake') self.assertTrue(isinstance(err.exception, TypeError), 'reject bad kwargs')
def test_trajectory_transform_values(): for window in [15, 30, 60]: stabilizer = VidStab() stabilizer.gen_transforms(input_path=OSTRICH_VIDEO, smoothing_window=window) pickle_test_transforms(stabilizer, 'pickled_transforms') check_transforms(stabilizer, is_cv4=imutils.is_cv4())
def test_kp_options(): stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False) assert not stabilizer.kp_detector.getNonmaxSuppression() assert stabilizer.kp_detector.getThreshold() == 42 with pytest.raises(TypeError) as err: VidStab(kp_method='FAST', fake='fake') assert 'invalid keyword argument' in str(err.value)
def test_writer_reset(): path_1 = 'stable_1.avi' path_2 = 'stable_2.avi' stabilizer = VidStab() stabilizer.stabilize(OSTRICH_VIDEO, path_1, max_frames=16, smoothing_window=1) stabilizer.stabilize(OSTRICH_VIDEO, path_2, max_frames=16, smoothing_window=1) assert os.path.exists(path_1) assert os.path.exists(path_2)
def video_stabilizer(input_video_filepath, output_video_filepath): # input_video_filepath = glob.glob('/home/afarahani/Projects/output/*') # output_video_filepath = '/home/afarahani/Projects/stab_output/' for video_path in input_video_filepath: head, tail = os.path.split(video_path) stabilizer = VidStab() # black borders stabilizer.stabilize(input_path=video_path, output_path=output_video_filepath+tail, border_type='black')
def __init__(self, directory, file_name): """ Initialise variables and call the function to stabilise the specified video. :param directory: the directory where the video file to stabilise is located :param file_name: the mp4 video file's name """ self.directory = directory self.file = file_name self.new_file = file_name[:-4] self.stabiliser = VidStab() self.stabilise_video()
def run(self): for vid in self.vids: out_vid = os.path.join(self.output_path, os.path.basename(vid)) if os.path.isfile(out_vid): continue try: stabilizer = VidStab() stabilizer.stabilize(input_path=vid, output_path=out_vid) except: print ("error in ", vid) shutil.copy(vid, out_vid)
def stabilize_video(source, video, destination_folder): input_video_path = source + '/' + video output_file_path = destination_folder + "/" + \ os.path.basename(video).split('.')[-2] + "_stabilized.mp4" print("Video to be stabilized:" + input_video_path) stabilizer = VidStab(kp_method='FAST') stabilizer.stabilize(input_path=input_video_path, output_path=output_file_path, border_type='black') print("Output file is ready: " + output_file_path)
def test_max_frames(): max_frames = 16 with tempfile.TemporaryDirectory() as tmpdir: output_path = '{}/stable_1.avi'.format(tmpdir) stabilizer = VidStab() stabilizer.stabilize(OSTRICH_VIDEO, output_path, max_frames=max_frames, smoothing_window=1) output_frame_count = imutils.video.count_frames(output_path) assert max_frames == output_frame_count
def test_video_dep_funcs_run(self): # just tests to check functions run # input_vid = 'https://s3.amazonaws.com/python-vidstab/trunc_video.avi' input_vid = local_trunc_vid stabilizer = VidStab() stabilizer.gen_transforms(input_vid, smoothing_window=1, show_progress=True) self.assertEqual(stabilizer.smoothed_trajectory.shape, stabilizer.trajectory.shape, 'trajectory/transform obj shapes') self.assertEqual(stabilizer.transforms.shape, stabilizer.trajectory.shape, 'trajectory/transform obj shapes') with tempfile.TemporaryDirectory() as tmpdir: output_vid = '{}/test_output.avi'.format(tmpdir) try: stabilizer.apply_transforms(input_vid, output_vid) except Exception as e: self.fail("stabilizer.apply_transforms ran into {}".format(e)) try: stabilizer.stabilize(input_vid, output_vid, smoothing_window=1) except Exception as e: self.fail("stabilizer.stabilize ran into {}".format(e))
def test_output_fps(): force_fps = 10 with tempfile.TemporaryDirectory() as tmpdir: output_vid = '{}/test_output.avi'.format(tmpdir) stabilizer = VidStab() stabilizer.stabilize(OSTRICH_VIDEO, output_vid, max_frames=16, smoothing_window=1, output_fps=force_fps) output_fps = cv2.VideoCapture(output_vid).get(cv2.CAP_PROP_FPS) assert force_fps == output_fps
def test_trajectory_transform_values(): for window in [15, 30, 60]: stabilizer = VidStab() stabilizer.gen_transforms(input_path=ostrich_video, smoothing_window=window) pickle_test_transforms(stabilizer, 'pickled_transforms') unpickled_transforms = download_pickled_transforms( window, cv4=imutils.is_cv4()) assert np.allclose(stabilizer.transforms, unpickled_transforms[0]) assert np.allclose(stabilizer.trajectory, unpickled_transforms[1]) assert np.allclose(stabilizer.smoothed_trajectory, unpickled_transforms[2])
class VideoStabiliser: def __init__(self, directory, file_name): """Initialise variables and start stabilising""" self.directory = directory self.file = file_name self.new_file = file_name[:-4] self.stabiliser = VidStab() self.stabilise_video() @make_spin(Box1, "Stabilising video...") def stabilise_video(self): self.stabiliser.stabilize( input_path="{}{}".format(self.directory, self.file), output_path="{}/stable-{}.avi".format(self.directory, self.new_file), border_type="reflect") print("\nVideo stabilised!")
def test_stabilize_frame(): # Init stabilizer and video reader stabilizer = VidStab(processing_max_dim=float('inf')) vidcap = cv2.VideoCapture(OSTRICH_VIDEO) window_size = 30 while True: _, frame = vidcap.read() # Pass frame to stabilizer even if frame is None stabilized_frame = stabilizer.stabilize_frame( input_frame=frame, smoothing_window=window_size, border_size=10) if stabilized_frame is None: break check_transforms(stabilizer, is_cv4=imutils.is_cv4())
def stabilize_video(): stabilizer = VidStab() stabilizer.stabilize(input_path='static/Uploads/file.mp4', output_path='static/Output/stable_video.avi') stabilizer.plot_trajectory() plt.savefig('static/img/plot_trajectory.png') stabilizer.plot_transforms() plt.savefig('static/img/plot_transforms.png') return ('________________Completed convertion_____________')
def main(original_video_file, stabilized_video_file): stabilizer = VidStab() stabilizer.stabilize(input_path=original_video_file, output_path=stabilized_video_file) stabilizer.plot_trajectory() plt.show() stabilizer.plot_transforms() plt.show()
def __init__(self, camera, camera_rasp, show_image): self.LAT_LONG = [0.0, 0.0, 0.0, 0.0] self.LIMIT = 6 self.DISTANCE = 15 self.SIZE = 480 self.CODE, self.READ_CODE = '', '' self.CAMERA = camera self.show_image = show_image self.camera_rasp = camera_rasp self.stabilizer = VidStab()
def test_video_dep_funcs_run(): # just tests to check functions run stabilizer = VidStab() stabilizer.gen_transforms(TRUNCATED_OSTRICH_VIDEO, smoothing_window=2, show_progress=True) assert stabilizer.smoothed_trajectory.shape == stabilizer.trajectory.shape assert stabilizer.transforms.shape == stabilizer.trajectory.shape with tempfile.TemporaryDirectory() as tmpdir: output_vid = '{}/test_output.avi'.format(tmpdir) stabilizer.apply_transforms(TRUNCATED_OSTRICH_VIDEO, output_vid) stabilizer.stabilize(TRUNCATED_OSTRICH_VIDEO, output_vid, smoothing_window=2)
def test_stabilize_frame(): # Init stabilizer and video reader stabilizer = VidStab() vidcap = cv2.VideoCapture(ostrich_video) window_size = 30 while True: grabbed_frame, frame = vidcap.read() # Pass frame to stabilizer even if frame is None stabilized_frame = stabilizer.stabilize_frame( input_frame=frame, smoothing_window=window_size, border_size=10) if stabilized_frame is None: break unpickled_transforms = download_pickled_transforms(window_size, cv4=imutils.is_cv4()) assert np.allclose(stabilizer.transforms, unpickled_transforms[0]) assert np.allclose(stabilizer.trajectory, unpickled_transforms[1]) assert np.allclose(stabilizer.smoothed_trajectory, unpickled_transforms[2])
def test_resize(): # Init stabilizer and video reader max_dim = 30 stabilizer = VidStab(processing_max_dim=max_dim) assert stabilizer.processing_max_dim == max_dim # noinspection PyProtectedMember assert stabilizer._processing_resize_kwargs == {} vidcap = cv2.VideoCapture(OSTRICH_VIDEO) _, frame = vidcap.read() _ = stabilizer.stabilize_frame(input_frame=frame, smoothing_window=1) _, frame = vidcap.read() stabilized_frame = stabilizer.stabilize_frame(input_frame=frame, smoothing_window=1) assert stabilized_frame.shape == (446, 876, 3) assert max(stabilizer.prev_gray.shape) <= max_dim # noinspection PyProtectedMember assert stabilizer._processing_resize_kwargs == {'width': max_dim}
def main(): # Video Stabilizer device_val = None stabilizer = VidStab() # For webcam input: cap = cv2.VideoCapture(0) cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) # set new dimensions to cam object (not cap) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) cap.set(cv2.CAP_PROP_FPS, 120) # Check OS os = platform.system() if os == "Linux": device_val = "/dev/video2" # Start virtual camera with pyvirtualcam.Camera(1280, 720, 120, device=device_val, fmt=PixelFormat.BGR) as cam: print('Virtual camera device: ' + cam.device) while True: success, img = cap.read() img = frame_manipulate(img) # Stabilize the image to make sure that the changes with Zoom are very smooth img = stabilizer.stabilize_frame(input_frame=img, smoothing_window=2, border_size=-20) # Resize the image to make sure it does not crash pyvirtualcam img = cv2.resize(img, (1280, 720), interpolation=cv2.INTER_CUBIC) cam.send(img) cam.sleep_until_next_frame()
def test_invalid_input_path(): stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False) with pytest.raises(FileNotFoundError) as err: stabilizer.gen_transforms('fake_input_path.mp4') assert 'fake_input_path.mp4 does not exist' in str(err.value) with pytest.raises(FileNotFoundError) as err: stabilizer.stabilize('fake_input_path.mp4', 'output.avi') assert 'fake_input_path.mp4 does not exist' in str(err.value) with pytest.raises(ValueError) as err: tmp_file = tempfile.NamedTemporaryFile(suffix='.mp4') with pytest.warns(UserWarning, match='No progress bar will be shown'): stabilizer.stabilize(tmp_file.name, 'output.avi') assert 'First frame is None' in str(err.value)
def test_video_dep_funcs_run(): # just tests to check functions run stabilizer = VidStab() stabilizer.gen_transforms(local_trunc_vid, smoothing_window=2, show_progress=True) assert stabilizer.smoothed_trajectory.shape == stabilizer.trajectory.shape assert stabilizer.transforms.shape == stabilizer.trajectory.shape with tempfile.TemporaryDirectory() as tmpdir: output_vid = '{}/test_output.avi'.format(tmpdir) try: stabilizer.apply_transforms(local_trunc_vid, output_vid) except Exception as e: pytest.fail("stabilizer.apply_transforms ran into {}".format(e)) try: stabilizer.stabilize(local_trunc_vid, output_vid, smoothing_window=2) except Exception as e: pytest.fail("stabilizer.stabilize ran into {}".format(e))
def test_invalid_input_path(): stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False) with pytest.raises(FileNotFoundError) as err: stabilizer.gen_transforms("fake_input_path.mp4") assert "fake_input_path.mp4 does not exist" in str(err.value) with pytest.raises(FileNotFoundError) as err: stabilizer.stabilize("fake_input_path.mp4", "output.avi") assert "fake_input_path.mp4 does not exist" in str(err.value) with pytest.raises(ValueError) as err: tmp_file = tempfile.NamedTemporaryFile(suffix=".mp4") stabilizer.stabilize(tmp_file.name, "output.avi") assert "First frame is None" in str(err.value)
def test_invalid_input_path(): stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False) with pytest.raises(FileNotFoundError) as err: stabilizer.gen_transforms("fake_input_path.mp4") assert "fake_input_path.mp4 does not exist" in str(err.value) with pytest.raises(FileNotFoundError) as err: stabilizer.stabilize("fake_input_path.mp4", "output.avi") assert "fake_input_path.mp4 does not exist" in str(err.value)
def vidstab(input_path, output_path, kp_method='GFTT', smoothing_window=30, border_type='black', border_size=0): # stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False) # Using different parameters stabilizer = VidStab(kp_method=kp_method) # Default stabilizer.stabilize(input_path=input_path, output_path=output_path, smoothing_window=smoothing_window, border_type=border_type, border_size=border_size) stabilizer.plot_trajectory() plt.show() stabilizer.plot_transforms() plt.show() return
# local_vid = '{}/vid.avi'.format(tmp_dir) # local_vid = '/home/som/Downloads/SequenceOfBscanForSom/SequenceOfBscan_Niksa_20181109/video_orig.avi' # local_vid = '/home/som/data/OCT/videostab_results/shaky_car_MATLAB/original_vid.avi' # local_vid = '/home/som/Downloads/Bscan/video_orig.avi' # local_vid = '/home/som/Downloads/Bscan/video_Bscan46_bilateral_19_19.avi' # stab_local_trunc_vid = '{}/stab_trunc_vid.avi'.format(tmp_dir) # stab_local_vid = '{}/stab_vid.avi'.format(tmp_dir) # stab_local_vid = '/home/som/Downloads/SequenceOfBscanForSom/SequenceOfBscan_Niksa_20181109/video_stab_orig_2.avi' # stab_local_vid = '/home/som/data/OCT/videostab_results/shaky_car_MATLAB/stab_vid_vidstab_2.avi' # stab_local_vid = '/home/som/Downloads/Bscan/stab_video_orig.avi' # stab_local_vid = '/home/som/Downloads/Bscan/stab_video_Bscan46_bilateral_19_19.avi' # urlretrieve(remote_trunc_vid, local_trunc_vid) # urlretrieve(remote_vid, local_vid) stabilizer = VidStab(kp_method='ORB',stab_algo=args.stab_algo,scale=args.scale) print("stab_algo:",stabilizer.stab_algo) stabilizer.stabilize(input_path=local_vid, output_path=stab_local_vid,smoothing_window=args.smoothing_window,playback=True,border_type='replicate',max_frames=args.max_frames) sys.path.append('/home/som/code/Video-Stabilization/src/functs/') from stabFuncts import * # ITF ITF_crop_flag = False if args.live: local_vid = stab_local_vid[:-4]+'_input.avi' print("ITF of input video", getITF(local_vid,crop=ITF_crop_flag)) print("ITF of stabilized video", getITF(stab_local_vid,crop=ITF_crop_flag)) os.system("python Movie2Frame.py --in-file-name {} --max-frames 10 --write-gray".format(local_vid.split('/')[-1]))