Ejemplo n.º 1
0
    def feature_repository(cls, filename=None):
        from dcase_util.processors import ProcessingChain

        if filename is None:
            filename = cls.audio_filename()

        fs = 44100
        chain = ProcessingChain([{
            'processor_name':
            'dcase_util.processors.MonoAudioReadingProcessor',
            'init_parameters': {
                'fs': fs
            }
        }, {
            'processor_name':
            'dcase_util.processors.RepositoryFeatureExtractorProcessor',
            'init_parameters': {
                'parameters': {
                    'mel': {},
                    'mfcc': {},
                    'mfcc_delta': {},
                    'mfcc_acceleration': {},
                    'zcr': {},
                    'rmse': {},
                    'centroid': {},
                }
            }
        }])
        return chain.process(filename=filename)
Ejemplo n.º 2
0
    def process(self, data=None, store_processing_chain=False, **kwargs):
        """Extract features

        Parameters
        ----------
        data : AudioContainer
            Audio data to extract features

        store_processing_chain : bool
            Store processing chain to data container returned
            Default value False

        Returns
        -------
        FeatureContainer

        """

        from dcase_util.containers import FeatureContainer, AudioContainer

        if isinstance(data, AudioContainer):
            if store_processing_chain:
                if hasattr(data, 'processing_chain') and data.processing_chain.chain_item_exists(
                        processor_name='dcase_util.processors.' + self.__class__.__name__):
                    # Current processor is already in the processing chain, get that
                    processing_chain_item = data.processing_chain.chain_item(
                        processor_name='dcase_util.processors.' + self.__class__.__name__
                    )

                else:
                    # Create a new processing chain item
                    processing_chain_item = self.get_processing_chain_item()

                processing_chain_item.update({
                    'process_parameters': kwargs
                })

                if hasattr(data, 'processing_chain'):
                    data.processing_chain.push_processor(**processing_chain_item)
                    processing_chain = data.processing_chain

                else:
                    processing_chain = ProcessingChain().push_processor(**processing_chain_item)

            else:
                processing_chain = None

            return FeatureContainer(
                data=self.extract(y=data.get_focused()),
                time_resolution=self.hop_length_seconds,
                processing_chain=processing_chain
            )

        else:
            message = '{name}: Wrong input data type, type required [{input_type}].'.format(
                name=self.__class__.__name__,
                input_type=self.input_type)

            self.logger.exception(message)
            raise ValueError(message)
Ejemplo n.º 3
0
    def __init__(self, *args, **kwargs):
        self.processing_chain = kwargs.get('processing_chain',
                                           ProcessingChain())
        if not hasattr(self, 'input_type'):
            self.input_type = None

        if not hasattr(self, 'output_type'):
            self.output_type = None
Ejemplo n.º 4
0
    def process(self, data=None, store_processing_chain=False, **kwargs):
        """Encode metadata

        Parameters
        ----------
        data : MetaDataContainer
            Meta data to encode.

        store_processing_chain : bool
            Store processing chain to data container returned
            Default value False

        Returns
        -------
        BinaryMatrixContainer

        """

        from dcase_util.containers import MetaDataContainer

        if isinstance(data, MetaDataContainer):
            # Do processing
            self.encoder.encode(metadata_container=data)

            if store_processing_chain:
                # Get processing chain item
                processing_chain_item = self.get_processing_chain_item()

                processing_chain_item.update({'process_parameters': kwargs})

                # Create processing chain to be stored in the container, and push chain item into it
                if hasattr(data, 'processing_chain'):
                    data.processing_chain.push_processor(
                        **processing_chain_item)
                    processing_chain = data.processing_chain

                else:
                    processing_chain = ProcessingChain().push_processor(
                        **processing_chain_item)

            else:
                processing_chain = None

            from dcase_util.containers import BinaryMatrix2DContainer
            container = BinaryMatrix2DContainer(
                data=self.encoder.data,
                label_list=self.encoder.label_list,
                time_resolution=self.encoder.time_resolution,
                processing_chain=processing_chain)

            return container

        else:
            message = '{name}: Wrong input data type, type required [{input_type}].'.format(
                name=self.__class__.__name__, input_type=self.input_type)

            self.logger.exception(message)
            raise ValueError(message)
