org.openimaj.io.IOUtils.writeBinary()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中org.openimaj.io.IOUtils.writeBinary()方法的一些代码示例,展示了IOUtils.writeBinary()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.writeBinary()方法的具体详情如下:
包路径:org.openimaj.io.IOUtils
类名称:IOUtils
方法名:writeBinary

IOUtils.writeBinary介绍

[英]Writeable object is written to the output stream in binary format. Firstly the binaryHeader is written then the object is handed the output stream to write it's content.
[中]可写对象以二进制格式写入输出流。首先写入binaryHeader,然后将输出流交给对象以写入其内容。

代码示例

代码示例来源:origin: openimaj/openimaj

/**
 * Convenience function for serializing a writeable object as a byte array.
 * Calls {@link IOUtils#writeBinary(OutputStream, WriteableBinary)} on a
 * {@link ByteArrayOutputStream} then calls
 * {@link ByteArrayOutputStream#toByteArray()}
 * 
 * @param object
 * @return serialised object
 * @throws IOException
 */
public static byte[] serialize(WriteableBinary object) throws IOException {
  final ByteArrayOutputStream stream = new ByteArrayOutputStream();
  IOUtils.writeBinary(stream, object);
  return stream.toByteArray();
}

代码示例来源:origin: openimaj/openimaj

private void replaceSequenceFileWithCluster(String sequenceFile, ByteCentroidsResult cluster) throws IOException {
  final Path p = new Path(sequenceFile);
  final FileSystem fs = HadoopFastKMeansOptions.getFileSystem(p.toUri());
  fs.delete(p, true); // Delete the sequence file of this name
  FSDataOutputStream stream = null;
  try {
    stream = fs.create(p);
    IOUtils.writeBinary(stream, cluster); // Write the cluster
  } finally {
    if (stream != null)
      stream.close();
  }
}

代码示例来源:origin: openimaj/openimaj

/**
 * Write an object to a file fully. The object will be saved with class
 * information so that it can be automatically re-instantiated using
 * {@link #read(File)} without needing to know the actual type.
 * 
 * @param <T>
 *            instance type expected
 * @param f
 *            the file
 * @param object
 *            the object to write
 * @throws IOException
 *             problem reading file
 */
public static <T extends WriteableBinary> void writeBinaryFull(File f, T object) throws IOException {
  IOUtils.writeBinary(f, new ObjectWrapper(object));
}

代码示例来源:origin: openimaj/openimaj

@Override
  protected Object readValue(DataInput in) throws IOException {
    WordDFIDF idf = new WordDFIDF();
    idf.readBinary(in);
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      IOUtils.writeBinary(baos, idf);
      context.write(key, new BytesWritable(baos.toByteArray()));
    } catch (InterruptedException e) {
      throw new IOException("");
    }
    return NullWritable.get();
  }
});

代码示例来源:origin: org.openimaj.hadoop.tools/HadoopTwitterTokenTool

@Override
  protected Object readValue(DataInput in) throws IOException {
    WordDFIDF idf = new WordDFIDF();
    idf.readBinary(in);
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      IOUtils.writeBinary(baos, idf);
      context.write(key, new BytesWritable(baos.toByteArray()));
    } catch (InterruptedException e) {
      throw new IOException("");
    }
    return NullWritable.get();
  }
});

代码示例来源:origin: openimaj/openimaj

@Override
public boolean execute(String word, int wordCount) {
  final TimeperiodTweetCountWordCount timeMap = new TimeperiodTweetCountWordCount(key.get(), wordCount,
      periodCountWordCount.getNTweets());
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  try {
    IOUtils.writeBinary(os, timeMap);
    final BytesWritable writeable = new BytesWritable(os.toByteArray());
    context.write(new Text(word), writeable);
  } catch (final IOException e) {
    return false;
  } catch (final InterruptedException e) {
    return false;
  }
  return true;
}
  });

代码示例来源:origin: org.openimaj.hadoop.tools/HadoopTwitterTokenTool

@Override
public boolean execute(String word, int wordCount) {
  final TimeperiodTweetCountWordCount timeMap = new TimeperiodTweetCountWordCount(key.get(), wordCount,
      periodCountWordCount.getNTweets());
  final ByteArrayOutputStream os = new ByteArrayOutputStream();
  try {
    IOUtils.writeBinary(os, timeMap);
    final BytesWritable writeable = new BytesWritable(os.toByteArray());
    context.write(new Text(word), writeable);
  } catch (final IOException e) {
    return false;
  } catch (final InterruptedException e) {
    return false;
  }
  return true;
}
  });

