SPPAS 4.22

https://sppas.org/

Module sppas.src.resources

Class sppasHandResource

Description

Manage hand files in the resources folder to the cued speech annotation.

A hands set is composing of 9 hand shapes images and sights annotation file (xra or csv). The files are to be in the resources/cuedspeech folder with this specific files name format: - {prefix}{handshapenumber}{postfix}.png - {prefix}{handshapenumber}{postfix}.xra || {prefix}{handshape_number}{postfix}.csv

The prefix and postfix mustn't content number (0-9) character.

Constructor

The constructor of the sppasHandResource class.

View Source
def __init__(self):
    """The constructor of the sppasHandResource class.

    """
    self.__hands = dict()

Public functions

get_hand_sets_identifiers

Get all hands sets identifiers loaded.

The identifiers are the prefix of the hands sets when they are loaded.

Returns
  • (list[str]) All identifiers
View Source
def get_hand_sets_identifiers(self):
    """Get all hands sets identifiers loaded.

        The identifiers are the prefix of the hands sets when they are loaded.

        :return: (list[str]) All identifiers
        """
    return list(self.__hands.keys())

get_hand_images

Get all hands shapes images path of a hands set linked with the given prefix.

Parameters
  • identifier: (str) The identifier (prefix) of the hands set
Returns
  • (list[str] or None) The path of the hands shapes images None if no hands set is associated with the given prefix
View Source
def get_hand_images(self, identifier):
    """Get all hands shapes images path of a hands set linked with the given prefix.

        :param identifier: (str) The identifier (prefix) of the hands set

        :return: (list[str] or None) The path of the hands shapes images
                                     None if no hands set is associated with the given prefix

        """
    if self.__hands.get(identifier) is None:
        return None
    return self.__hands.get(identifier)[0]

get_shape

Get hand image path associated with the given prefix and index.

Parameters
  • identifier: (str) The identifier (prefix) of the hands set
  • index: (int) The index of the shape
Raises
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of shapes
Returns
  • (sppasImage or None) The path of the hand shape None if no hands set is associated with the given prefix
View Source
def get_shape(self, identifier, index):
    """Get hand image path associated with the given prefix and index.

        :param identifier: (str) The identifier (prefix) of the hands set
        :param index: (int) The index of the shape

        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of shapes

        :return: (sppasImage or None) The path of the hand shape
                                      None if no hands set is associated with the given prefix

        """
    if index < 0:
        raise NegativeValueError(index)
    if index > NB_SHAPES:
        raise IndexRangeException(index, 0, NB_SHAPES)
    if self.__hands.get(identifier) is None:
        return None
    return self.__hands.get(identifier)[0][index]

get_hands_sights

Get all hands shapes sights path linked with the given prefix.

Parameters
  • identifier: (str) The identifier (prefix) of the hands set
Returns
  • (list[sppasSights] or None) The path of the hands shapes sights None if no hands set is associated with the given prefix
View Source
def get_hands_sights(self, identifier):
    """Get all hands shapes sights path linked with the given prefix.

        :param identifier: (str) The identifier (prefix) of the hands set

        :return: (list[sppasSights] or None) The path of the hands shapes sights
                                             None if no hands set is associated with the given prefix

        """
    if self.__hands.get(identifier) is None:
        return None
    return self.__hands.get(identifier)[1]

get_sights

Get hand image path associated with the given prefix and index.

Parameters
  • identifier: (str) The identifier (prefix) of the hands set
  • index: (int) The index of the shape
Raises
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of shapes
Returns
  • (sppasImage or None) The path of the hand shape None if no hands set is associated with the given prefix
View Source
def get_sights(self, identifier, index):
    """Get hand image path associated with the given prefix and index.

        :param identifier: (str) The identifier (prefix) of the hands set
        :param index: (int) The index of the shape

        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of shapes

        :return: (sppasImage or None) The path of the hand shape
                                      None if no hands set is associated with the given prefix

        """
    if index < 0:
        raise NegativeValueError(index)
    if index > NB_SHAPES:
        raise IndexRangeException(index, 0, NB_SHAPES)
    if self.__hands.get(identifier) is None:
        return None
    return self.__hands.get(identifier)[1][index]

