SPPAS 4.22

https://sppas.org/

Module sppas.src.imgdata

Class sppasSights

Description

Data structure to store sights.

This class is storing nb sights; each sight is made of 4 values: - x: coordinate on the x-axis, initialized to 0 - y: coordinate on the y-axis, initialized to 0 - z: an optional coordinate on the z-axis, initialized to None - an optional confidence score, initialized to None

Notice that each of the sight parameter is stored into a list of 'nb' values, instead of storing a single list of 'nb' lists of values, because:

  • 2 lists of 'nb' int and 1 of float = [x1,x2,...] [y1,y2,...] [s1,s2,...] 364 + 26824 + 168*24 = 5088
  • 1 list of 'nb' lists of 2 int and 1 float: [[x1,y1,s1], [x2,y2,s2]...] 64 + 6864 + 26824 + 168*24 = 9312

Constructor

Constructor of the sppasSights class.

Parameters
  • nb: (int) Number of expected sights
Raises
  • sppasTypeError: If the number parameter is not an integer
View Source
def __init__(self, nb=68):
    """Constructor of the sppasSights class.

    :param nb: (int) Number of expected sights

    :raises sppasTypeError: If the number parameter is not an integer

    """
    self.__nb = sppasCoords.to_dtype(nb, int, unsigned=True)
    self.__x = [0] * nb
    self.__y = [0] * nb
    self.__z = None
    self.__confidence = None

Public functions

get_x

Return the list of x values.

Returns
  • (list[int]) The list that contains all x values
View Source
def get_x(self):
    """Return the list of x values.

        :return: (list[int]) The list that contains all x values

        """
    return self.__x.copy()

get_y

Return the list of y values.

Returns
  • (list[int]) The list that contains all y values
View Source
def get_y(self):
    """ Return the list of y values.

        :return: (list[int]) The list that contains all y values

        """
    return self.__y.copy()

get_z

Return the list of z values or None.

Returns
  • (list[int] or None) The list that contains all z values Or None if 3D axis is not set
View Source
def get_z(self):
    """Return the list of z values or None.

        :return: (list[int] or None) The list that contains all z values
                                     Or None if 3D axis is not set

        """
    if self.__z is None:
        return None
    return self.__z.copy()

get_score

Return the list of confidence score values or None.

Returns
  • (list[float] or None) The list that contains all score values Or None if scores are not set
View Source
def get_score(self):
    """Return the list of confidence score values or None.

        :return: (list[float] or None) The list that contains all score values
                                       Or None if scores are not set

        """
    if self.__confidence is None:
        return None
    return self.__confidence.copy()

x

Return the x-axe value of the sight at the given index.

Parameters
  • index: (int) Index of the sight
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
Returns
  • (int) The x value
View Source
def x(self, index):
    """Return the x-axe value of the sight at the given index.

        :param index: (int) Index of the sight

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        :return: (int) The x value

        """
    checked_index = self.check_index(index)
    return self.__x[checked_index]

y

Return the y-axe value of the sight at the given index.

Parameters
  • index: (int) Index of the sight
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
Returns
  • (int) The y value
View Source
def y(self, index):
    """Return the y-axe value of the sight at the given index.

        :param index: (int) Index of the sight

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        :return: (int) The y value

        """
    checked_index = self.check_index(index)
    return self.__y[checked_index]

z

Return the z-axe value of the sight at the given index or None.

Parameters
  • index: (int) Index of the sight
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
Returns
  • (int or None) The z value or None if 3D axis is not set
View Source
def z(self, index):
    """Return the z-axe value of the sight at the given index or None.

        :param index: (int) Index of the sight

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        :return: (int or None) The z value or None if 3D axis is not set

        """
    if self.__z is None:
        return None
    checked_index = self.check_index(index)
    return self.__z[checked_index]

score

Return the score of the sight at the given index or None.

Parameters
  • index: (int) Index of the sight
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
Returns
  • (float or None) The score value or None if scores are not set
