SPPAS 4.22

https://sppas.org/

Module sppas.src.imgdata

Class sppasCoordsReader

Description

Read&create coords from a CSV/XRA file.

The CSV file must have the following columns:

  • index of the coords in the image;
  • confidence;
  • x; y; w; h;
  • image name

Constructor

Set the list of coords defined in the given file.

Parameters
  • input_file: (str) coords from a sppasCoordsImageWriter
  • csv_separator: (char) Columns separator in the CSV file
View Source
def __init__(self, input_file, csv_separator=';'):
    """Set the list of coords defined in the given file.

    :param input_file: (str) coords from a sppasCoordsImageWriter
    :param csv_separator: (char) Columns separator in the CSV file

    """
    self.coords = list()
    self.names = list()
    fn, fe = os.path.splitext(input_file)
    if fe.lower() == '.csv':
        self.__load_from_csv(input_file, csv_separator)
    elif fe.lower() == '.xra':
        self.__load_from_xra(input_file)
    else:
        raise Exception('Unrecognized extension, expected .csv or .xra.Got {} instead.'.format(fe))

Protected functions

__load_from_csv

View Source
def __load_from_csv(self, input_file, separator):
    with codecs.open(input_file, 'r') as csv:
        lines = csv.readlines()
    if len(lines) > 0:
        for line in lines:
            content = line.split(separator)
            if len(content) > 5:
                coord = sppasCoords(int(content[2]), int(content[3]), int(content[4]), int(content[5]), float(content[1]))
                self.coords.append(coord)
            if len(content) > 6:
                self.names.append(content[6])
            else:
                self.names.append(content[0])

__load_from_xra

View Source
def __load_from_xra(self, input_file):
    trs = sppasXRA('ImageCoordinates')
    trs.read(input_file)
    if len(trs) == 1:
        tier = trs[0]
    else:
        tier = trs.find(sppasCoordsImageWriter().get_xra_tiername())
    if tier is None:
        raise Exception('Invalid tier in XRA. Cant load coordinates.')
    for ann in tier:
        for label in ann.get_labels():
            for tag, score in label:
                fuzzy_rect = tag.get_typed_content()
                x, y, w, h = fuzzy_rect.get_midpoint()
                coord = sppasCoords(x, y, w, h, score)
                self.coords.append(coord)
                media_name = ann.get_meta('image_name', 'unk')
                self.names.append(media_name)