Skip to content

Input Test

Error

Bases: Exception

Base class for exceptions in this module.

InputError

InputError(message)

Bases: Error

Exception raised for errors in the input.

Parameters:

Name Type Description Default
message str

Explanation of the error.

required
Source code in musicalgestures/_input_test.py
16
17
def __init__(self, message):
    self.message = message

mg_input_test

mg_input_test(filename, array, fps, filtertype, threshold, starttime, endtime, blur, skip, frames)

Gives feedback to user if initialization from input went wrong.

Parameters:

Name Type Description Default
filename str

Path to the input video file.

required
array ndarray

Generates an MgVideo object from a video array. Defauts to None.

required
fps float

The frequency at which consecutive images from the video array are captured or displayed. Defauts to None.

required
filtertype str

'Regular' turns all values below threshold to 0. 'Binary' turns all values below threshold to 0, above threshold to 1. 'Blob' removes individual pixels with erosion method.

required
threshold float

A number in the range of 0 to 1. Eliminates pixel values less than given threshold.

required
starttime int / float

Trims the video from this start time (s).

required
endtime int / float

Trims the video until this end time (s).

required
blur str

'Average' to apply a 10px * 10px blurring filter, 'None' otherwise.

required
skip int

Every n frames to discard. skip=0 keeps all frames, skip=1 skips every other frame.

required
frames int

Specify a fixed target number of frames to extract from the video.

required

Raises:

Type Description
InputError

If the types or options are wrong in the input.

Source code in musicalgestures/_input_test.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def mg_input_test(filename, array, fps, filtertype, threshold, starttime, endtime, blur, skip, frames):
    """
    Gives feedback to user if initialization from input went wrong.

    Args:
        filename (str): Path to the input video file.
        array (np.ndarray, optional): Generates an MgVideo object from a video array. Defauts to None.
        fps (float, optional): The frequency at which consecutive images from the video array are captured or displayed. Defauts to None.
        filtertype (str): 'Regular' turns all values below `threshold` to 0. 'Binary' turns all values below `threshold` to 0, above `threshold` to 1. 'Blob' removes individual pixels with erosion method.
        threshold (float): A number in the range of 0 to 1. Eliminates pixel values less than given threshold.
        starttime (int/float): Trims the video from this start time (s).
        endtime (int/float): Trims the video until this end time (s).
        blur (str): 'Average' to apply a 10px * 10px blurring filter, 'None' otherwise.
        skip (int): Every n frames to discard. `skip=0` keeps all frames, `skip=1` skips every other frame.
        frames (int): Specify a fixed target number of frames to extract from the video. 

    Raises:
        InputError: If the types or options are wrong in the input.
    """

    # Check if FFmpeg is installed
    try:
        subprocess.check_call(['ffmpeg', '-version'])
    except:
        msg = 'FFmpeg must be installed and accessible via the path environment variable.\nMore information on how to install FFmpeg: https://github.com/fourMs/MGT-python/wiki/0-%E2%80%90-Installation'
        raise InputError(msg)

    filenametest = type(filename) == str

    if filenametest:
        if array is not None:
            if fps is None:
                msg = 'Please specify frame per second (fps) parameter for generating video from array.'
                raise InputError(msg)

        if filtertype.lower() not in ['regular', 'binary', 'blob']:
            msg = 'Please specify a filter type as str: "Regular", "Binary" or "Blob"'
            raise InputError(msg)

        if blur.lower() not in ['average', 'none']:
            msg = 'Please specify a blur type as str: "Average" or "None"'
            raise InputError(msg)

        if not isinstance(threshold, (float, int)):
            msg = 'Please specify a threshold as a float between 0 and 1.'
            raise InputError(msg)

        if not isinstance(starttime, (float, int)):
            msg = 'Please specify a starttime as a float.'
            raise InputError(msg)

        if not isinstance(endtime, (float, int)):
            msg = 'Please specify a endtime as a float.'
            raise InputError(msg)

        if not isinstance(skip, int):
            msg = 'Please specify a skip as an integer of frames you wish to skip (Max = N frames).'
            raise InputError(msg)

        if not isinstance(frames, int):
            msg = 'Please specify a frames as an integer of fixed frames you wish to keep.'
            raise InputError(msg)

    else:
        msg = 'Minimum input for this function: filename as a str.'
        raise InputError(msg)