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.vlad; 031 032import java.io.File; 033import java.io.FileFilter; 034import java.io.IOException; 035import java.util.Arrays; 036import java.util.List; 037 038import org.openimaj.feature.local.list.LocalFeatureList; 039import org.openimaj.image.FImage; 040import org.openimaj.image.ImageUtilities; 041import org.openimaj.image.feature.local.engine.DoGSIFTEngine; 042import org.openimaj.image.feature.local.keypoints.Keypoint; 043import org.openimaj.io.IOUtils; 044import org.openimaj.util.function.Operation; 045import org.openimaj.util.parallel.Parallel; 046 047public class MIRFlickrIndexer { 048 public static void main(String[] args) { 049 final List<File> images = Arrays.asList((new 050 File("/Volumes/My Book/Data/ukbench/images/")).listFiles(new FileFilter() { 051 @Override 052 public boolean accept(File pathname) { 053 return pathname.getName().endsWith(".jpg"); 054 } 055 })); 056 057 final File outDir = new File("/Volumes/My Book/Data/ukbench/features/sift-2x"); 058 outDir.mkdirs(); 059 060 Parallel.forEach(images, new Operation<File>() { 061 @Override 062 public void perform(File file) { 063 try { 064 final File out = new File(outDir, file.getName().replace("jpg", "sift")); 065 066 if (out.exists()) 067 return; 068 069 final DoGSIFTEngine engine = new DoGSIFTEngine(); 070 // engine.getOptions().setDoubleInitialImage(false); 071 System.out.println(file); 072 073 final FImage image = ImageUtilities.readF(file); 074 // image = ResizeProcessor.resizeMax(image, 300); 075 076 final LocalFeatureList<Keypoint> keys = engine.findFeatures(image); 077 078 IOUtils.writeBinary(out, keys); 079 } catch (final IOException e) { 080 e.printStackTrace(); 081 } 082 } 083 }); 084 } 085 086 // public static void main(String[] args) throws IOException { 087 // final VLADIndexer indexer = VLADIndexer.read(new 088 // File("/Users/jsh2/vlad-indexer-mirflickr25k-1x.dat")); 089 // 090 // final List<IntObjectPair<float[]>> index = new 091 // ArrayList<IntObjectPair<float[]>>(); 092 // final List<IntObjectPair<float[]>> syncList = 093 // Collections.synchronizedList(index); 094 // 095 // Parallel.forEach(Arrays.asList(new 096 // File("/Volumes/Raid/mirflickr/sift-1x-300px").listFiles()), new 097 // Operation<File>() 098 // { 099 // 100 // @Override 101 // public void perform(File f) { 102 // if (!f.getName().endsWith(".sift")) 103 // return; 104 // 105 // try { 106 // System.out.println(f); 107 // 108 // final int id = Integer.parseInt(f.getName().replace("im", 109 // "").replace(".sift", "")); 110 // 111 // final MemoryLocalFeatureList<Keypoint> keys = 112 // MemoryLocalFeatureList.read(f, Keypoint.class); 113 // final MemoryLocalFeatureList<FloatKeypoint> fkeys = 114 // FloatKeypoint.convert(keys); 115 // 116 // for (final FloatKeypoint k : fkeys) { 117 // HellingerNormaliser.normalise(k.vector, 0); 118 // } 119 // 120 // indexer.index(fkeys, id, syncList); 121 // } catch (final Exception e) { 122 // e.printStackTrace(); 123 // } 124 // } 125 // }); 126 // 127 // IOUtils.writeToFile(index, new 128 // File("/Users/jsh2/Desktop/mirflickr25k.idx")); 129 // } 130}