001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.image.objectdetection.datasets;
031
032import java.util.AbstractList;
033import java.util.ArrayList;
034import java.util.List;
035import java.util.Random;
036
037import org.apache.commons.vfs2.FileSystemException;
038import org.openimaj.data.DataUtils;
039import org.openimaj.data.dataset.GroupedDataset;
040import org.openimaj.data.dataset.ListBackedDataset;
041import org.openimaj.data.dataset.ListDataset;
042import org.openimaj.data.dataset.MapBackedDataset;
043import org.openimaj.data.dataset.VFSListDataset;
044import org.openimaj.experiment.annotations.DatasetDescription;
045import org.openimaj.image.FImage;
046import org.openimaj.image.Image;
047import org.openimaj.image.ImageUtilities;
048import org.openimaj.io.InputStreamObjectReader;
049import org.openimaj.math.geometry.shape.Rectangle;
050
051@DatasetDescription(
052                name = "INRIAPerson",
053                description = "Images of upright people in images and video. " +
054                                "The dataset is divided in two formats: (a) original " +
055                                "images with corresponding annotation files, and " +
056                                "(b) positive images in normalized 64x128 pixel format " +
057                                "(as used in the CVPR paper) with original negative images",
058                creator = "Navneet Dalal",
059                url = "http://pascal.inrialpes.fr/data/human/",
060                downloadUrls = {
061                                "http://datasets.openimaj.org/INRIAPerson.zip",
062                })
063public class INRIAPersonDataset {
064        static class NegEx {
065                int id;
066                Rectangle r;
067        }
068
069        public static <IMAGE extends Image<?, IMAGE>> ListDataset<IMAGE> getNegativeTrainingImages(
070                        InputStreamObjectReader<IMAGE> reader) throws FileSystemException
071        {
072                final VFSListDataset<IMAGE> images = new VFSListDataset<IMAGE>(DataUtils.getDataLocation(
073                                "INRIAPerson/train_64x128_H96/neg")
074                                .toString(), reader);
075
076                return images;
077        }
078
079        public static <IMAGE extends Image<?, IMAGE>> ListDataset<IMAGE> getPositiveTrainingImages(
080                        InputStreamObjectReader<IMAGE> reader) throws FileSystemException
081        {
082                final VFSListDataset<IMAGE> images = new VFSListDataset<IMAGE>(DataUtils.getDataLocation(
083                                "INRIAPerson/train_64x128_H96/pos")
084                                .toString(), reader);
085
086                return images;
087        }
088
089        public static <IMAGE extends Image<?, IMAGE>> ListDataset<IMAGE> generateNegativeExamples(int numSamplesPerImage,
090                        int width, int height, long seed, InputStreamObjectReader<IMAGE> reader) throws FileSystemException
091        {
092                final Random rng = new Random(seed);
093                final ListDataset<IMAGE> images = getNegativeTrainingImages(reader);
094
095                final List<NegEx> data = new ArrayList<NegEx>();
096                for (int i = 0; i < images.size(); i++) {
097                        final IMAGE image = images.getInstance(i);
098                        final int imWidth = image.getWidth();
099                        final int imHeight = image.getHeight();
100
101                        for (int j = 0; j < numSamplesPerImage; j++) {
102                                final NegEx ex = new NegEx();
103                                ex.id = i;
104                                ex.r = generateRandomRect(rng, imWidth, imHeight, width, height);
105                                data.add(ex);
106                        }
107                }
108
109                return new ListBackedDataset<IMAGE>(new AbstractList<IMAGE>() {
110                        int lastId = -1;
111                        IMAGE lastImage;
112
113                        @Override
114                        public IMAGE get(int index) {
115                                final NegEx ex = data.get(index);
116
117                                final IMAGE image;
118                                if (ex.id != lastId) {
119                                        lastImage = images.get(ex.id);
120                                        lastId = ex.id;
121                                }
122                                image = lastImage;
123
124                                return image.extractROI(ex.r);
125                        }
126
127                        @Override
128                        public int size() {
129                                return data.size();
130                        }
131                });
132        }
133
134        private static Rectangle generateRandomRect(Random rng, int imWidth, int imHeight, int width, int height) {
135                final int maxx = imWidth - width;
136                final int maxy = imHeight - height;
137
138                final int x = rng.nextInt(maxx);
139                final int y = rng.nextInt(maxy);
140
141                return new Rectangle(x, y, width, height);
142        }
143
144        public static GroupedDataset<Boolean, ListDataset<FImage>, FImage> getTrainingData() throws FileSystemException {
145                final MapBackedDataset<Boolean, ListDataset<FImage>, FImage> ds = new MapBackedDataset<Boolean, ListDataset<FImage>, FImage>();
146
147                ds.put(true, getPositiveTrainingImages(ImageUtilities.FIMAGE_READER));
148                ds.put(false, generateNegativeExamples(10, 64, 128, 0L, ImageUtilities.FIMAGE_READER));
149
150                return ds;
151        }
152}