代码示例来源:origin: openimaj/openimaj

@Override
  public void process(SimilarityMatrix matrix, File output) throws Exception {
    matrix.processInplace(new Threshold(threshold));
    
    if (output == null)
      System.out.println(matrix);
    else 
      IOUtils.writeBinary(output, matrix);
  }
}

代码示例来源:origin: org.openimaj.hadoop.tools/HadoopTwitterTokenTool

@Override
  protected void reduce(LongWritable key, Iterable<BytesWritable> values,
      Reducer<LongWritable, BytesWritable, LongWritable, BytesWritable>.Context context) throws IOException,
      InterruptedException
  {
    final TweetCountWordMap accum = new TweetCountWordMap();
    for (final BytesWritable tweetwordmapbytes : values) {
      TweetCountWordMap tweetwordmap = null;
      tweetwordmap = IOUtils.read(new ByteArrayInputStream(tweetwordmapbytes.getBytes()),
          TweetCountWordMap.class);
      accum.combine(tweetwordmap);
    }
    final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    IOUtils.writeBinary(outstream, accum);
    context.write(key, new BytesWritable(outstream.toByteArray()));
  }
}

代码示例来源:origin: openimaj/openimaj

@Override
  protected void cleanup(Mapper<LongWritable, Text, LongWritable, BytesWritable>.Context context)
      throws IOException, InterruptedException
  {
    System.out.println("Cleaing up mapper, seen " + this.tweetWordMap.entrySet().size() + " time slots");
    for (final Entry<Long, TweetCountWordMap> tpMapEntry : this.tweetWordMap.entrySet()) {
      final Long time = tpMapEntry.getKey();
      final TweetCountWordMap map = tpMapEntry.getValue();
      System.out.println("... time( " + time + ") seen " + map.getTweetWordMap().size() + " words");
      final ByteArrayOutputStream outarr = new ByteArrayOutputStream();
      IOUtils.writeBinary(outarr, map);
      final byte[] arr = outarr.toByteArray();
      final BytesWritable toWrite = new BytesWritable(arr);
      context.write(END_TIME, toWrite);
      context.write(new LongWritable(time), toWrite);
      context.getCounter(TextEntryType.ACUAL_EMITS).increment(1);
    }
  }
}

代码示例来源:origin: org.openimaj.hadoop.tools/HadoopTwitterTokenTool

@Override
  protected void cleanup(Mapper<LongWritable, Text, LongWritable, BytesWritable>.Context context)
      throws IOException, InterruptedException
  {
    System.out.println("Cleaing up mapper, seen " + this.tweetWordMap.entrySet().size() + " time slots");
    for (final Entry<Long, TweetCountWordMap> tpMapEntry : this.tweetWordMap.entrySet()) {
      final Long time = tpMapEntry.getKey();
      final TweetCountWordMap map = tpMapEntry.getValue();
      System.out.println("... time( " + time + ") seen " + map.getTweetWordMap().size() + " words");
      final ByteArrayOutputStream outarr = new ByteArrayOutputStream();
      IOUtils.writeBinary(outarr, map);
      final byte[] arr = outarr.toByteArray();
      final BytesWritable toWrite = new BytesWritable(arr);
      context.write(END_TIME, toWrite);
      context.write(new LongWritable(time), toWrite);
      context.getCounter(TextEntryType.ACUAL_EMITS).increment(1);
    }
  }
}

代码示例来源:origin: openimaj/openimaj

void execute() throws IOException {
  FeatureVector fv = featureOp.extract(input);
  
  if (binary)
    IOUtils.writeBinary(output, fv);
  else
    IOUtils.writeASCII(output, fv);
}

代码示例来源:origin: org.openimaj.tools/GlobalFeaturesTool

void execute() throws IOException {
  FImage mask = FloodFill.floodFill(input, px, py, thresh);
  
  if (maskoutput != null)
    ImageUtilities.write(mask, maskoutput.getName().substring(maskoutput.getName().lastIndexOf(".") + 1), maskoutput);
  
  if (feature != null) { 
    FeatureVector fv = featureOp.execute(input, mask);
  
    if (binary)
      IOUtils.writeBinary(output, fv);
    else
      IOUtils.writeASCII(output, fv);
  }
}

代码示例来源:origin: org.openimaj/sandbox

