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  package org.openimaj.image.model.asm.datasets;
31  
32  import java.io.File;
33  import java.io.IOException;
34  import java.net.URL;
35  
36  import org.apache.commons.io.FileUtils;
37  import org.openimaj.data.DataUtils;
38  import org.openimaj.experiment.annotations.DatasetDescription;
39  import org.openimaj.image.Image;
40  import org.openimaj.io.InputStreamObjectReader;
41  
42  /**
43   * Tim Cootes's sample appearance modelling data
44   * 
45   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
46   */
47  @DatasetDescription(
48  		name = "Tim Cootes's sample appearance modelling data",
49  		description = "The sample data (images, points and connections) that come with Tim Cootes's am_tools software.",
50  		creator = "Tim Cootes",
51  		url = "http://www.isbe.man.ac.uk/~bim/software/am_tools_doc/",
52  		downloadUrls = {
53  				"http://datasets.openimaj.org/am_tools_data.zip"
54  		})
55  public class AMToolsSampleDataset {
56  	private static final String DATA_ZIP = "am_tools_data.zip";
57  	private static final String DATA_DOWNLOAD_URL = "http://datasets.openimaj.org/am_tools_data.zip";
58  
59  	/**
60  	 * Get a dataset of the IMM images and points. If the dataset hasn't been
61  	 * downloaded, it will be fetched automatically and stored in the OpenIMAJ
62  	 * data directory. The images in the dataset are grouped by their class.
63  	 * 
64  	 * @see DataUtils#getDataDirectory()
65  	 * 
66  	 * @param reader
67  	 *            the reader for images (can be <code>null</code> if you only
68  	 *            care about the points)
69  	 * @return the dataset
70  	 * @throws IOException
71  	 *             if an error occurs
72  	 */
73  	public static <IMAGE extends Image<?, IMAGE>> ShapeModelDataset<IMAGE> load(InputStreamObjectReader<IMAGE> reader)
74  			throws IOException
75  	{
76  		final String basePath = downloadAndGetPath();
77  		return ShapeModelDatasets.loadPTSDataset(basePath + "points/", basePath + "images/", basePath
78  				+ "models/face.parts", reader);
79  	}
80  
81  	private static String downloadAndGetPath() throws IOException {
82  		final File dataset = DataUtils.getDataLocation(DATA_ZIP);
83  
84  		if (!(dataset.exists())) {
85  			dataset.getParentFile().mkdirs();
86  			FileUtils.copyURLToFile(new URL(DATA_DOWNLOAD_URL), dataset);
87  		}
88  
89  		return "zip:file:" + dataset.toString() + "!am_tools_data/";
90  	}
91  
92  }