OpenIMAJ - 错误读取功能列表保存为ascii

时间:2015-02-26 07:47:45

标签: openimaj

使用OpenIMAJ我想保存功能列表供以后使用但我在重新读取刚保存的功能文件时遇到java.util.NoSuchElementException: No line found异常(见下文)。我已经检查过文本文件是否存在虽然我不确定全部内容是否应该是什么(它很长)。

任何想法有什么不对?

提前致谢!

(我的试用代码粘贴在下面)。

java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Unknown Source)
    at org.openimaj.image.feature.local.keypoints.Keypoint.readASCII(Keypoint.java:296)
    at org.openimaj.feature.local.list.LocalFeatureListUtils.readASCII(LocalFeatureListUtils.java:170)
    at org.openimaj.feature.local.list.LocalFeatureListUtils.readASCII(LocalFeatureListUtils.java:136)
    at org.openimaj.feature.local.list.MemoryLocalFeatureList.read(MemoryLocalFeatureList.java:134)
    ... 

我的试用版代码如下:

Video<MBFImage> originalVideo = getVideo();
MBFImage frame = originalVideo.getCurrentFrame().clone();

DoGSIFTEngine engine = new DoGSIFTEngine();
LocalFeatureList<Keypoint> originalFeatureList = engine.findFeatures(frame.flatten());

try {
    originalFeatureList.writeASCII(new PrintWriter(new File("featureList.txt")));
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

System.out.println("Saved feature list with "+originalFeatureList.size()+" keypoints.");

MemoryLocalFeatureList<Keypoint> loadedFeatureList = null;
try {
    loadedFeatureList = MemoryLocalFeatureList.read(new File("featureList.txt"), Keypoint.class);
} catch (IOException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

System.out.println("Loaded feature list with "+loadedFeatureList.size()+" keypoints.");

1 个答案:

答案 0 :(得分:2)

我认为问题在于您没有关闭用于保存功能的PrintWriter,并且没有时间实际编写内容。但是,您不应该直接使用LocalFeatureList.writeASCII方法,因为它不会写入标头信息;而是使用IOUtils.writeASCII。替换:

originalFeatureList.writeASCII(new PrintWriter(new File("featureList.txt")));

IOUtils.writeASCII(new File("featureList.txt"), originalFeatureList);

然后它应该工作。这也涉及在文件写完后关闭文件。