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!")
Example #2
0
    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))
Example #3
0
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())
Example #4
0
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')
Example #6
0
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 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_____________')
Example #8
0
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 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 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
Example #11
0
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)
Example #12
0
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_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
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!")
Example #15
0
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)
Example #16
0
def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'],
                                    encoding='utf-8')

    if VIDSTAB_ERROR is not None:
        raise VIDSTAB_ERROR

    try:
        in_path = download_file(bucket, key)
        out_path = "/tmp/out_{}".format(key)
        head_response = s3.head_object(Bucket=bucket, Key=key)
        size = head_response['ContentLength']
        content_type = head_response['ContentType']
        if size <= MAX_SIZE:
            # Do the stabilization
            if content_type[0:5] == "video":
                stabilizer = VidStab(kp_method=KP_METHOD)
                if BORDER_TYPE == "black":
                    stabilizer.stabilize(input_path=in_path,
                                         output_path=out_path,
                                         border_type=BORDER_TYPE,
                                         border_size=BORDER_SIZE)
                else:
                    stabilizer.stabilize(input_path=in_path,
                                         output_path=out_path,
                                         border_type=BORDER_TYPE)
                return upload_file(OUTPUT_BUCKET, key, out_path)
            else:
                raise IOError("Object {} from bucket {} ".format(key, bucket) +
                              "is not a video.")
        else:
            raise IOError("Object {} from bucket {} ".format(key, bucket) +
                          "exceeds max allowable size for stabilization.")

    except IOError as e:
        logger.exception("IOError occurred during lambda handling")
        raise e
    except Exception as e:
        logger.exception(
            "Error processing object {} from bucket {}. ".format(key, bucket) +
            "Make sure your object and bucket exist and your bucket is in the same region as this function."
        )
        raise e
Example #17
0
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))
Example #18
0
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_writer_reset():
    with tempfile.TemporaryDirectory() as tmpdir:
        path_1 = '{}/stable_1.avi'.format(tmpdir)
        path_2 = '{}/stable_2.avi'.format(tmpdir)

        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)

        imutils.video.count_frames(path_1)
Example #20
0
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
Example #21
0
def adam_spanbaauer(video_pth: str, debug: bool = False):
    if os.path.exists('../.cache/default_stable_video.avi'):
        print("Cached Video")
        out = cv2.VideoCapture('../.cache/default_stable_video.avi')

    else:
        # Using defaults
        stabilizer = VidStab()
        stabilizer.stabilize(input_path=video_pth,
                             output_path='../.cache/default_stable_video.avi')

        # Using a specific keypoint detector
        stabilizer = VidStab(kp_method='ORB')
        stabilizer.stabilize(input_path=video_pth,
                             output_path='../.cache/ORB_stable_video.avi')

        # Using a specific keypoint detector and customizing keypoint parameters
        stabilizer = VidStab(kp_method='FAST',
                             threshold=42,
                             nonmaxSuppression=False)
        stabilizer.stabilize(input_path=video_pth,
                             output_path='../.cache/FAST_stable_video.avi')

        out = cv2.VideoCapture('../.cache/default_stable_video.avi')

        if debug:
            stabilizer.plot_trajectory()
            plt.show()

            stabilizer.plot_transforms()
            plt.show()
    generate_frames(out, "adam")
# remote_ostrich_vid = 'https://s3.amazonaws.com/python-vidstab/ostrich.mp4'
# local_ostrich_vid = '{}/ostrich.mp4'.format(tmp_dir.name)
# urlretrieve(remote_ostrich_vid, local_ostrich_vid)
# local_vid = local_ostrich_vid

remote_skateline_vid = 'https://s3.amazonaws.com/python-vidstab/thrasher.mp4'
local_skateline_vid = '{}/skateline.mp4'.format(tmp_dir.name)
urlretrieve(remote_skateline_vid, local_skateline_vid)
local_vid = local_skateline_vid

# set params for test stabilization
input_path = local_vid
border_type = 'black'
border_size = 'auto'
layer_func = layer_overlay
playback = True