@Override
  public void perform(File file) {
    try {
      final File out = new File(outDir, file.getName().replace("jpg", "sift"));
      if (out.exists())
        return;
      final DoGSIFTEngine engine = new DoGSIFTEngine();
      // engine.getOptions().setDoubleInitialImage(false);
      System.out.println(file);
      final FImage image = ImageUtilities.readF(file);
      // image = ResizeProcessor.resizeMax(image, 300);
      final LocalFeatureList<Keypoint> keys = engine.findFeatures(image);
      IOUtils.writeBinary(out, keys);
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
});

代码示例来源:origin: openimaj/openimaj

@Override
  protected void
      map(Text key, BytesWritable value, Mapper<Text, BytesWritable, Text, BytesWritable>.Context context)
          throws InterruptedException
  {
    try {
      final MBFImage img = ImageUtilities.readMBF(new ByteArrayInputStream(value.getBytes()));
      final FeatureVector fv = options.featureOp.extract(img);
      final ByteArrayOutputStream baos = new ByteArrayOutputStream();
      if (options.binary)
        IOUtils.writeBinary(baos, fv);
      else
        IOUtils.writeASCII(baos, fv);
      context.write(key, new BytesWritable(baos.toByteArray()));
    } catch (final Exception e) {
      logger.warn("Problem processing image " + key + " (" + e + ")");
    }
  }
}

代码示例来源:origin: openimaj/openimaj

void execute() throws IOException {
  FImage mask = FloodFill.floodFill(input, px, py, thresh);
  
  if (maskoutput != null)
    ImageUtilities.write(mask, maskoutput.getName().substring(maskoutput.getName().lastIndexOf(".") + 1), maskoutput);
  
  if (feature != null) { 
    FeatureVector fv = featureOp.execute(input, mask);
  
    if (binary)
      IOUtils.writeBinary(output, fv);
    else
      IOUtils.writeASCII(output, fv);
  }
}

代码示例来源:origin: org.openimaj.tools/GlobalFeaturesTool

void execute() throws IOException {
  FeatureVector fv = featureOp.extract(input);
  
  if (binary)
    IOUtils.writeBinary(output, fv);
  else
    IOUtils.writeASCII(output, fv);
}

代码示例来源:origin: openimaj/openimaj

@Override
  protected void reduce(LongWritable key, Iterable<BytesWritable> values,
      Reducer<LongWritable, BytesWritable, LongWritable, BytesWritable>.Context context) throws IOException,
      InterruptedException
  {
    final TweetCountWordMap accum = new TweetCountWordMap();
    for (final BytesWritable tweetwordmapbytes : values) {
      TweetCountWordMap tweetwordmap = null;
      tweetwordmap = IOUtils.read(new ByteArrayInputStream(tweetwordmapbytes.getBytes()),
          TweetCountWordMap.class);
      accum.combine(tweetwordmap);
    }
    final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    IOUtils.writeBinary(outstream, accum);
    context.write(key, new BytesWritable(outstream.toByteArray()));
  }
}

代码示例来源:origin: openimaj/openimaj

private void saveFoldParameterLearner(int fold, int j, BilinearSparseOnlineLearner learner) {
  // save the state
  final File learnerOut = new File(String.format("%s/fold_%d", currentOutputRoot(), fold), String.format(
      "learner_%d", j));
  final File learnerOutMat = new File(String.format("%s/fold_%d", currentOutputRoot(), fold), String.format(
      "learner_%d.mat", j));
  learnerOut.getParentFile().mkdirs();
  try {
    IOUtils.writeBinary(learnerOut, learner);
    final Collection<MLArray> data = new ArrayList<MLArray>();
    data.add(CFMatrixUtils.toMLArray("u", learner.getU()));
    data.add(CFMatrixUtils.toMLArray("w", learner.getW()));
    if (learner.getBias() != null) {
      data.add(CFMatrixUtils.toMLArray("b", learner.getBias()));
    }
    final MatFileWriter writer = new MatFileWriter(learnerOutMat, data);
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: org.openimaj.tools/LocalFeaturesTool

@Override
  public void perform(File input) {
    try {
      final byte[] img = options.getInputImage(input);
      final Timer timing = Timer.timer();
      final LocalFeatureList<? extends LocalFeature<?, ?>> kpl = options.getMode().extract(img);
      timing.stop();
      if (options.printTiming()) {
        System.out.println("Took: " + timing.duration());
      }
      if (options.isAsciiMode()) {
        IOUtils.writeASCII(options.getOutput(input), kpl);
      } else {
        IOUtils.writeBinary(options.getOutput(input), kpl);
      }
    } catch (final IOException e) {
      System.err.println(e);
    }
  }
}, pool);

相关文章