use of com.joliciel.jochre.doc.DocumentObserver in project jochre by urieli.
the class JochreCorpusImageProcessor method process.
public void process() {
this.initialiseStream();
JochreDocument currentDoc = null;
JochrePage currentPage = null;
for (JochreImage image : this.getImages()) {
if (!image.getPage().equals(currentPage)) {
if (currentPage != null) {
for (DocumentObserver observer : observers) {
observer.onPageComplete(currentPage);
}
LOG.debug("completed page: " + currentPage);
}
}
if (!image.getPage().getDocument().equals(currentDoc)) {
if (currentDoc != null) {
for (DocumentObserver observer : observers) {
observer.onDocumentComplete(currentDoc);
}
LOG.debug("completed doc: " + currentDoc);
}
currentDoc = image.getPage().getDocument();
LOG.debug("next doc: " + currentDoc);
for (DocumentObserver observer : observers) {
observer.onDocumentStart(currentDoc);
}
}
if (!image.getPage().equals(currentPage)) {
currentPage = image.getPage();
LOG.debug("next page: " + currentPage);
for (DocumentObserver observer : observers) {
observer.onPageStart(currentPage);
}
}
LOG.debug("next image: " + image);
for (DocumentObserver observer : observers) {
observer.onImageStart(image);
}
for (DocumentObserver observer : observers) {
observer.onImageComplete(image);
}
LOG.debug("completed image: " + image);
image.clearMemory();
}
if (currentPage != null) {
for (DocumentObserver observer : observers) {
observer.onPageComplete(currentPage);
}
LOG.debug("completed page: " + currentPage);
}
if (currentDoc != null) {
for (DocumentObserver observer : observers) {
observer.onDocumentComplete(currentDoc);
}
LOG.debug("completed doc: " + currentDoc);
}
for (DocumentObserver observer : observers) {
observer.onAnalysisComplete();
}
}
use of com.joliciel.jochre.doc.DocumentObserver in project jochre by urieli.
the class TextController method onTimer$startRenderTimer.
@Listen("onTimer = #startRenderTimer")
public void onTimer$startRenderTimer(Event event) {
try {
progressBox.setVisible(true);
if (currentImage != null) {
Html html = new Html();
StringWriter out = new StringWriter();
DocumentObserver textGetter = new TextGetter(out, TextFormat.XHTML);
textGetter.onImageComplete(currentImage);
html.setContent(out.toString());
htmlContent.appendChild(html);
progressMeter1.setValue(100);
// progressBox.setVisible(false);
} else {
if (currentPageIndex < currentDoc.getPages().size()) {
JochrePage page = currentDoc.getPages().get(currentPageIndex);
for (JochreImage image : page.getImages()) {
Html html = new Html();
StringWriter out = new StringWriter();
DocumentObserver textGetter = new TextGetter(out, TextFormat.XHTML);
textGetter.onImageComplete(image);
out.append("<HR/>");
html.setContent(out.toString());
htmlContent.appendChild(html);
}
page.clearMemory();
currentPageIndex++;
double percentComplete = ((double) currentPageIndex / (double) currentDoc.getPages().size()) * 100;
progressMeter1.setValue(new Double(percentComplete).intValue());
startRenderTimer.setRunning(true);
} else {
progressMeter1.setValue(100);
// progressBox.setVisible(false);
}
}
} catch (Exception e) {
LOG.error("Failure in onTimer$startRenderTimer", e);
throw new RuntimeException(e);
}
}
use of com.joliciel.jochre.doc.DocumentObserver in project jochre by urieli.
the class Jochre method doCommandEvaluateFull.
/**
* Evaluate a suite of split/merge models and letter guessing model.
* @param criteria
* for selecting the evaluation corpus
* @param save
* whether or not the letter guesses should be saved
*/
public void doCommandEvaluateFull(CorpusSelectionCriteria criteria, boolean save, File outputDir, MostLikelyWordChooser wordChooser, String suffix, List<DocumentObserver> observers) throws IOException {
String baseName = jochreSession.getLetterModelPath().substring(0, jochreSession.getLetterModelPath().indexOf("."));
if (baseName.lastIndexOf("/") > 0)
baseName = baseName.substring(baseName.lastIndexOf("/") + 1);
ClassificationModel letterModel = jochreSession.getLetterModel();
List<String> letterFeatureDescriptors = letterModel.getFeatureDescriptors();
LetterFeatureParser letterFeatureParser = new LetterFeatureParser();
Set<LetterFeature<?>> letterFeatures = letterFeatureParser.getLetterFeatureSet(letterFeatureDescriptors);
LetterGuesser letterGuesser = new LetterGuesser(letterFeatures, letterModel.getDecisionMaker());
ClassificationModel splitModel = jochreSession.getSplitModel();
if (splitModel == null)
throw new IllegalArgumentException("Missing parameter: jochre.image-analyser.split-model");
List<String> splitFeatureDescriptors = splitModel.getFeatureDescriptors();
SplitFeatureParser splitFeatureParser = new SplitFeatureParser();
Set<SplitFeature<?>> splitFeatures = splitFeatureParser.getSplitFeatureSet(splitFeatureDescriptors);
SplitCandidateFinder splitCandidateFinder = new SplitCandidateFinder(jochreSession);
splitCandidateFinder.setMinDistanceBetweenSplits(5);
ShapeSplitter shapeSplitter = new RecursiveShapeSplitter(splitCandidateFinder, splitFeatures, splitModel.getDecisionMaker(), jochreSession);
ClassificationModel mergeModel = jochreSession.getMergeModel();
if (mergeModel == null)
throw new IllegalArgumentException("Missing parameter: jochre.image-analyser.merge-model");
List<String> mergeFeatureDescriptors = mergeModel.getFeatureDescriptors();
MergeFeatureParser mergeFeatureParser = new MergeFeatureParser();
Set<MergeFeature<?>> mergeFeatures = mergeFeatureParser.getMergeFeatureSet(mergeFeatureDescriptors);
ShapeMerger shapeMerger = new ShapeMerger(mergeFeatures, mergeModel.getDecisionMaker());
BoundaryDetector boundaryDetector = null;
String boundaryDetectorTypeName = jochreSession.getConfig().getConfig("jochre.boundaries").getString("boundary-detector-type");
BoundaryDetectorType boundaryDetectorType = BoundaryDetectorType.valueOf(boundaryDetectorTypeName);
switch(boundaryDetectorType) {
case LetterByLetter:
boundaryDetector = new LetterByLetterBoundaryDetector(shapeSplitter, shapeMerger, jochreSession);
break;
case Deterministic:
boundaryDetector = new DeterministicBoundaryDetector(shapeSplitter, shapeMerger, jochreSession);
break;
}
ImageAnalyser imageAnalyser = new BeamSearchImageAnalyser(boundaryDetector, letterGuesser, wordChooser, jochreSession);
LetterValidator letterValidator = new ComponentCharacterValidator(jochreSession);
OriginalShapeLetterAssigner shapeLetterAssigner = new OriginalShapeLetterAssigner();
shapeLetterAssigner.setEvaluate(true);
shapeLetterAssigner.setSave(save);
shapeLetterAssigner.setLetterValidator(letterValidator);
shapeLetterAssigner.setSingleLetterMethod(false);
imageAnalyser.addObserver(shapeLetterAssigner);
ErrorLogger errorLogger = new ErrorLogger(jochreSession);
Writer errorWriter = null;
File errorFile = new File(outputDir, baseName + suffix + "errors.txt");
errorFile.delete();
errorWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(errorFile, true), "UTF8"));
errorLogger.setErrorWriter(errorWriter);
imageAnalyser.addObserver(errorLogger);
JochreCorpusImageProcessor imageProcessor = new JochreCorpusImageProcessor(criteria, jochreSession);
imageProcessor.addObserver(imageAnalyser);
for (DocumentObserver observer : observers) imageProcessor.addObserver(observer);
imageProcessor.process();
LOG.debug("F-score for " + jochreSession.getLetterModelPath() + ": " + shapeLetterAssigner.getFScoreCalculator().getTotalFScore());
String modelFileName = baseName + suffix + "_full";
File fscoreFile = new File(outputDir, modelFileName + "_fscores.csv");
Writer fscoreWriter = errorWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fscoreFile, true), jochreSession.getCsvEncoding()));
shapeLetterAssigner.getFScoreCalculator().writeScoresToCSV(fscoreWriter);
}
use of com.joliciel.jochre.doc.DocumentObserver in project jochre by urieli.
the class Jochre method doCommandAnalyse.
/**
* Analyse a set of images based on a given letter-guessing model.
*
* @param criteria
* the criteria used to select the documents to be analysed
* @param wordChooser
* the word chooser to use
* @param observers
* the observers, used to create analysis output
*/
public void doCommandAnalyse(CorpusSelectionCriteria criteria, MostLikelyWordChooser wordChooser, List<DocumentObserver> observers) throws IOException {
ClassificationModel letterModel = jochreSession.getLetterModel();
List<String> letterFeatureDescriptors = letterModel.getFeatureDescriptors();
LetterFeatureParser letterFeatureParser = new LetterFeatureParser();
Set<LetterFeature<?>> letterFeatures = letterFeatureParser.getLetterFeatureSet(letterFeatureDescriptors);
LetterGuesser letterGuesser = new LetterGuesser(letterFeatures, letterModel.getDecisionMaker());
ImageAnalyser analyser = new BeamSearchImageAnalyser(null, letterGuesser, wordChooser, jochreSession);
LetterAssigner letterAssigner = new LetterAssigner();
analyser.addObserver(letterAssigner);
JochreCorpusImageProcessor imageProcessor = new JochreCorpusImageProcessor(criteria, jochreSession);
imageProcessor.addObserver(analyser);
for (DocumentObserver observer : observers) imageProcessor.addObserver(observer);
imageProcessor.process();
}
use of com.joliciel.jochre.doc.DocumentObserver in project jochre by urieli.
the class Jochre method doCommandTransform.
/**
* Transform stuff in the corpus into a given output format.
* @param criteria
* the criteria used to select the documents to be analysed
* @param observers
* @param imageObservers
*/
public void doCommandTransform(CorpusSelectionCriteria criteria, List<DocumentObserver> observers, List<PdfImageObserver> imageObservers) throws IOException {
JochreCorpusImageProcessor imageProcessor = new JochreCorpusImageProcessor(criteria, jochreSession);
for (DocumentObserver observer : observers) imageProcessor.addObserver(observer);
imageProcessor.process();
}
Aggregations