Ejemplo n.º 5
0
    def process(self,
                data=None,
                filename=None,
                focus_start_samples=None,
                focus_stop_samples=None,
                focus_duration_samples=None,
                focus_start_seconds=None,
                focus_stop_seconds=None,
                focus_duration_seconds=None,
                focus_channel=None,
                store_processing_chain=False,
                **kwargs):
        """Audio reading

        Parameters
        ----------
        data :

        filename : str
            Filename

        focus_start_samples : int
            Sample index of focus segment start

        focus_stop_samples : int
            Sample index of focus segment stop

        focus_duration_samples : int
            Sample count of focus segment

        focus_start_seconds : float
            Time stamp (in seconds) of focus segment start

        focus_stop_seconds : float
            Time stamp (in seconds) of focus segment stop

        focus_duration_seconds : float
            Duration (in seconds) of focus segment

        focus_channel : int or str
            Audio channel id or name to focus. In case of stereo signal, valid channel labels to select
            single channel are 'L', 'R', 'left', and 'right' or 0, 1, and to get mixed down version
            of all channels 'mixdown'.

        store_processing_chain : bool
            Store processing chain to data container returned
            Default value False

        Returns
        -------
        AudioContainer

        """

        if data is None and self.input_type == ProcessingChainItemType.NONE:
            audio_container = AudioContainer(**self.init_parameters)

            if filename:
                audio_container.load(filename=filename,
                                     mono=self.init_parameters.get('mono'))

            # Set focus segment and channel
            audio_container.set_focus(start=focus_start_samples,
                                      stop=focus_stop_samples,
                                      duration=focus_duration_samples,
                                      start_seconds=focus_start_seconds,
                                      stop_seconds=focus_stop_seconds,
                                      duration_seconds=focus_duration_seconds,
                                      channel=focus_channel)

            if store_processing_chain:
                processing_chain_item = self.get_processing_chain_item()

                if 'process_parameters' not in processing_chain_item:
                    processing_chain_item['process_parameters'] = {}

                processing_chain_item['process_parameters'][
                    'filename'] = filename

                processing_chain_item['process_parameters'][
                    'focus_start_samples'] = focus_start_samples
                processing_chain_item['process_parameters'][
                    'focus_stop_samples'] = focus_stop_samples
                processing_chain_item['process_parameters'][
                    'focus_duration_samples'] = focus_duration_samples

                processing_chain_item['process_parameters'][
                    'focus_start_seconds'] = focus_start_seconds
                processing_chain_item['process_parameters'][
                    'focus_stop_seconds'] = focus_stop_seconds
                processing_chain_item['process_parameters'][
                    'focus_duration_seconds'] = focus_duration_seconds

                processing_chain_item['process_parameters'][
                    'focus_channel'] = focus_channel

                # Push chain item into processing chain stored in the container

                # Create processing chain to be stored in the container, and push chain item into it
                if hasattr(audio_container, 'processing_chain'):
                    audio_container.processing_chain.push_processor(
                        **processing_chain_item)

                else:
                    audio_container.processing_chain = ProcessingChain(
                    ).push_processor(**processing_chain_item)

            return audio_container

        else:
            message = '{name}: Wrong input data type, type required [{input_type}].'.format(
                name=self.__class__.__name__, input_type=self.input_type)

            self.logger.exception(message)
            raise ValueError(message)
