simple_ffmpeg_batch_io.FrameContainer

Create a FrameContainer with data and associated timestamp(s)

Authors

Dominique Vaufreydaz

 1"""
 2Create a ``FrameContainer`` with data and associated timestamp(s)
 3
 4Authors
 5-------
 6Dominique Vaufreydaz
 7
 8"""
 9
10__authors__ = ("Dominique Vaufreydaz")
11
12import numpy as np
13
14# class to get audio or video frames with associated timestamp list
15class FrameContainer:
16    """
17    Create a Container with audio or image data and associated timestamp(s).
18    """
19
20    class FrameContainerException(Exception):
21        """
22        Dedicated exception class for FrameContainer class.
23        """
24        def __init__(self, message="Error while setting FrameCounter parameters."):
25            self.message = message
26            super().__init__(self.message)
27
28    nb_frames: int
29    """ number of frames in the frame container. 1 for audio frame or images, n for a batch. """
30
31    data: np.array
32    """ Audio frame or image, or batch of images or audio frames. """
33
34    timestamps: np.array
35    """ an np.array of timestamp(s) associated to the data frame(s). """
36
37    def __init__(self, nb_frames : int, frames : np.array, fps : float, start_time : float = 0.0 ):
38        """
39        Create a FrameContainer object with data and associated timestamps
40
41        Parameters
42        ----------
43        nb_frames: int.
44            Number of frame(s) in the frame container. Either 1, either number of element in the batch.
45
46        frames:  np.array
47            Data for image, audio frame or batch of them.
48
49        fps : float.
50            fps of the read stream.
51            
52        start_time: float (default = 0.0).
53            Start time of the current FrameContainer, i.e. time of the (first) frame.
54        """
55        
56        # check params
57        if nb_frames <= 0:
58            raise self.FrameContainerException("nb_frames must be > 0.")
59        if nb_frames > 1 and frames.shape[0] != nb_frames:
60            raise self.FrameContainerException("nb_frames and batch size (frames.shape[0]) differs.")
61        if fps <= 0.0:
62            raise self.FrameContainerException("fps must be > 0.0.")
63        if start_time < 0.0:
64            raise self.FrameContainerException("start_time must be >= 0.0.")
65
66        self.nb_frames = nb_frames
67        self.data = frames
68        # first frame is at time start_time
69        self.timestamps = np.linspace(start_time, start_time+(self.nb_frames-1)*fps, self.nb_frames).tolist()
70
71    def totorch(self):
72        """
73        Convert the numy array into a pytorch tensor.
74
75        Returns
76        ----------
77            pytorch tensor.
78        """
79        # import torch late as it is not in the standard requirements for simple-ffmpeg-batch-io
80        import torch
81        return torch.from_numpy(self.data)
class FrameContainer:
16class FrameContainer:
17    """
18    Create a Container with audio or image data and associated timestamp(s).
19    """
20
21    class FrameContainerException(Exception):
22        """
23        Dedicated exception class for FrameContainer class.
24        """
25        def __init__(self, message="Error while setting FrameCounter parameters."):
26            self.message = message
27            super().__init__(self.message)
28
29    nb_frames: int
30    """ number of frames in the frame container. 1 for audio frame or images, n for a batch. """
31
32    data: np.array
33    """ Audio frame or image, or batch of images or audio frames. """
34
35    timestamps: np.array
36    """ an np.array of timestamp(s) associated to the data frame(s). """
37
38    def __init__(self, nb_frames : int, frames : np.array, fps : float, start_time : float = 0.0 ):
39        """
40        Create a FrameContainer object with data and associated timestamps
41
42        Parameters
43        ----------
44        nb_frames: int.
45            Number of frame(s) in the frame container. Either 1, either number of element in the batch.
46
47        frames:  np.array
48            Data for image, audio frame or batch of them.
49
50        fps : float.
51            fps of the read stream.
52            
53        start_time: float (default = 0.0).
54            Start time of the current FrameContainer, i.e. time of the (first) frame.
55        """
56        
57        # check params
58        if nb_frames <= 0:
59            raise self.FrameContainerException("nb_frames must be > 0.")
60        if nb_frames > 1 and frames.shape[0] != nb_frames:
61            raise self.FrameContainerException("nb_frames and batch size (frames.shape[0]) differs.")
62        if fps <= 0.0:
63            raise self.FrameContainerException("fps must be > 0.0.")
64        if start_time < 0.0:
65            raise self.FrameContainerException("start_time must be >= 0.0.")
66
67        self.nb_frames = nb_frames
68        self.data = frames
69        # first frame is at time start_time
70        self.timestamps = np.linspace(start_time, start_time+(self.nb_frames-1)*fps, self.nb_frames).tolist()
71
72    def totorch(self):
73        """
74        Convert the numy array into a pytorch tensor.
75
76        Returns
77        ----------
78            pytorch tensor.
79        """
80        # import torch late as it is not in the standard requirements for simple-ffmpeg-batch-io
81        import torch
82        return torch.from_numpy(self.data)

