https://sppas.org/

SPPAS 4.16

Module sppas.src.videodata

Class sppasCoordsVideoReader

Description

Read&create list of coords from a CSV or XRA file.

The CSV file must have the following columns:

  • frame number (ignored)
  • the index of the coords -- int
  • timestamp (ignored)
  • confidence -- float
  • success -- int (0=failed, 1=success)
  • buffer number (ignored)
  • x, y, w, h -- int
  • index in the buffer (ignored)

Constructor

Set the list of coords defined in the given file.

Parameters

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

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

    """
    self.coords = 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
def __load_from_csv(self, input_file, separator):
    prev = -1
    with codecs.open(input_file, 'r') as csv:
        lines = csv.readlines()
    if len(lines) > 0:
        for line in lines:
            columns = line.split(separator)
            if int(columns[0]) != prev:
                self.coords.append(list())
                prev = int(columns[0])
            if int(columns[4]) == 1 and len(columns) > 8:
                coord = sppasCoords(int(columns[5]), int(columns[6]), int(columns[7]), int(columns[8]), float(columns[3]))
                self.coords[len(self.coords) - 1].append(coord)
__load_from_xra
def __load_from_xra(self, input_file):
    trs = sppasXRA('VideoCoordinates')
    trs.read(input_file)
    if len(trs) == 1:
        tier = trs[0]
    else:
        tier = trs.find(sppasCoordsVideoWriter().get_xra_tiername())
    if tier is None:
        raise Exception('No valid tier in XRA: not found. Cant load coordinates.')
    media = tier.get_media()
    if media is None:
        raise Exception('Invalid tier in XRA: no media. Cant load coordinates.')
    fps = media.get_meta('fps', None)
    if fps is None:
        raise Exception('Invalid media: no fps metadata. Cant load coordinates.')
    fps = float(fps)
    image_idx = 0
    for ann in tier:
        frame_idx = ann.get_meta('frame_index', None)
        if frame_idx is None:
            loc = ann.get_location().get_highest_localization()
            start_time = loc.get_midpoint() - loc.get_radius()
            frame_idx = round(start_time * fps)
        else:
            frame_idx = int(frame_idx)
        for i in range(image_idx, frame_idx + 1):
            self.coords.append(list())
        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[frame_idx].append(coord)
        image_idx = frame_idx + 1