Example #1
0
    def __init__(self, fname, dataset=None, **kwargs):

        kwargs.setdefault('input_parser', audio.raw)

        if kwargs.get('input_parser') is None:
            raise ValueError("input_parser must be set")

        if kwargs.get('label_parser') is None:
            raise ValueError("label_parser must be set")

        with codecs.open(fname, 'r', encoding='utf8') as f:
            ld = json.load(f)

        data = utils.ld2dl(ld)

        if dataset:
            inputs = np.array(
                [d['audio'] for d in data if d['dataset'] == dataset])
            labels = np.array(
                [d['label'] for d in data if d['dataset'] == dataset])
        else:
            inputs = np.array(data['audio'])
            labels = np.array(data['label'])

        super(JSONIterator, self).__init__(inputs, labels, **kwargs)

        self.durations = np.array(data['duration'])
Example #2
0
    def __init__(self, fname, dataset=None, **kwargs):

        self._logger = logging.getLogger('%s.%s' % (__name__,
                                                    self.__class__.__name__))

        kwargs.setdefault('input_parser', audio.raw)

        if kwargs.get('input_parser') is None:
            raise ValueError("input_parser must be set")

        if kwargs.get('label_parser') is None:
            raise ValueError("label_parser must be set")

        with codecs.open(fname, 'r', encoding='utf8') as f:
            ld = json.load(f)

        data = utils.ld2dl(ld)

        if dataset and 'dataset' not in data:
            self._logger.warning('No dataset key found. Falling back to None')
            dataset = None

        if dataset:
            inputs = np.array([i for i, d in zip(
                data['input'], data['dataset']) if d == dataset])
            labels = np.array([l for l, d in zip(
                data['label'], data['dataset']) if d == dataset])
        else:
            inputs = np.array(data['input'])
            labels = np.array(data['label'])

        super(JSONIterator, self).__init__(inputs, labels, **kwargs)

        self.durations = np.array(data['duration'])
Example #3
0
    def to_json(self, fname=None):
        ''' Parse the entire dataset to a list of dictionary containin at least
        two keys:
            `input`: path to audio file
            `duration`: length of the audio
            `label`: transcription of the audio
        '''
        fname = fname or os.path.join(self.default_output_dir, 'data.json')

        if os.path.exists(fname) and override:
            os.remove(fname)

        if not os.path.isdir(os.path.split(fname)[0]):
            safe_mkdirs(os.path.split(fname)[0])

        data = self._to_ld()

        with codecs.open(fname, 'w', encoding='utf8') as f:
            json.dump(data, f)

        self._logger.info(self._report(ld2dl(data)))

        return fname