Read&create sights from a CSV/XRA file.
The CSV file must have the following columns:
- 1
- index of the sights in the image;
- success -- like in OpenFace2 CSV results
- number of sights
- all x values
- all y values
- all z values -- if not None
- image name
Read&create sights from a CSV/XRA file.
The CSV file must have the following columns:
Set the list of sights defined in the given file.
def __init__(self, input_file, csv_separator=';'):
"""Set the list of sights defined in the given file.
:param input_file: (str) Coords from a sppasCoordsImageWriter
:param csv_separator: (char) Optional parameter, ';' character by default
Columns separator in the CSV file
:raises sppasTypeError: If one of the two parameters are not a string object
:raises sppasIOError: If the input file is not found
:raises sppasExtensionReadError: If the extension of the input file is not 'xra' or 'csv'
"""
if not isinstance(input_file, str):
raise sppasTypeError(input_file, 'str')
if not os.path.exists(input_file):
raise sppasIOError(input_file)
self.sights = list()
_, file_extension = os.path.splitext(input_file)
if file_extension.lower() == '.csv':
if not isinstance(csv_separator, str):
raise sppasTypeError(csv_separator, 'str')
self.__load_from_csv(input_file, csv_separator)
elif file_extension.lower() == '.xra':
self.__load_from_xra(input_file)
else:
raise sppasExtensionReadError('Unrecognized extension, expected .csv or .xra.Got {0} instead.'.format(file_extension))
Initialize all sights data from the csv file data loaded.
def __load_from_csv(self, input_file, separator):
"""Initialize all sights data from the csv file data loaded.
:param input_file: (str) Coords from a sppasCoordsImageWriter
:param separator: (char) Columns separator in the CSV file
:raises sppasTypeError: If the csv has a bad value in this data
"""
with codecs.open(input_file, 'r') as csv:
lines = csv.readlines()
if len(lines) == 0:
return
for line in lines:
content = line.split(separator)
if content[2] == '1':
nb_sights = sppasCoords.to_dtype(content[3])
current_sights = sppasSights(nb_sights)
for i in range(3, 3 + nb_sights):
x = sppasCoords.to_dtype(content[i - 3], unsigned=False)
y = sppasCoords.to_dtype(content[i - 3 + nb_sights], unsigned=False)
current_sights.set_sight(i - 3, x, y)
self.sights.append(current_sights)
Initialize all sights data from the xra file data loaded.
def __load_from_xra(self, input_file):
"""Initialize all sights data from the xra file data loaded.
:param input_file: (str) Coords from a sppasCoordsImageWriter
:raises sppasError: If the annotation file doesn't contain any sights tier.
"""
transcription = sppasXRA('HandImageSights')
transcription.read(input_file)
tier = None
for current_tier in transcription:
if current_tier.is_point() is True and current_tier.is_fuzzypoint() is True:
tier = current_tier
break
if tier is None:
raise sppasError('No valid tier in XRA. Cant load sights.')
for annotation in tier:
current_sights = sppasSights(nb=len(annotation.get_labels()))
for label in annotation.get_labels():
tag = label.get_best()
score = label.get_score(tag)
fuzzy_point = tag.get_typed_content()
x, y = fuzzy_point.get_midpoint()
sight_index = label.get_key()[-2:]
try:
sight_index = int(sight_index)
current_sights.set_sight(sight_index, x, y, z=None, score=score)
except ValueError:
logging.error('The key of the sight is incorrect : ' + sight_index)
self.sights.append(current_sights)