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.tools.localfeature.options; 031 032import java.io.File; 033import java.io.FileInputStream; 034import java.io.IOException; 035 036import org.kohsuke.args4j.Option; 037import org.kohsuke.args4j.ProxyOptionHandler; 038import org.openimaj.io.IOUtils; 039import org.openimaj.tools.localfeature.options.LocalFeatureMode.LocalFeatureModeOp; 040 041/** 042 * Options for the Extractor tool 043 * 044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 045 */ 046public class BaseExtractorOptions extends SharedOptions { 047 @Option( 048 name = "--mode", 049 aliases = "-m", 050 required = false, 051 usage = "SIFT keypoint mode.", 052 handler = ProxyOptionHandler.class) 053 private LocalFeatureMode mode = LocalFeatureMode.SIFT; 054 private LocalFeatureModeOp modeOp = LocalFeatureMode.SIFT.getOptions(); 055 056 @Option( 057 name = "--print-time-taken", 058 aliases = "-ptt", 059 required = false, 060 usage = "Print to the standard output the time taken to extract features") 061 boolean printTime = false; 062 063 @Option( 064 name = "--extractor-file", 065 aliases = "-ef", 066 usage = "Serialise the configured feature extractor to the given file") 067 File extractorFile; 068 069 /** 070 * @return the mode 071 */ 072 public LocalFeatureModeOp getMode() { 073 return modeOp; 074 } 075 076 /** 077 * Read the input image as bytes 078 * 079 * @param input 080 * the input location 081 * 082 * @return the input image as bytes 083 * @throws IOException 084 */ 085 public byte[] getInputImage(String input) throws IOException { 086 return getInputImage(new File(input)); 087 } 088 089 /** 090 * Read the input image as bytes 091 * 092 * @param file 093 * the input location 094 * 095 * @return the input image as bytes 096 * @throws IOException 097 */ 098 public byte[] getInputImage(File file) throws IOException { 099 100 if (file.isDirectory()) 101 throw new RuntimeException("Unsupported operation, file " 102 + file.getAbsolutePath() + " is a directory"); 103 if (file.length() > Integer.MAX_VALUE) 104 throw new RuntimeException("Unsupported operation, file " 105 + file.getAbsolutePath() + " is too big"); 106 107 Throwable pending = null; 108 FileInputStream in = null; 109 final byte buffer[] = new byte[(int) file.length()]; 110 try { 111 in = new FileInputStream(file); 112 in.read(buffer); 113 } catch (final Exception e) { 114 pending = new RuntimeException("Exception occured on reading file " 115 + file.getAbsolutePath(), e); 116 } finally { 117 if (in != null) { 118 try { 119 in.close(); 120 } catch (final Exception e) { 121 if (pending == null) { 122 pending = new RuntimeException( 123 "Exception occured on closing file" 124 + file.getAbsolutePath(), e); 125 } 126 } 127 } 128 if (pending != null) { 129 throw new RuntimeException(pending); 130 } 131 } 132 return buffer; 133 } 134 135 /** 136 * @return true if timing information should be printed 137 */ 138 public boolean printTiming() { 139 return this.printTime; 140 } 141 142 /** 143 * Serialise the feature extractor to a file so it can be reused by other 144 * tools or code. 145 * 146 * @throws IOException 147 */ 148 public void serialiseExtractor() throws IOException { 149 if (extractorFile != null) { 150 IOUtils.writeToFile(this.modeOp, extractorFile); 151 } 152 } 153}