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.demos.sandbox.asm;
031
032import java.io.BufferedReader;
033import java.io.File;
034import java.io.FileReader;
035import java.io.IOException;
036import java.util.List;
037
038import org.openimaj.data.dataset.ListBackedDataset;
039import org.openimaj.image.FImage;
040import org.openimaj.image.ImageUtilities;
041import org.openimaj.image.model.asm.datasets.ShapeModelDataset;
042import org.openimaj.math.geometry.point.Point2dImpl;
043import org.openimaj.math.geometry.point.PointList;
044import org.openimaj.math.geometry.point.PointListConnections;
045import org.openimaj.util.pair.IndependentPair;
046
047public class AMPTSDataset extends ListBackedDataset<IndependentPair<PointList, FImage>>
048                implements
049                ShapeModelDataset<FImage>
050{
051        PointListConnections connections;
052
053        public AMPTSDataset(String[] filenames, File ptsDir, File imgDir, File connFile) throws IOException {
054                for (final String name : filenames) {
055                        final FImage img = ImageUtilities.readF(new File(imgDir, name + ".jpg"));
056                        final PointList pts = readAMPTSPts(new File(ptsDir, name + ".pts"));
057                        data.add(new IndependentPair<PointList, FImage>(pts, img));
058                }
059
060                connections = readAMPTSConnections(connFile);
061        }
062
063        public AMPTSDataset(File ptsDir, File imgDir, File connFile) throws IOException {
064                this(new String[] {
065                                "107_0764", "107_0766", "107_0779", "107_0780", "107_0781", "107_0782", "107_0784",
066                                "107_0785", "107_0786", "107_0787", "107_0788", "107_0789", "107_0790", "107_0791", "107_0792" },
067                                ptsDir, imgDir, connFile);
068        }
069
070        private PointListConnections readAMPTSConnections(File connFile) throws IOException {
071                final BufferedReader br = new BufferedReader(new FileReader(connFile));
072                final PointListConnections plc = new PointListConnections();
073
074                String line;
075                while ((line = br.readLine()) != null) {
076                        if (!line.trim().startsWith("indices"))
077                                continue;
078
079                        final String[] data = line.trim().replace("indices(", "").replace(")", "").split(",");
080                        final boolean isOpen = (br.readLine().contains("open_boundary"));
081
082                        int prev = Integer.parseInt(data[0]);
083                        for (int i = 1; i < data.length; i++) {
084                                final int next = Integer.parseInt(data[i]);
085                                plc.addConnection(prev, next);
086                                prev = next;
087                        }
088
089                        if (!isOpen) {
090                                plc.addConnection(Integer.parseInt(data[data.length - 1]), Integer.parseInt(data[0]));
091                        }
092                }
093
094                br.close();
095
096                return plc;
097        }
098
099        private PointList readAMPTSPts(File file) throws IOException {
100                final PointList pl = new PointList();
101                final BufferedReader br = new BufferedReader(new FileReader(file));
102
103                br.readLine();
104                br.readLine();
105                br.readLine();
106
107                String line;
108                while ((line = br.readLine()) != null) {
109                        if (!line.startsWith("}") && line.trim().length() > 0) {
110                                final String[] parts = line.split("\\s+");
111
112                                final float x = Float.parseFloat(parts[0].trim());
113                                final float y = Float.parseFloat(parts[1].trim());
114
115                                pl.points.add(new Point2dImpl(x, y));
116                        }
117                }
118                br.close();
119
120                return pl;
121        }
122
123        @Override
124        public PointListConnections getConnections() {
125                return connections;
126        }
127
128        @Override
129        public List<PointList> getPointLists() {
130                return IndependentPair.getFirst(this);
131        }
132
133        @Override
134        public List<FImage> getImages() {
135                return IndependentPair.getSecond(this);
136        }
137}