Auto-CS 2.0

https://sourceforge.net/projects/autocs/

Module CuedSpeech.whowtag.hands

Class sppasHandsSet

Description

Data structure to load and store all hands.

Constructor

Store an image of a hand for each given consonant.

Parameters
  • cue_rules: (CuedSpeechKeys) Cued speech rules Optional parameter, new instance of CuedSpeechKeys class by default
Raises
  • sppasTypeError: If the parameter is not an instance of CuedSpeechKeys
View Source
def __init__(self, cue_rules: CuedSpeechKeys=CuedSpeechKeys()):
    """Store an image of a hand for each given consonant.

    :param cue_rules: (CuedSpeechKeys) Cued speech rules
                      Optional parameter, new instance of CuedSpeechKeys class by default
    :raises: sppasTypeError: If the parameter is not an instance of CuedSpeechKeys

    """
    if isinstance(cue_rules, CuedSpeechKeys) is False:
        raise sppasTypeError(cue_rules, 'CuedSpeechKeys')
    self.__cued = cue_rules
    self.__hands_properties = dict()
    self.__hands_filter = sppasHandFilters()

Public functions

set_cue_rules

Set the CuedSpeechKeys used to tag the video.

Parameters
  • cue_rules: (CuedSpeechKeys) The instance of the cuedSpeechKeys to set
View Source
def set_cue_rules(self, cue_rules: CuedSpeechKeys):
    """Set the CuedSpeechKeys used to tag the video.

        :param cue_rules: (CuedSpeechKeys) The instance of the cuedSpeechKeys to set

        """
    self.__cued = cue_rules

image

Return a deep copy of the hand image matching with the given code.

Return None if no image associated with the given code.

Parameters
  • shape_code: (str) Hand shape vowel code
Returns
  • (sppasImage or None) The hand image corresponding to the code, or None if the code is wrong
View Source
def image(self, shape_code: str):
    """Return a deep copy of the hand image matching with the given code.

        Return None if no image associated with the given code.

        :param shape_code: (str) Hand shape vowel code
        :return: (sppasImage or None) The hand image corresponding to the code, or None if the code is wrong

        """
    if shape_code in self.__hands_properties.keys():
        return self.__hands_properties[shape_code].image().copy()
    else:
        return None

target_coords

Return target coords of the hand image matching with the given code.

Return None if no image associated with the given code.

Parameters
  • shape_code: (str) Hand shape vowel code
Returns
  • ((int, int) or None) The target coords of the hand image, or None if the code is wrong
View Source
def target_coords(self, shape_code: str):
    """Return target coords of the hand image matching with the given code.

        Return None if no image associated with the given code.

        :param shape_code: (str) Hand shape vowel code
        :return: ((int, int) or None) The target coords of the hand image, or None if the code is wrong

        """
    if shape_code in self.__hands_properties:
        return self.__hands_properties[shape_code].target_coords()
    else:
        return None

get_sight

Return a sight of a hand with the given shape code and index.

Return None if no image associated with the given code.

Parameters
  • shape_code: (str) Hand shape vowel code
  • index: (int) The index of the sight that we want
Raises
  • IntervalRangeException: If the index is negative or out of bounds
Returns
  • (tuple[int, int] or None) The coords of the sight, or None if the code is wrong
View Source
def get_sight(self, shape_code: str, index: int):
    """Return a sight of a hand with the given shape code and index.

        Return None if no image associated with the given code.

        :param shape_code: (str) Hand shape vowel code
        :param index: (int) The index of the sight that we want
        :raises: IntervalRangeException: If the index is negative or out of bounds
        :return: (tuple[int, int] or None) The coords of the sight, or None if the code is wrong

        """
    if shape_code in self.__hands_properties:
        return self.__hands_properties[shape_code].get_sight(index)
    else:
        return None

angle

Return the angle of the hand image matching with the given code.

Return 0 if no image associated with the given code.

Parameters
  • shape_code: (str) Hand shape vowel code
Returns
  • (int) The angle of the hand image, Or 0 if the code is wrong
View Source
def angle(self, shape_code: str) -> int:
    """Return the angle of the hand image matching with the given code.

        Return 0 if no image associated with the given code.

        :param shape_code: (str) Hand shape vowel code
        :return: (int) The angle of the hand image, Or 0 if the code is wrong

        """
    if shape_code in self.__hands_properties:
        return self.__hands_properties[shape_code].angle()
    return 0

angle_to_s0

Return the angle of the given sight compared to S0-S9 axis.

Parameters
  • shape_code: (str) Hand shape vowel code
  • sight_index: (int) The index of the sight
Returns
  • (int) the computed angle
Raises
  • IntervalRangeException: If the index is negative or out of bounds
View Source
def angle_to_s0(self, shape_code: str, sight_index: int=0) -> int:
    """Return the angle of the given sight compared to S0-S9 axis.

        :param shape_code: (str) Hand shape vowel code
        :param sight_index: (int) The index of the sight
        :return: (int) the computed angle
        :raises: IntervalRangeException: If the index is negative or out of bounds

        """
    if shape_code in self.__hands_properties:
        return self.__hands_properties[shape_code].get_angle_with_s0(sight_index)
    return 0

distance

Return the distance of the hand image matching with the given code.

Return 0 if no image associated with the given code.

Parameters
  • shape_code: (str) Shape code name
Returns
  • (int) The distance of the current hand, or 0 if the code is wrong
