Diagnose if files are appropriate.
A set of methods to check if files are valid for SPPAS automatic annotations. Each method returns a status and a message depending on the fact that the given file is matching the requirements.
Diagnose if files are appropriate.
A set of methods to check if files are valid for SPPAS automatic annotations. Each method returns a status and a message depending on the fact that the given file is matching the requirements.
Check file of any type: audio or image or annotated file.
The extension of the filename is used to know the type of the file.
@staticmethod
def check_file(filename):
"""Check file of any type: audio or image or annotated file.
The extension of the filename is used to know the type of the file.
:param filename: (str) name of the input file to diagnose.
:return: tuple(status identifier, message)
"""
if os.path.exists(filename) is False:
return (annots.error, info(1008, 'annotations'))
ext = os.path.splitext(filename)[1]
if ext.lower() in audioopy_ext:
return sppasDiagnosis.check_audio_file(filename)
if ext.lower() in anndata_ext:
return sppasDiagnosis.check_trs_file(filename)
if ext.lower() in imgdata_ext:
return sppasDiagnosis.check_img_file(filename)
if ext.lower() in videodata_ext:
return sppasDiagnosis.check_video_file(filename)
message = info(1006, 'annotations') + info(1020, 'annotations').format(extension=ext)
return (annots.error, message)
Check an audio file.
Are verified:
@staticmethod
def check_audio_file(filename):
"""Check an audio file.
Are verified:
1. the format of the file (error);
2. the number of channels (error);
3. the sample width (error or warning);
4. the framerate (error or warning;
5. the filename (warning).
:param filename: (str) name of the input file
:return: tuple with (status identifier, message)
"""
if AUDIOOPY is False:
status = annots.error
message = "Audio file support is not available. 'Base' feature must be installed"
return (status, message)
status = annots.ok
message = ''
try:
audio = audioopy.aio.open(filename)
fm = audio.get_framerate()
sp = audio.get_sampwidth() * 8
nc = audio.get_nchannels()
audio.close()
except UnicodeDecodeError:
message = info(1004, 'annotations') + info(1026, 'annotations').format(encoding=sg.__encoding__)
return (annots.error, message)
except Exception as e:
message = info(1004, 'annotations') + str(e)
return (annots.error, message)
if nc > sppasDiagnosis.EXPECTED_CHANNELS:
status = annots.error
message += info(1010, 'annotations').format(number=nc)
if sp < sppasDiagnosis.EXPECTED_SAMPLE_WIDTH * 8:
status = annots.error
message += info(1012, 'annotations').format(sampwidth=sp)
if fm < sppasDiagnosis.EXPECTED_FRAME_RATE:
status = annots.error
message += info(1014, 'annotations').format(framerate=fm)
if status != annots.error:
if sp > sppasDiagnosis.EXPECTED_SAMPLE_WIDTH * 8:
status = annots.warning
message += info(1016, 'annotations').format(sampwidth=sp)
if fm > sppasDiagnosis.EXPECTED_FRAME_RATE:
status = annots.warning
message += info(1018, 'annotations').format(framerate=fm)
if all((ord(x) < 128 for x in filename)) is False:
status = annots.warning
message += info(1022, 'annotations')
if ' ' in filename:
status = annots.warning
message += info(1024, 'annotations')
if status == annots.error:
message = info(1004, 'annotations') + message
elif status == annots.warning:
message = info(1002, 'annotations') + message
else:
message = info(1000, 'annotations')
return (status, message)
Check an annotated file.
Are verified:
@staticmethod
def check_trs_file(filename):
"""Check an annotated file.
Are verified:
1. the format of the file (error);
2. the file encoding (error);
3. the filename (warning).
:param filename: (string) name of the input file
:returns: tuple with (status identifier, message)
"""
status = annots.ok
message = info(1000, 'annotations')
try:
f = codecs.open(filename, 'r', sg.__encoding__)
f.close()
except UnicodeDecodeError:
message = info(1004, 'annotations') + info(1026, 'annotations').format(encoding=sg.__encoding__)
return (annots.error, message)
except Exception as e:
message = info(1004, 'annotations') + str(e)
return (annots.error, message)
if all((ord(x) < 128 for x in filename)) is False:
message = info(1002, 'annotations') + info(1022, 'annotations')
return (annots.warning, message)
if ' ' in filename:
message = info(1002, 'annotations') + info(1024, 'annotations')
return (annots.warning, message)
return (status, message)
Check an image file.
Are verified:
opencv can open the file
@staticmethod
def check_img_file(filename):
"""Check an image file.
Are verified:
opencv can open the file
:param filename: (string) name of the input file
:returns: tuple with (status identifier, message)
"""
status = annots.ok
message = info(1000, 'annotations')
return (status, message)
Check a video file.
Are verified:
opencv can open the file
@staticmethod
def check_video_file(filename):
"""Check a video file.
Are verified:
opencv can open the file
:param filename: (string) name of the input file
:returns: tuple with (status identifier, message)
"""
status = annots.ok
message = info(1000, 'annotations')
return (status, message)