Ejemplo n.º 6
0
    def process(self, data=None, store_processing_chain=False, **kwargs):
        """Extract features

        Parameters
        ----------
        data : AudioContainer
            Audio data to extract features

        store_processing_chain : bool
            Store processing chain to data container returned
            Default value False

        Returns
        -------
        FeatureRepository

        """

        from dcase_util.containers import FeatureRepository, AudioContainer

        if isinstance(data, AudioContainer):
            if store_processing_chain:
                if hasattr(data, 'processing_chain'
                           ) and data.processing_chain.chain_item_exists(
                               processor_name='dcase_util.processors.' +
                               self.__class__.__name__):
                    # Current processor is already in the processing chain, get that
                    processing_chain_item = data.processing_chain.chain_item(
                        processor_name='dcase_util.processors.' +
                        self.__class__.__name__)

                else:
                    # Create a new processing chain item
                    processing_chain_item = self.get_processing_chain_item()

                # Update current processing parameters into chain item
                processing_chain_item.update({'process_parameters': kwargs})

                # Create processing chain to be stored in the container, and push chain item into it
                if hasattr(data, 'processing_chain'):
                    data.processing_chain.push_processor(
                        **processing_chain_item)
                    processing_chain = data.processing_chain

                else:
                    processing_chain = ProcessingChain().push_processor(
                        **processing_chain_item)

            else:
                processing_chain = None

            # Create repository container
            repository = FeatureRepository(processing_chain=processing_chain)

            # Make local copy of data
            current_data = copy.deepcopy(data)

            if data.streams == 1:
                # We have single channel audio input
                for label, parameters in iteritems(self.parameters):
                    if label in self.label_to_class:
                        # Get processor
                        processor = self.label_to_class[label](**parameters)

                        # Reset processing chain
                        current_data.processing_chain = ProcessingChain()

                        # Extract features
                        extracted = processor.process(data=current_data)

                        repository.set_container(container=extracted,
                                                 label=label)

                    else:
                        message = '{name}: Unknown label [{label}], no corresponding class found.'.format(
                            name=self.__class__.__name__, label=label)

                        self.logger.exception(message)
                        raise AssertionError(message)

            elif data.streams > 1:
                # We have multi-channel audio input
                for stream_id in range(0, data.streams):
                    for label, parameters in iteritems(self.parameters):
                        if label in self.label_to_class:
                            # Get processor
                            processor = self.label_to_class[label](
                                **parameters)

                            # Reset processing chain
                            current_data.processing_chain = ProcessingChain()

                            # Set focus to the current stream
                            current_data.focus_channel = stream_id

                            # Extract features
                            extracted = processor.process(data=current_data)

                            # Add extracted features to the repository
                            repository.set_container(container=extracted,
                                                     label=label,
                                                     stream_id=stream_id)

                        else:
                            message = '{name}: Unknown label [{label}], no corresponding class found.'.format(
                                name=self.__class__.__name__, label=label)

                            self.logger.exception(message)
                            raise AssertionError(message)

            return repository
Ejemplo n.º 7
0
    def process(self,
                data=None,
                focus_field=None,
                length_frames=None,
                length_seconds=None,
                store_processing_chain=False,
                **kwargs):
        """Encode metadata

        Parameters
        ----------
        data : MetaDataContainer
            Meta data to encode.

        focus_field : str
            Field from the meta data item to be used in encoding. If None, one given as parameter for
            class constructor is used.

        length_frames : int
            Length of encoded segment in frames. If None, one given as parameter for class constructor is used.

        length_seconds : float > 0.0
            Length of encoded segment in seconds. If None, one given as parameter for class constructor is used.

        store_processing_chain : bool
            Store processing chain to data container returned
            Default value False

        Returns
        -------
        BinaryMatrixContainer

        """

        from dcase_util.containers import MetaDataContainer

        if focus_field is None:
            focus_field = self.focus_field

        if isinstance(data, MetaDataContainer):
            if length_frames is None and length_seconds is not None:
                length_frames = self._length_to_frames(length_seconds)

            if length_frames is None:
                length_frames = self.length_frames

            if len(data) > 0:
                label_list = data[0].get(focus_field)
                if isinstance(label_list, str):
                    label_list = [label_list]

            # Do processing
            self.encode(label_list=label_list, length_frames=length_frames)

            if store_processing_chain:
                if hasattr(data, 'processing_chain'
                           ) and data.processing_chain.chain_item_exists(
                               processor_name='dcase_util.processors.' +
                               self.__class__.__name__):
                    # Current processor is already in the processing chain, get that
                    processing_chain_item = data.processing_chain.chain_item(
                        processor_name='dcase_util.processors.' +
                        self.__class__.__name__)

                else:
                    # Create a new processing chain item based on current processor class
                    processing_chain_item = self.get_processing_chain_item()

                if 'process_parameters' not in processing_chain_item:
                    processing_chain_item['process_parameters'] = {}

                processing_chain_item['process_parameters'][
                    'focus_field'] = focus_field
                processing_chain_item['process_parameters'][
                    'length_frames'] = length_frames

                # Create processing chain to be stored in the container, and push chain item into it
                if hasattr(data, 'processing_chain'):
                    data.processing_chain.push_processor(
                        **processing_chain_item)
                    processing_chain = data.processing_chain

                else:
                    processing_chain = ProcessingChain().push_processor(
                        **processing_chain_item)

            else:
                processing_chain = None

            from dcase_util.containers import BinaryMatrix2DContainer
            container = BinaryMatrix2DContainer(
                data=self.data,
                label_list=self.label_list,
                time_resolution=self.time_resolution,
                processing_chain=processing_chain)

            return container

        else:
            message = '{name}: Wrong input data type, type required [{input_type}].'.format(
                name=self.__class__.__name__, input_type=self.input_type)

            self.logger.exception(message)
            raise ValueError(message)