View Source
def distance(self, shape_code: str) -> int:
    """Return the distance of the hand image matching with the given code.

        Return 0 if no image associated with the given code.

        :param shape_code: (str) Shape code name
        :return: (int) The distance of the current hand, or 0 if the code is wrong

        """
    if shape_code in self.__hands_properties:
        return self.__hands_properties[shape_code].distance()
    return 0

distance_to_s0

Get the distance between s0 and a sight of the hand.

Parameters
  • shape_code: (str) Shape code name
  • sight_index: (int) The index of the sight
Raises
  • IntervalRangeException: If the index is negative or out of bounds
Returns
  • (int) the computed distance
View Source
def distance_to_s0(self, shape_code: str, sight_index: int=0) -> int:
    """Get the distance between s0 and a sight of the hand.

        :param shape_code: (str) Shape code name
        :param sight_index: (int) The index of the sight
        :raises: IntervalRangeException: If the index is negative or out of bounds
        :return: (int) the computed distance

        """
    if shape_code in self.__hands_properties:
        return self.__hands_properties[shape_code].get_distance_with_s0(sight_index)
    return 0

load

Load the hand images matching with the given prefix.

Parameters
  • prefix: (str) Prefix in hand image filenames
Raises
  • sppasIOError: If the files with the given prefix and pattern are not found
Returns
  • (int) The number of hands loaded
View Source
def load(self, prefix: str) -> int:
    """Load the hand images matching with the given prefix.

        :param prefix: (str) Prefix in hand image filenames
        :raises: sppasIOError: If the files with the given prefix and pattern are not found
        :return: (int) The number of hands loaded

        """
    hands_sets_manager = sppasHandResource()
    hands_sets_manager.load_hand_set(prefix)
    self.__load_hand_pictures(hands_sets_manager.get_hand_images(prefix), hands_sets_manager.get_hands_sights(prefix))
    return len(self.__hands_properties)

apply_hands_filter

Apply a filter on all hands images loaded.

Parameters
  • filter_name: (str) The name of the filter to apply
Raises
  • sppasValueError: Unknown filter name
View Source
def apply_hands_filter(self, filter_name: str) -> None:
    """Apply a filter on all hands images loaded.

        :param filter_name: (str) The name of the filter to apply
        :raises: sppasValueError: Unknown filter name

        """
    if hasattr(self.__hands_filter, filter_name):
        for (key, item) in self.__hands_properties.items():
            item.set_image(getattr(self.__hands_filter, filter_name)(item, key))
    else:
        raise sppasValueError('filter_name', filter_name)

Protected functions

__load_hand_pictures

Return the dictionary of pictures filepath for the hand shapes.

The number of images and sights should be the same and equal to the number of shapes for the given language.

Notice that it is supposed that the loaded images are ranked in the same order as the shapes, i.e., shape "0" is represented in the first image, etc.

Parameters
  • hands_images: (list) Prefix in hand image filenames
  • hands_sights: (list) Pattern in hand image filenames
Raises
  • sppasError: If the number of images is different of sights
  • sppasIOError: If a file to read is not found
  • sppasError: If invalid number of images or sights.
Returns
  • (dict[str, SppasHandProperties]) The pictures filepath dictionary
View Source
def __load_hand_pictures(self, hands_images: list, hands_sights: list) -> None:
    """Return the dictionary of pictures filepath for the hand shapes.

        The number of images and sights should be the same and equal to the
        number of shapes for the given language.

        Notice that it is supposed that the loaded images are ranked
        in the same order as the shapes, i.e., shape "0" is
        represented in the first image, etc.

        :param hands_images: (list) Prefix in hand image filenames
        :param hands_sights: (list) Pattern in hand image filenames

        :raises: sppasError: If the number of images is different of sights
        :raises: sppasIOError: If a file to read is not found
        :raises: sppasError: If invalid number of images or sights.
        :return: (dict[str, SppasHandProperties]) The pictures filepath dictionary

        """
    _shapes = self.__cued.get_consonants_codes()
    if len(_shapes) != len(hands_images):
        raise sppasError(f'Invalid number of hand images. Expected {len(_shapes)}. Got {len(hands_images)} instead.')
    if len(_shapes) != len(hands_sights):
        raise sppasError(f'Invalid number of hand sights. Expected {len(_shapes)}. Got {len(hands_sights)} instead.')
    for (i, paths) in enumerate(zip(hands_images, hands_sights)):
        if os.path.exists(paths[0]) is False:
            logging.warning(f"Can't find hand picture file {paths[0]}.")
            break
        hand_img = sppasImage(filename=paths[0])
        if os.path.exists(paths[1]) is False:
            logging.warning(f"Can't find hand sights file {paths[1]}.")
            break
        try:
            data = sppasImageSightsReader(paths[1])
            if len(data.sights) != 1:
                raise sppasError(f'Invalid file sights {paths[1]}. ({len(data.sights)} != 1)')
            current_sights = data.sights[0]
            if current_sights.get_sight(0) is None or current_sights.get_sight(9) is None:
                raise sppasIOError(paths[1])
            target_index = self.__cued.get_shape_target(_shapes[i])
            self.__hands_properties[_shapes[i]] = sppasHandProperties(hand_img, current_sights, target_index)
        except sppasIOError as e:
            logging.error(f'Error while reading hand sights: {e}')

Overloads

__len__

Return the number of hand shapes loaded.

View Source
def __len__(self):
    """Return the number of hand shapes loaded."""
    return len(self.__hands_properties)