Morten Grum
Morten Grum

Reputation: 1011

OpenIMAJ - error reading feature list saved as ascii

Working with OpenIMAJ I'd like to save feature lists for later use but I'm getting a java.util.NoSuchElementException: No line found exception (see below) while re-reading the feature file I just saved. I've checked that the text file exists though I'm not really sure whether the full contents is what is ought to be (it's very long).

Any ideas what's wrong?

Thanks in advance!

(My trial code is pasted below).

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)
    ... 

My trial code looks like this:

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.");

Upvotes: 0

Views: 83

Answers (1)

Jon
Jon

Reputation: 841

I think the problem is that you're not closing the PrintWriter used to save the features, and that it hasn't had a time to actually write the contents. However you shouldn't really use the LocalFeatureList.writeASCII method directly as it will not write the header information; rather use IOUtils.writeASCII. Replace:

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

with

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

and then it should work. This also deals with closing the file once it's written.

Upvotes: 2

Related Questions