public byte[] getByteArrayRepresentation() { byte[] result = new byte[correlogram.length * correlogram[0].length * 4 + 5]; for (int i = 0; i < correlogram.length; i++) { System.arraycopy( SerializationUtils.toByteArray(correlogram[i]), 0, result, i * correlogram[i].length * 4, correlogram[i].length * 4); } System.arraycopy(SerializationUtils.toBytes(numBins), 0, result, result.length - 5, 4); result[result.length - 1] = (byte) distanceSet.length; return result; }
public void setByteArrayRepresentation(byte[] in) { byte[] numBinsBytes = new byte[4]; numBinsBytes[0] = in[in.length - 5]; numBinsBytes[1] = in[in.length - 4]; numBinsBytes[2] = in[in.length - 3]; numBinsBytes[3] = in[in.length - 2]; int maxDistance = (int) in[in.length - 1]; numBins = SerializationUtils.toInt(numBinsBytes); correlogram = new float[numBins][maxDistance]; float[] temp = SerializationUtils.toFloatArray(in); for (int i = 0; i < correlogram.length; i++) { System.arraycopy(temp, i * maxDistance, correlogram[i], 0, maxDistance); } distanceSet = new int[maxDistance]; for (int i = 0; i < distanceSet.length; i++) { distanceSet[i] = i + 1; } }
/** * Reads data from a file and writes it to an index. * * @param indexWriter the index to write to. * @param inputFile the input data for the process. * @throws IOException * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException */ private void readFile(IndexWriter indexWriter, File inputFile) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { BufferedInputStream in = new BufferedInputStream(new FileInputStream(inputFile)); byte[] tempInt = new byte[4]; int tmp, tmpFeature, count = 0; byte[] temp = new byte[100 * 1024]; // read file hashFunctionsFileName length: while (in.read(tempInt, 0, 4) > 0) { Document d = new Document(); tmp = SerializationUtils.toInt(tempInt); // read file hashFunctionsFileName: in.read(temp, 0, tmp); String filename = new String(temp, 0, tmp); // normalize Filename to full path. filename = inputFile .getCanonicalPath() .substring(0, inputFile.getCanonicalPath().lastIndexOf(inputFile.getName())) + filename; d.add(new StringField(DocumentBuilder.FIELD_NAME_IDENTIFIER, filename, Field.Store.YES)); // System.out.print(filename); while ((tmpFeature = in.read()) < 255) { // System.out.print(", " + tmpFeature); LireFeature f = (LireFeature) Class.forName(Extractor.features[tmpFeature]).newInstance(); // byte[] length ... in.read(tempInt, 0, 4); tmp = SerializationUtils.toInt(tempInt); // read feature byte[] in.read(temp, 0, tmp); f.setByteArrayRepresentation(temp, 0, tmp); addToDocument(f, d, Extractor.featureFieldNames[tmpFeature]); // d.add(new StoredField(Extractor.featureFieldNames[tmpFeature], // f.getByteArrayRepresentation())); } if (run == 2) indexWriter.addDocument(d); docCount++; // if (count%1000==0) System.out.print('.'); // if (count%10000==0) System.out.println(" " + count); } in.close(); }
protected void addToDocument(GlobalFeature feature, Document document, String featureFieldName) { // This is for debugging the image features. // try { //// System.out.println(feature.getClass().getName() + " " + // document.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]); // LireFeature f1 = feature.getClass().newInstance(); // f1.extract(ImageIO.read(new // File(document.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0]))); // float distance = feature.getDistance(f1); // if (distance != 0) { // System.out.println("Extracted:" + // java.util.Arrays.toString(f1.getFeatureVector()).replaceAll("\\.0,", "") + "\n" + // "Data :" + // java.util.Arrays.toString(feature.getFeatureVector()).replaceAll("\\.0,", "") + "\n" + // "Problem with " + f1.getClass().getName() + " at file " + // document.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0] + ", distance=" + distance // ); //// System.out.println("Problem with " + f1.getClass().getName() + " at file " + // document.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0] + ", distance=" + distance); // } // } catch (Exception e) { // e.printStackTrace(); // // } if (feature.getClass().getCanonicalName().equals(featureClass.getCanonicalName())) { // generate hashes here: // int[] hashes = // LocalitySensitiveHashing.generateHashes(feature.getFeatureVector()); int[] hashes = BitSampling.generateHashes(feature.getFeatureVector()); // System.out.println(Arrays.toString(hashes)); // store hashes in index as terms document.add( new TextField( featureFieldName + "_hash", SerializationUtils.arrayToString(hashes), Field.Store.YES)); // add the specific feature document.add(new StoredField(featureFieldName, feature.getByteArrayRepresentation())); } // add the specific feature // document.add(new StoredField(featureFieldName, feature.getByteArrayRepresentation())); }
@Override public void setByteArrayRepresentation(byte[] in, int offset, int length) { histogram = SerializationUtils.toDoubleArray(in, offset, length); }
public void setByteArrayRepresentation(byte[] in) { histogram = SerializationUtils.toDoubleArray(in); }
public byte[] getByteArrayRepresentation() { return SerializationUtils.toByteArray(histogram); }