Exemplo n.º 1
0
def test_4a():
    """Test for the different behaviour of byte strings between python 2/3."""
    version = sys.version_info[0]
    answer = version < 3 #True for py2, else False
    left = tools.is_string(b'hello bytes')
    right = answer
    message = "{0} resulted in {1}, but {2} was expected"\
        .format(b'hello bytes',left, right)
    assert left == right, message
Exemplo n.º 2
0
def test_4a():
    """Test for the different behaviour of byte strings between python 2/3."""
    version = sys.version_info[0]
    answer = version < 3 #True for py2, else False
    left = tools.is_string(b'hello bytes')
    right = answer
    message = "{0} resulted in {1}, but {2} was expected"\
        .format(b'hello bytes',left, right)
    assert left == right, message
Exemplo n.º 3
0
    def fl(self, fun, apply_to=[] , keep_duration=True):
        """ General processing of a clip.

        Returns a new Clip whose frames are a transformation
        (through function ``fun``) of the frames of the current clip.
        
        Parameters
        -----------
        
        fun
          A function with signature (gf,t -> frame) where ``gf`` will
          represent the current clip's ``get_frame`` method,
          i.e. ``gf`` is a function (t->image). Parameter `t` is a time
          in seconds, `frame` is a picture (=Numpy array) which will be
          returned by the transformed clip (see examples below).
           
        apply_to
          Can be either ``'mask'``, or ``'audio'``, or
          ``['mask','audio']``.
          Specifies if the filter ``fl`` should also be applied to the
          audio or the mask of the clip, if any.
        
        keep_duration
          Set to True if the transformation does not change the
          ``duration`` of the clip.
          
        Examples
        --------
        
        In the following ``newclip`` a 100 pixels-high clip whose video
        content scrolls from the top to the bottom of the frames of
        ``clip``.
        
        >>> fl = lambda gf,t : gf(t)[int(t):int(t)+50, :]
        >>> newclip = clip.fl(fl, apply_to='mask')
        
        """

        #mf = copy(self.make_frame)
        newclip = self.set_make_frame(lambda t: fun(self.get_frame, t))
        
        if not keep_duration:
            newclip.duration = None
            newclip.end = None
            
        if is_string(apply_to):
            apply_to = [apply_to]

        for attr in apply_to:
            if hasattr(newclip, attr):
                a = getattr(newclip, attr)
                if a is not None:
                    new_a =  a.fl(fun, keep_duration=keep_duration)
                    setattr(newclip, attr, new_a)
                    
        return newclip
Exemplo n.º 4
0
def test_4():
    '''tests the is_string function in tools'''
    lefts = ["hello straight string", r'hello raw string', 42, True]
    rights = [True, True, False, False]
    for i in range(len(lefts)):
        left = tools.is_string(lefts[i])
        right = rights[i]
        message = "{0} resulted in {1}, but {2} was expected"\
            .format(lefts[i],left, right)
        assert left == right, message
Exemplo n.º 5
0
def test_4():
    """Test the is_string function in tools."""
    lefts = ["hello straight string", r'hello raw string',42, True ]
    rights = [True, True, False, False]
    for i in range(len(lefts)):
        left = tools.is_string(lefts[i])
        right = rights[i]
        message = "{0} resulted in {1}, but {2} was expected"\
            .format(lefts[i],left, right)
        assert left == right, message