View Source
def score(self, index):
    """Return the score of the sight at the given index or None.

        :param index: (int) Index of the sight

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        :return: (float or None) The score value or None if scores are not set

        """
    if self.__confidence is None:
        return None
    checked_index = self.check_index(index)
    return self.__confidence[checked_index]

get_sight

Return the (x, y, z, s) of the given sight.

Parameters
  • index: (int) Index of the sight
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
Returns
  • (tuple[int, int, int, float]) The data of the sight associated with the given index
View Source
def get_sight(self, index):
    """Return the (x, y, z, s) of the given sight.

        :param index: (int) Index of the sight

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        :return: (tuple[int, int, int, float]) The data of the sight associated with the given index

        """
    checked_index = self.check_index(index)
    return (self.__x[checked_index], self.__y[checked_index], self.z(checked_index), self.score(checked_index))

set_sight

Set the sight at the given index.

Parameters
  • index: (int) Index of the sight
  • x: (int) pixel position on the x-axis (width)
  • y: (int) pixel position on the y-axis (height)
  • z: (int or None) pixel position on the z axis
  • score: (float or None) An optional confidence score
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
View Source
def set_sight(self, index, x, y, z=None, score=None):
    """Set the sight at the given index.

        :param index: (int) Index of the sight
        :param x: (int) pixel position on the x-axis (width)
        :param y: (int) pixel position on the y-axis (height)
        :param z: (int or None) pixel position on the z axis
        :param score: (float or None) An optional confidence score

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        """
    checked_index = self.check_index(index)
    checked_x = sppasCoords.to_dtype(x, int, unsigned=True)
    checked_y = sppasCoords.to_dtype(y, int, unsigned=True)
    self.__x[checked_index] = checked_x
    self.__y[checked_index] = checked_y
    self.set_sight_z(checked_index, z)
    self.set_sight_score(checked_index, score)

set_sight_z

Set a z value to the sight at the given index.

Parameters
  • index: (int) Index of the sight
  • z: (int or None) An optional z value
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
View Source
def set_sight_z(self, index, z):
    """Set a z value to the sight at the given index.

        :param index: (int) Index of the sight
        :param z: (int or None) An optional z value

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        """
    checked_index = self.check_index(index)
    if z is None:
        if self.__z is not None:
            self.__z[checked_index] = None
    else:
        checked_z = sppasCoords.to_dtype(z, int, unsigned=False)
        if self.__z is None:
            self.__z = [None] * self.__nb
        self.__z[checked_index] = checked_z

set_sight_score

Set a score to the sight at the given index.

Parameters
  • index: (int) Index of the sight
  • score: (float or None) An optional confidence score
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
View Source
def set_sight_score(self, index, score):
    """Set a score to the sight at the given index.

        :param index: (int) Index of the sight
        :param score: (float or None) An optional confidence score

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        """
    checked_index = self.check_index(index)
    if score is None:
        if self.__confidence is not None:
            self.__confidence[checked_index] = None
    else:
        checked_sight = sppasCoords.to_dtype(score, float, unsigned=False)
        if self.__confidence is None:
            self.__confidence = [None] * self.__nb
        self.__confidence[checked_index] = checked_sight

reset

Reset all values.

View Source
def reset(self):
    """Reset all values."""
    self.__x = [0] * self.__nb
    self.__y = [0] * self.__nb
    self.__z = None
    self.__confidence = None

copy

Return a deep copy of the current sppasSights instance.

Returns
  • (sppasSights) The copy of the instance
View Source
def copy(self):
    """Return a deep copy of the current sppasSights instance.

        :return: (sppasSights) The copy of the instance

        """
    copied = sppasSights(nb=self.__nb)
    for i in range(self.__nb):
        x, y, z, s = self.get_sight(i)
        copied.set_sight(i, x, y, z, s)
    return copied

check_index

Raise an exception if the given index is not valid.

Parameters
  • value: (int) The index to check
Raises
  • sppasTypeError: If the index is not an integer
  • NegativeValueError: If the index is negative (Impossible for an index !)
  • IndexRangeException: If the index is superior of the number of sights
Returns
  • (int) The value given
