View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  /**
31   *
32   */
33  package org.openimaj.audio;
34  
35  import java.util.List;
36  import java.util.Set;
37  
38  import org.openimaj.feature.DoubleFV;
39  import org.openimaj.feature.DoubleFVComparison;
40  import org.openimaj.feature.FeatureExtractor;
41  import org.openimaj.feature.IdentityFeatureExtractor;
42  import org.openimaj.ml.annotation.Annotated;
43  import org.openimaj.ml.annotation.IncrementalAnnotator;
44  import org.openimaj.ml.annotation.ScoredAnnotation;
45  import org.openimaj.ml.annotation.basic.KNNAnnotator;
46  import org.openimaj.ml.annotation.bayes.NaiveBayesAnnotator;
47  import org.openimaj.ml.annotation.bayes.NaiveBayesAnnotator.Mode;
48  
49  /**
50   * A classifier/annotator for audio frames. Note that this is a pretty general
51   * annotator that takes {@link DoubleFV}s and String labels. The
52   * {@link DoubleFV}s can be extracted from audio with this class also, as it
53   * also implements FeatureExtractor interface for {@link SampleChunk}s
54   * (returning {@link DoubleFV}s). However, there is no implementation for the
55   * necessary {@link FeatureExtractor#extractFeature(Object)} method, so
56   * subclasses must implement this. The default annotator in use is a general
57   * {@link KNNAnnotator} for {@link DoubleFV}s, however, this can be changed
58   * using {@link #setAnnotator(AudioAnnotatorType)}.
59   * 
60   * @author David Dupplaw (dpd@ecs.soton.ac.uk)
61   * @created 8 Mar 2013
62   */
63  public abstract class AudioAnnotator
64  		extends
65  		IncrementalAnnotator<DoubleFV, String>
66  		implements
67  		FeatureExtractor<DoubleFV, SampleChunk>
68  {
69  	/**
70  	 * An enumeration that allows different trainers to be used and specified on
71  	 * the command-line.
72  	 * 
73  	 * @author David Dupplaw (dpd@ecs.soton.ac.uk)
74  	 * @created 6 Dec 2012
75  	 * @version $Author$, $Revision$, $Date$
76  	 */
77  	public enum AudioAnnotatorType
78  	{
79  		/** KNN Annotator */
80  		KNN {
81  			private KNNAnnotator<DoubleFV, String, DoubleFV> k;
82  
83  			@Override
84  			public IncrementalAnnotator<DoubleFV, String> getAnnotator()
85  			{
86  				if (this.k == null)
87  					this.k = new KNNAnnotator<DoubleFV, String, DoubleFV>(
88  							new IdentityFeatureExtractor<DoubleFV>(),
89  							DoubleFVComparison.EUCLIDEAN);
90  				return this.k;
91  			}
92  		},
93  
94  		/** Naive Bayes annotator */
95  		BAYES {
96  			private IncrementalAnnotator<DoubleFV, String> n;
97  
98  			@Override
99  			public IncrementalAnnotator<DoubleFV, String> getAnnotator()
100 			{
101 				if (this.n == null)
102 					this.n = new NaiveBayesAnnotator<DoubleFV, String>(
103 							new IdentityFeatureExtractor<DoubleFV>(),
104 							Mode.ALL);
105 				return this.n;
106 			}
107 		};
108 
109 		/**
110 		 * Returns a annotator that can train a DoubleFV feature with a specific
111 		 * String label. The annotators will all have
112 		 * {@link IdentityFeatureExtractor} feature extractors so the audio
113 		 * features must be extracted before hand.
114 		 * 
115 		 * @return The annotator
116 		 */
117 		public abstract IncrementalAnnotator<DoubleFV, String> getAnnotator();
118 	}
119 
120 	/** The specfic annotator being used */
121 	private AudioAnnotatorType annotator = AudioAnnotatorType.KNN;
122 
123 	/**
124 	 * {@inheritDoc}
125 	 * 
126 	 * @see org.openimaj.ml.training.IncrementalTrainer#train(java.lang.Object)
127 	 */
128 	@Override
129 	public void train(final Annotated<DoubleFV, String> annotated)
130 	{
131 		this.getAnnotator().getAnnotator().train(annotated);
132 	}
133 
134 	/**
135 	 * {@inheritDoc}
136 	 * 
137 	 * @see org.openimaj.ml.training.IncrementalTrainer#reset()
138 	 */
139 	@Override
140 	public void reset()
141 	{
142 		this.getAnnotator().getAnnotator().reset();
143 	}
144 
145 	/**
146 	 * {@inheritDoc}
147 	 * 
148 	 * @see org.openimaj.ml.annotation.Annotator#getAnnotations()
149 	 */
150 	@Override
151 	public Set<String> getAnnotations()
152 	{
153 		return this.getAnnotator().getAnnotator().getAnnotations();
154 	}
155 
156 	/**
157 	 * {@inheritDoc}
158 	 * 
159 	 * @see org.openimaj.ml.annotation.Annotator#annotate(java.lang.Object)
160 	 */
161 	@Override
162 	public List<ScoredAnnotation<String>> annotate(final DoubleFV object)
163 	{
164 		return this.getAnnotator().getAnnotator().annotate(object);
165 	}
166 
167 	/**
168 	 * Get the annotator type in use.
169 	 * 
170 	 * @return The annotator type
171 	 */
172 	public AudioAnnotatorType getAnnotator()
173 	{
174 		return this.annotator;
175 	}
176 
177 	/**
178 	 * Set the annotator type to use.
179 	 * 
180 	 * @param annotator
181 	 *            The annotator type.
182 	 */
183 	public void setAnnotator(final AudioAnnotatorType annotator)
184 	{
185 		this.annotator = annotator;
186 	}
187 }