Exemplo n.º 6
0
    def __init__(self, subtitles, make_textclip=None):
        
        VideoClip.__init__(self, has_constant_size=False)

        if is_string(subtitles):
            subtitles = file_to_subtitles(subtitles)

        subtitles = [(map(cvsecs, tt),txt) for tt, txt in subtitles]
        self.subtitles = subtitles
        self.textclips = dict()

        if make_textclip is None:

            make_textclip = lambda txt: TextClip(txt, font='Georgia-Bold',
                                        fontsize=24, color='white',
                                        stroke_color='black', stroke_width=0.5)

        self.make_textclip = make_textclip
        self.start=0
        self.duration = max([tb for ((ta,tb), txt) in self.subtitles])
        self.end=self.duration
        
        def add_textclip_if_none(t):
            """ Will generate a textclip if it hasn't been generated asked
            to generate it yet. If there is no subtitle to show at t, return
            false. """
            sub =[((ta,tb),txt) for ((ta,tb),txt) in self.textclips.keys()
                   if (ta<=t<tb)]
            if sub == []:
                sub = [((ta,tb),txt) for ((ta,tb),txt) in self.subtitles if
                       (ta<=t<tb)]
                if sub == []:
                    return False
            sub = sub[0]
            if sub not in self.textclips.keys():
                self.textclips[sub] = self.make_textclip(sub[1])

            return sub

        def make_frame(t):
            sub = add_textclip_if_none(t)
            return (self.textclips[sub].get_frame(t) if sub
                    else np.array([[[0,0,0]]]))

        def make_mask_frame(t):
            sub = add_textclip_if_none(t)
            return (self.textclips[sub].mask.get_frame(t) if sub
                    else np.array([[0]]))
        
        self.make_frame = make_frame
        hasmask = (self.make_textclip('T').mask is not None)
        self.mask = (VideoClip(make_mask_frame, ismask=True) if hasmask else None)
Exemplo n.º 7
0
    def __init__(self, sequence, fps=None, durations=None, with_mask=True,
                 ismask=False, load_images=False):

        # CODE WRITTEN AS IT CAME, MAY BE IMPROVED IN THE FUTURE
        
        if (fps is None) and (duration is None):
            raise ValueError("Please provide either 'fps' or 'durations'.")
        VideoClip.__init__(self, ismask=ismask)

        # Parse the data

        fromfiles = True

        if isinstance(sequence, list):
            if is_string(sequence[0]):
                if load_images:
                    sequence = [imread(f) for f in sequence]
                    fromfiles = False
                else:
                    fromfiles= True
            else:
                # sequence is already a list of numpy arrays
                fromfiles = False
        else:
            # sequence is a folder name, make it a list of files:
            fromfiles = True
            sequence = sorted([os.path.join(sequence, f)
                        for f in os.listdir(sequence)])

        self.fps = fps
        if fps is not None:
            durations = [1.0/fps for image in sequence]
        self.durations = durations
        self.images_starts = [0]+list(np.cumsum(durations))
        self.duration = sum(durations)
        self.end = self.duration
        self.sequence = sequence
        
        def find_image_index(t):
            return max([i for i in range(len(self.sequence))
                              if self.images_starts[i]<=t])

        if fromfiles:

            self.lastindex = None
            self.lastimage = None

            def make_frame(t):
            
                index = find_image_index(t)

                if index != self.lastindex:
                    self.lastimage = imread(self.sequence[index])[:,:,:3] 
                    self.lastindex = index
                
                return self.lastimage

            if with_mask and (imread(self.sequence[0]).shape[2]==4):

                self.mask = VideoClip(ismask=True)
                self.mask.lastindex = None
                self.mask.lastimage = None

                def mask_make_frame(t):
            
                    index = find_image_index(t)
                    if index != self.mask.lastindex:
                        frame = imread(self.sequence[index])[:,:,3]
                        self.mask.lastimage = frame.astype(float)/255
                        self.mask.lastindex = index

                    return self.mask.lastimage

                self.mask.make_frame = mask_make_frame
                self.mask.size = mask_make_frame(0).shape[:2][::-1]


        else:

            def make_frame(t):
            
                index = find_image_index(t)
                return self.sequence[index][:,:,:3]

            if with_mask and (self.sequence[0].shape[2]==4):

                self.mask = VideoClip(ismask=True)

                def mask_make_frame(t):
                    index = find_image_index(t)
                    return 1.0*self.sequence[index][:,:,3]/255

                self.mask.make_frame = mask_make_frame
                self.mask.size = mask_make_frame(0).shape[:2][::-1]
        
            
        self.make_frame = make_frame
        self.size = make_frame(0).shape[:2][::-1]
Exemplo n.º 8
0
def test_is_string(given, expected):
    """Test the is_string function in tools."""
    assert tools.is_string(given) == expected