clear_hands_resources

Clear all hands sets loaded.

View Source
def clear_hands_resources(self):
    """Clear all hands sets loaded."""
    self.__hands.clear()

automatic_loading

Search and load all hands sets in the resources' folder.

Raises
  • sppasEnableFeatureError: If the resources/cuedspeech folder doesn't exist
  • sppasIOError: If a hands set found has a missing file (image or annotation)
View Source
def automatic_loading(self):
    """Search and load all hands sets in the resources' folder.

        :raises sppasEnableFeatureError: If the resources/cuedspeech folder doesn't exist
        :raises sppasIOError: If a hands set found has a missing file (image or annotation)

        """
    if not os.path.exists(os.path.join(paths.resources, 'cuedspeech')):
        raise sppasEnableFeatureError('cuedspeech')
    files = os.listdir(os.path.join(paths.resources, 'cuedspeech'))
    for file in files:
        if not re.search('^[^0-9]+_[0-8][^0-9]*\\..+$', file):
            continue
        file_root = FileRoot.root(file)
        prefix = file_root[:-2]
        postfix = FileRoot.pattern(file)
        if prefix in self.__hands.keys():
            continue
        try:
            self.load_hand_set(prefix, postfix=postfix)
        except sppasIOError as error:
            logging.warning("The hands set '{0}' is missing a file.\nLog Trace : {1}".format(prefix, error))

load_hand_set

Load a hands set from the given prefix and postfix in the resources' folder.

Parameters
  • prefix: (str) The prefix (identifier) of the hands set
  • postfix: (str) The postfix of the hands set
Raises
  • sppasIOError: If the image or the annotation files are not found
View Source
def load_hand_set(self, prefix, postfix='-hands'):
    """Load a hands set from the given prefix and postfix in the resources' folder.

        :param prefix: (str) The prefix (identifier) of the hands set
        :param postfix: (str) The postfix of the hands set

        :raises sppasIOError: If the image or the annotation files are not found

        """
    image_list = list()
    sights_list = list()
    for i in range(NB_SHAPES):
        image_filepath, sights_filepath = self.__check_shape_files(prefix, postfix, i)
        if image_filepath == '':
            raise sppasIOError(image_filepath)
        if sights_filepath == '':
            raise sppasIOError(sights_filepath)
        image_list.append(image_filepath)
        sights_list.append(sights_filepath)
    self.__hands[prefix] = (image_list, sights_list)

Protected functions

__check_shape_files

Return the file path of the image and annotation file (or empty string).

Parameters
  • prefix: (str) The prefix (identifier) of the hands set
  • postfix: (str) The postfix of the hands set
  • index: (int) The index of the shape files
Returns
  • (tuple[str, str]) The file path of the image and sights annotation Empty string if the doesn't exist
View Source
def __check_shape_files(self, prefix, postfix, index):
    """Return the file path of the image and annotation file (or empty string).

        :param prefix: (str) The prefix (identifier) of the hands set
        :param postfix: (str) The postfix of the hands set
        :param index: (int) The index of the shape files

        :return: (tuple[str, str]) The file path of the image and sights annotation
                                  Empty string if the doesn't exist

        """
    img_file_path = ''
    annotation_file_path = ''
    files_name = '{0}_{1}{2}'
    files_path1 = os.path.join(paths.resources, 'cuedspeech', files_name.format(prefix, index, ''))
    files_path2 = os.path.join(paths.resources, 'cuedspeech', files_name.format(prefix, index, postfix))
    for extension in image_extensions:
        if os.path.exists(files_path1 + extension):
            img_file_path = files_path1 + extension
            break
    if os.path.exists(files_path2 + '.xra'):
        annotation_file_path = files_path2 + '.xra'
    elif os.path.exists(files_path2 + '.csv'):
        annotation_file_path = files_path2 + '.csv'
    return (img_file_path, annotation_file_path)

Overloads

__len__

Return the number of hands sets loaded.

View Source
def __len__(self):
    """Return the number of hands sets loaded."""
    return len(self.__hands)