Ejemplo n.º 8
0
    def process(self,
                data=None,
                label=None,
                focus_field=None,
                length_frames=None,
                length_seconds=None,
                store_processing_chain=False,
                **kwargs):
        """Encode metadata

        Parameters
        ----------
        data : MetaDataContainer
            Meta data to encode. Give data in either through meta data container or directly with label parameter.

        label : str
            Class label to be hot

        focus_field : str
            Field from the meta data item to be used in encoding. If None, one given as parameter for class
            constructor is used.

        length_frames : int
            Length of encoded segment in frames. If None, one given as parameter for class constructor is used.

        length_seconds : float > 0.0
            Length of encoded segment in seconds. If None, one given as parameter for class constructor is used.

        store_processing_chain : bool
            Store processing chain to data container returned
            Default value False

        Returns
        -------
        BinaryMatrixContainer

        """

        if data is None and label is None:
            message = '{name}: Give data or label parameter.'.format(
                name=self.__class__.__name__)
            self.logger.exception(message)
            raise ValueError(message)

        from dcase_util.containers import MetaDataContainer

        if data is not None and not isinstance(data, MetaDataContainer):
            message = '{name}: Wrong input data type, type required [{input_type}].'.format(
                name=self.__class__.__name__, input_type=self.input_type)

            self.logger.exception(message)
            raise ValueError(message)

        if focus_field is None:
            focus_field = self.focus_field

        if data is not None and len(data) > 0 and label is None:
            label = data[0].get(focus_field)

        # Do processing
        self.encoder.encode(label=label,
                            length_frames=length_frames,
                            length_seconds=length_seconds)

        if store_processing_chain:
            # Get processing chain item
            processing_chain_item = self.get_processing_chain_item()

            if 'process_parameters' not in processing_chain_item:
                processing_chain_item['process_parameters'] = {}

            processing_chain_item['process_parameters'][
                'focus_field'] = focus_field
            processing_chain_item['process_parameters'][
                'length_frames'] = length_frames

            # Create processing chain to be stored in the container, and push chain item into it
            if hasattr(data, 'processing_chain'):
                data.processing_chain.push_processor(**processing_chain_item)
                processing_chain = data.processing_chain

            else:
                processing_chain = ProcessingChain().push_processor(
                    **processing_chain_item)
        else:
            processing_chain = None

        from dcase_util.containers import BinaryMatrix2DContainer
        container = BinaryMatrix2DContainer(
            data=self.encoder.data,
            label_list=self.encoder.label_list,
            time_resolution=self.encoder.time_resolution,
            processing_chain=processing_chain)

        return container