View Source
def check_index(self, value):
    """Raise an exception if the given index is not valid.

        :param value: (int) The index to check

        :raises sppasTypeError: If the index is not an integer
        :raises NegativeValueError: If the index is negative (Impossible for an index !)
        :raises IndexRangeException: If the index is superior of the number of sights

        :return: (int) The value given

        """
    if not isinstance(value, int):
        raise sppasTypeError(value, 'int')
    if value < 0:
        raise NegativeValueError(value)
    if self.__nb < value:
        raise IndexRangeException(value, 0, self.__nb)
    return value

get_mean_score

Return the mean score or None.

Returns
  • (float or None) The mean score or None if no score is set
View Source
def get_mean_score(self):
    """Return the mean score or None.

        :return: (float or None) The mean score or None if no score is set

        """
    if self.__confidence is None:
        return None
    values = [v for v in self.__confidence if v is not None]
    if len(values) == 0:
        return None
    return sum(values) / len(values)

intermediate

Return the sights with the intermediate positions.

Parameters
  • other: (sppasSights) The other instance of sights
Returns
  • (sppasSights) The computed intermediate sights
View Source
def intermediate(self, other):
    """Return the sights with the intermediate positions.

        :param other: (sppasSights) The other instance of sights

        :return: (sppasSights) The computed intermediate sights

        """
    if not isinstance(other, sppasSights):
        raise sppasTypeError(other, 'sppasSights')
    if len(other) != self.__nb:
        raise sppasValueError(self.__nb, len(other))
    intermediate_sights = sppasSights(self.__nb)
    i = 0
    for s1, s2 in zip(self, other):
        intermediate_x = s1[0] + (s2[0] - s1[0]) // 2
        intermediate_y = s1[1] + (s2[1] - s1[1]) // 2
        intermediate_z = None
        if s1[2] is not None and s2[2] is not None:
            intermediate_z = s1[2] + (s2[2] - s1[2]) // 2
        intermediate_score = None
        if s1[3] is not None and s2[3] is not None:
            intermediate_score = (s1[3] + s2[3]) / 2.0
        intermediate_sights.set_sight(i, intermediate_x, intermediate_y, intermediate_z, intermediate_score)
        i += 1
    return intermediate_sights

Overloads

__str__

View Source
def __str__(self):
    string_builder = 'sppasSights['
    for i in range(self.__nb):
        string_builder += '({0},{1}'.format(self.__x[i], self.__y[i])
        if self.__z is not None:
            string_builder += ',{0}'.format(self.__z[i])
        if self.__confidence is not None and self.__confidence[i] is not None:
            string_builder += ': {0}), '.format(self.__confidence[i])
        string_builder += ']'
    return string_builder

__repr__

View Source
def __repr__(self):
    return self.__class__.__name__

__format__

View Source
def __format__(self, fmt):
    return str(self).__format__(fmt)

__len__

Return the number of sights.

View Source
def __len__(self):
    """Return the number of sights."""
    return self.__nb

__iter__

Browse the current sights.

View Source
def __iter__(self):
    """Browse the current sights."""
    for i in range(self.__nb):
        yield self.get_sight(i)

__getitem__

View Source
def __getitem__(self, item):
    if isinstance(item, slice):
        return [self.get_sight(ii) for ii in range(*item.indices(len(self)))]
    return self.get_sight(item)

__contains__

Return true if value in sights -- score is ignored.

Parameters
  • other: A list/tuple of(x,y,...)
View Source
def __contains__(self, other):
    """Return true if value in sights -- score is ignored.

        :param other: A list/tuple of (x,y,...)

        """
    if isinstance(other, (list, tuple)) is True:
        if len(other) < 2:
            return False
        c = sppasSights(1)
        c.set_sight(0, other[0], other[1])
        if len(other) > 2:
            if isinstance(other[2], int):
                c.set_sight_z(0, other[2])
        other = c
    if isinstance(other, sppasSights) is False:
        return False
    for i in range(self.__nb):
        if self.__x[i] == other.x(0) and self.__y[i] == other.y(0) and (self.z(i) == other.z(0)):
            return True
    return False