Create a Container with audio or image data and associated timestamp(s).

FrameContainer( nb_frames: int, frames: <built-in function array>, fps: float, start_time: float = 0.0)
38    def __init__(self, nb_frames : int, frames : np.array, fps : float, start_time : float = 0.0 ):
39        """
40        Create a FrameContainer object with data and associated timestamps
41
42        Parameters
43        ----------
44        nb_frames: int.
45            Number of frame(s) in the frame container. Either 1, either number of element in the batch.
46
47        frames:  np.array
48            Data for image, audio frame or batch of them.
49
50        fps : float.
51            fps of the read stream.
52            
53        start_time: float (default = 0.0).
54            Start time of the current FrameContainer, i.e. time of the (first) frame.
55        """
56        
57        # check params
58        if nb_frames <= 0:
59            raise self.FrameContainerException("nb_frames must be > 0.")
60        if nb_frames > 1 and frames.shape[0] != nb_frames:
61            raise self.FrameContainerException("nb_frames and batch size (frames.shape[0]) differs.")
62        if fps <= 0.0:
63            raise self.FrameContainerException("fps must be > 0.0.")
64        if start_time < 0.0:
65            raise self.FrameContainerException("start_time must be >= 0.0.")
66
67        self.nb_frames = nb_frames
68        self.data = frames
69        # first frame is at time start_time
70        self.timestamps = np.linspace(start_time, start_time+(self.nb_frames-1)*fps, self.nb_frames).tolist()

Create a FrameContainer object with data and associated timestamps

Parameters

nb_frames: int. Number of frame(s) in the frame container. Either 1, either number of element in the batch.

frames: np.array Data for image, audio frame or batch of them.

fps : float. fps of the read stream.

start_time: float (default = 0.0). Start time of the current FrameContainer, i.e. time of the (first) frame.

nb_frames: int

number of frames in the frame container. 1 for audio frame or images, n for a batch.

data: <built-in function array>

Audio frame or image, or batch of images or audio frames.

timestamps: <built-in function array>

an np.array of timestamp(s) associated to the data frame(s).

def totorch(self):
72    def totorch(self):
73        """
74        Convert the numy array into a pytorch tensor.
75
76        Returns
77        ----------
78            pytorch tensor.
79        """
80        # import torch late as it is not in the standard requirements for simple-ffmpeg-batch-io
81        import torch
82        return torch.from_numpy(self.data)

Convert the numy array into a pytorch tensor.

Returns

pytorch tensor.
class FrameContainer.FrameContainerException(builtins.Exception):
21    class FrameContainerException(Exception):
22        """
23        Dedicated exception class for FrameContainer class.
24        """
25        def __init__(self, message="Error while setting FrameCounter parameters."):
26            self.message = message
27            super().__init__(self.message)

Dedicated exception class for FrameContainer class.

FrameContainer.FrameContainerException(message='Error while setting FrameCounter parameters.')
25        def __init__(self, message="Error while setting FrameCounter parameters."):
26            self.message = message
27            super().__init__(self.message)
message