stabilizer = VidStab()
stabilizer.stabilize(input_path,
                     '{}/stable.avi'.format(tmp_dir.name),
                     border_type=border_type,
                     border_size=border_size,
                     layer_func=layer_func,
                     playback=playback)

stabilizer.plot_transforms()
plt.show()

stabilizer.plot_transforms(radians=True)
plt.show()
Example #23
0
import cv2
import numpy as np
from vidstab import VidStab

stabilizer = VidStab()
stabilizer.stabilize(input_path="Mouth.mp4", output_path="Stablize head.mp4", output_fourcc="mp4v", smoothing_window=100)
print("Video is stabilized")
from vidstab import VidStab
import matplotlib.pyplot as plt
import os
import cv2

stabilizer = VidStab()
stabilizer.stabilize(input_path='video_seq_1.avi',
                     output_path='stable_video.avi')

# Image Feature Extractionn using Key Point Detection
##############################################
stabilizer = VidStab(kp_method='ORB')
stabilizer.stabilize(input_path='video_seq_1.avi',
                     output_path='stable_video.avi',
                     border_type='black')

stabilizer = VidStab(kp_method='FAST', threshold=42, nonmaxSuppression=False)
stabilizer.stabilize(input_path='video_seq_1.avi',
                     output_path='stable_video.avi',
                     border_type='black',
                     border_size=100)
##############################################

#Plotting frame to frame transformations
stabilizer.plot_trajectory()
plt.show()

stabilizer.plot_transforms()
plt.show()

##############################################
# stabilizer.stabilize(input_path=input_vid,
#                      output_path='rep_stable_video.avi',
#                      border_type='replicate',
#                      border_size=100)
#
# # reflected border
# stabilizer.stabilize(input_path=input_vid,
#                      output_path='ref_stable_video.avi',
#                      border_type='reflect',
#                      border_size=100)

# USING LAYERING FUNCTIONS

# trail of frames
stabilizer.stabilize(input_path=input_vid,
                     output_path='trail_stable_video.avi',
                     border_type='black',
                     border_size=100,
                     layer_func=layer_overlay)


def layer_custom(foreground, background):
    return layer_blend(foreground, background, foreground_alpha=.8)


stabilizer.stabilize(input_path=input_vid,
                     output_path='blend_stable_video.avi',
                     border_type='black',
                     border_size=100,
                     layer_func=layer_custom)
#                             border_size='auto',
#                             playback=True)


##################################################################
# TEST TYPICAL STABILIZATION PROCESS
##################################################################
download_to_path = f'{tmp_dir.name}/test_video.mp4'
dl.download_ostrich_video(download_to_path)
# dl.download_skateline_video(download_to_path)

stabilizer = VidStab()
stabilizer.stabilize(download_to_path,
                     'stable.avi',
                     # max_frames=30,
                     border_type='black',
                     border_size='auto',
                     layer_func=layer_overlay,
                     playback=True)

##################################################################
# TEST PLOT OUTPUT
##################################################################
# import matplotlib.pyplot as plt
#
# download_to_path = f'{tmp_dir.name}/test_video.mp4'
# dl.download_ostrich_video(download_to_path)
# stabilizer = VidStab()
# stabilizer.gen_transforms(download_to_path)
#
# stabilizer.plot_transforms()
Example #27
0
"""Playback tests for visual inspection of output
"""

import tempfile
from vidstab import VidStab
import matplotlib.pyplot as plt

input_path = 'readme/ostrich.mp4'

border_type = 'reflect'
border_size = 'auto'
layer_func = None
playback = False

tmp_dir = tempfile.TemporaryDirectory()
output_path = '{}/stable.avi'.format(tmp_dir.name)

stabilizer = VidStab()
stabilizer.stabilize(input_path,
                     output_path,
                     border_type=border_type,
                     border_size=border_size,
                     layer_func=layer_func,
                     playback=playback)

stabilizer.plot_transforms()
plt.show()

stabilizer.plot_transforms(radians=True)
plt.show()
Example #28
0
# 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]))
frame_filename=local_vid[:-4]+'/frame8_'+local_vid[:-4].split('/')[-1]+'.jpg'
print("filename of frame of input video:",frame_filename)