Search in sources :

Example 11 with JochreImage

use of com.joliciel.jochre.graphics.JochreImage in project jochre by urieli.

the class DocumentController method getDocumentTree.

public TreeModel<TreeNode<DocOrImage>> getDocumentTree() {
    LOG.debug("getDocumentTree2");
    DocumentDao documentDao = DocumentDao.getInstance(jochreSession);
    List<JochreDocument> docs = documentDao.findDocuments();
    List<TreeNode<DocOrImage>> docNodeList = new ArrayList<>();
    for (JochreDocument doc : docs) {
        List<TreeNode<DocOrImage>> imageNodeList = new ArrayList<>();
        for (JochrePage page : doc.getPages()) {
            LOG.debug("page " + page.getIndex());
            for (JochreImage image : page.getImages()) {
                DefaultTreeNode<DocOrImage> imageNode = new DefaultTreeNode<DocumentController.DocOrImage>(new DocOrImage(image));
                imageNodeList.add(imageNode);
                if (imageId == image.getId())
                    currentImage = image;
            }
        // have images?
        }
        boolean open = docId == doc.getId();
        if (open)
            currentDoc = doc;
        DefaultTreeNode<DocOrImage> docNode = new DirectoryTreeNode<DocumentController.DocOrImage>(new DocOrImage(doc), imageNodeList, open);
        docNodeList.add(docNode);
    }
    DefaultTreeNode<DocOrImage> root = new DefaultTreeNode<DocumentController.DocOrImage>(null, docNodeList);
    TreeModel<TreeNode<DocOrImage>> docTree = new DefaultTreeModel<>(root);
    this.reloadDoc();
    this.reloadImage();
    return docTree;
}
Also used : JochreImage(com.joliciel.jochre.graphics.JochreImage) DefaultTreeNode(org.zkoss.zul.DefaultTreeNode) ArrayList(java.util.ArrayList) DocumentDao(com.joliciel.jochre.doc.DocumentDao) JochreDocument(com.joliciel.jochre.doc.JochreDocument) DefaultTreeModel(org.zkoss.zul.DefaultTreeModel) JochrePage(com.joliciel.jochre.doc.JochrePage) TreeNode(org.zkoss.zul.TreeNode) DefaultTreeNode(org.zkoss.zul.DefaultTreeNode)

Example 12 with JochreImage

use of com.joliciel.jochre.graphics.JochreImage in project jochre by urieli.

the class LetterFeatureTester method applyFeatures.

/**
 * Apply the features provided to all images currently marked as
 * ImageStatus.TRAINING_VALIDATED.
 *
 * @param minShapeId
 *            the first shape id to apply features to
 */
public void applyFeatures(Set<LetterFeature<?>> features, Set<String> letters, int minImageId, int minShapeId) {
    GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
    List<JochreImage> images = graphicsDao.findImages(new ImageStatus[] { ImageStatus.TRAINING_VALIDATED });
    for (JochreImage image : images) {
        if (image.getId() >= minImageId) {
            this.testFeatures(image, features, letters, minShapeId);
        }
        image.clearMemory();
    }
}
Also used : JochreImage(com.joliciel.jochre.graphics.JochreImage) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao)

Example 13 with JochreImage

use of com.joliciel.jochre.graphics.JochreImage in project jochre by urieli.

the class CorpusLexiconBuilder method buildLexicon.

/**
 * Build a lexicon from the training corpus.
 */
public TextFileLexicon buildLexicon() {
    TextFileLexicon lexicon = new TextFileLexicon();
    JochreCorpusImageReader imageReader = new JochreCorpusImageReader(jochreSession);
    imageReader.setSelectionCriteria(criteria);
    String wordText = "";
    while (imageReader.hasNext()) {
        JochreImage image = imageReader.next();
        for (Paragraph paragraph : image.getParagraphs()) {
            // rows ending in dashes can only be held-over within the same
            // paragraph.
            // to avoid strange things like a page number getting added to
            // the word,
            // if the dash is on the last row of the page.
            String holdoverWord = null;
            for (RowOfShapes row : paragraph.getRows()) {
                for (GroupOfShapes group : row.getGroups()) {
                    if (group.isBrokenWord())
                        continue;
                    wordText = "";
                    for (Shape shape : group.getShapes()) {
                        if (shape.getLetter() != null)
                            wordText += shape.getLetter();
                    }
                    if (wordText.length() == 0) {
                        lexicon.incrementEntry("");
                        continue;
                    }
                    List<String> words = jochreSession.getLinguistics().splitText(wordText);
                    int i = 0;
                    for (String word : words) {
                        if (i == 0) {
                            // first word
                            if (holdoverWord != null && holdoverWord.length() > 0) {
                                word = holdoverWord + word;
                                holdoverWord = null;
                            }
                        }
                        if (i == words.size() - 1) {
                            // last word
                            if (group.getIndex() == row.getGroups().size() - 1 && word.endsWith("-")) {
                                // a dash at the end of a line
                                if (group.isHardHyphen())
                                    holdoverWord = word;
                                else
                                    holdoverWord = word.substring(0, word.length() - 1);
                                word = "";
                            }
                        }
                        lexicon.incrementEntry(word);
                        i++;
                    }
                }
            }
        }
    }
    return lexicon;
}
Also used : JochreCorpusImageReader(com.joliciel.jochre.graphics.JochreCorpusImageReader) JochreImage(com.joliciel.jochre.graphics.JochreImage) Shape(com.joliciel.jochre.graphics.Shape) GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes) RowOfShapes(com.joliciel.jochre.graphics.RowOfShapes) Paragraph(com.joliciel.jochre.graphics.Paragraph)

Example 14 with JochreImage

use of com.joliciel.jochre.graphics.JochreImage in project jochre by urieli.

the class RecursiveShapeSplitterTest method testSplitShapeNoSplitMoreLikely.

@SuppressWarnings("unchecked")
@Test
public void testSplitShapeNoSplitMoreLikely() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    BufferedImage originalImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
    final JochreImage jochreImage = new JochreImage(originalImage, jochreSession);
    final Shape shape = new Shape(jochreImage, 0, 0, 63, 15, jochreSession);
    shape.setBaseLine(12);
    shape.setMeanLine(4);
    final Shape shape1 = new Shape(jochreImage, 0, 0, 31, 15, jochreSession);
    shape1.setBaseLine(12);
    shape1.setMeanLine(4);
    final Shape shape2 = new Shape(jochreImage, 32, 0, 63, 15, jochreSession);
    shape2.setBaseLine(12);
    shape2.setMeanLine(4);
    final SplitCandidateFinder splitCandidateFinder = mock(SplitCandidateFinder.class);
    final DecisionMaker decisionMaker = mock(DecisionMaker.class);
    Split split = new Split(shape, jochreSession);
    split.setPosition(31);
    List<Split> splits = new ArrayList<>();
    splits.add(split);
    when(splitCandidateFinder.findSplitCandidates(shape)).thenReturn(splits);
    Decision yesDecision = new Decision(SplitOutcome.DO_SPLIT.name(), 0.4);
    Decision noDecision = new Decision(SplitOutcome.DO_NOT_SPLIT.name(), 0.6);
    List<Decision> decisions = new ArrayList<>();
    decisions.add(yesDecision);
    decisions.add(noDecision);
    when(decisionMaker.decide(anyList())).thenReturn(decisions);
    Split split1 = new Split(shape1, jochreSession);
    split1.setPosition(15);
    List<Split> splits1 = new ArrayList<>();
    splits1.add(split1);
    when(splitCandidateFinder.findSplitCandidates(shape1)).thenReturn(splits1);
    Split split2 = new Split(shape2, jochreSession);
    split2.setPosition(15);
    List<Split> splits2 = new ArrayList<>();
    splits2.add(split2);
    when(splitCandidateFinder.findSplitCandidates(shape2)).thenReturn(splits2);
    Set<SplitFeature<?>> splitFeatures = new TreeSet<>();
    RecursiveShapeSplitter splitter = new RecursiveShapeSplitter(splitCandidateFinder, splitFeatures, decisionMaker, jochreSession);
    splitter.setBeamWidth(10);
    splitter.setMaxDepth(2);
    splitter.setMinWidthRatio(1.0);
    List<ShapeSequence> shapeSequences = splitter.split(shape);
    assertEquals(5, shapeSequences.size());
    int i = 0;
    double prob = 1.0;
    double twoThirds = 0.4 / 0.6;
    LOG.debug("twoThirds: " + twoThirds);
    for (ShapeSequence shapeSequence : shapeSequences) {
        LOG.debug("sequence " + i + " decisions:");
        for (Decision decision : shapeSequence.getDecisions()) LOG.debug("" + decision.getProbability());
        if (i == 0) {
            prob = 1.0;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(1, shapeSequence.size());
        } else if (i == 1) {
            prob = 1.0 * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(2, shapeSequence.size());
        } else if (i == 2) {
            prob = 1.0 * twoThirds * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(3, shapeSequence.size());
        } else if (i == 3) {
            prob = 1.0 * twoThirds * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(3, shapeSequence.size());
        } else if (i == 4) {
            prob = 1.0 * twoThirds * twoThirds * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(4, shapeSequence.size());
        }
        i++;
    }
}
Also used : JochreImage(com.joliciel.jochre.graphics.JochreImage) Shape(com.joliciel.jochre.graphics.Shape) Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) DecisionMaker(com.joliciel.talismane.machineLearning.DecisionMaker) SplitFeature(com.joliciel.jochre.boundaries.features.SplitFeature) BufferedImage(java.awt.image.BufferedImage) Decision(com.joliciel.talismane.machineLearning.Decision) TreeSet(java.util.TreeSet) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Example 15 with JochreImage

use of com.joliciel.jochre.graphics.JochreImage in project jochre by urieli.

the class RecursiveShapeSplitterTest method testSplitShapeSplitMoreLikely.

/**
 * If a split is always more likely (e.g. 60% likelihood), ensure the shape
 * sequences are ordered correctly.
 */
@SuppressWarnings("unchecked")
@Test
public void testSplitShapeSplitMoreLikely() throws Exception {
    System.setProperty("config.file", "src/test/resources/test.conf");
    ConfigFactory.invalidateCaches();
    Config config = ConfigFactory.load();
    JochreSession jochreSession = new JochreSession(config);
    BufferedImage originalImage = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
    final JochreImage jochreImage = new JochreImage(originalImage, jochreSession);
    final Shape shape = new Shape(jochreImage, 0, 0, 63, 15, jochreSession);
    shape.setBaseLine(12);
    shape.setMeanLine(4);
    final Shape shape1 = new Shape(jochreImage, 0, 0, 31, 15, jochreSession);
    shape1.setBaseLine(12);
    shape1.setMeanLine(4);
    final Shape shape2 = new Shape(jochreImage, 32, 0, 63, 15, jochreSession);
    shape2.setBaseLine(12);
    shape2.setMeanLine(4);
    final SplitCandidateFinder splitCandidateFinder = mock(SplitCandidateFinder.class);
    final DecisionMaker decisionMaker = mock(DecisionMaker.class);
    Split split = new Split(shape, jochreSession);
    split.setPosition(31);
    List<Split> splits = new ArrayList<>();
    splits.add(split);
    when(splitCandidateFinder.findSplitCandidates(shape)).thenReturn(splits);
    Decision yesDecision = new Decision(SplitOutcome.DO_SPLIT.name(), 0.6);
    Decision noDecision = new Decision(SplitOutcome.DO_NOT_SPLIT.name(), 0.4);
    List<Decision> decisions = new ArrayList<>();
    decisions.add(yesDecision);
    decisions.add(noDecision);
    when(decisionMaker.decide(anyList())).thenReturn(decisions);
    Split split1 = new Split(shape1, jochreSession);
    split1.setPosition(15);
    List<Split> splits1 = new ArrayList<>();
    splits1.add(split1);
    when(splitCandidateFinder.findSplitCandidates(shape1)).thenReturn(splits1);
    Split split2 = new Split(shape2, jochreSession);
    split2.setPosition(15);
    List<Split> splits2 = new ArrayList<>();
    splits2.add(split2);
    when(splitCandidateFinder.findSplitCandidates(shape2)).thenReturn(splits2);
    Set<SplitFeature<?>> splitFeatures = new TreeSet<>();
    RecursiveShapeSplitter splitter = new RecursiveShapeSplitter(splitCandidateFinder, splitFeatures, decisionMaker, jochreSession);
    splitter.setBeamWidth(10);
    splitter.setMaxDepth(2);
    splitter.setMinWidthRatio(1.0);
    List<ShapeSequence> shapeSequences = splitter.split(shape);
    assertEquals(5, shapeSequences.size());
    int i = 0;
    for (ShapeSequence shapeSequence : shapeSequences) {
        LOG.debug("sequence " + i + " shapes:");
        for (ShapeInSequence shapeInSequence : shapeSequence) {
            Shape oneShape = shapeInSequence.getShape();
            LOG.debug("Shape: " + oneShape.getLeft() + "," + oneShape.getRight());
        }
        LOG.debug("" + shapeSequence.getScore());
        i++;
    }
    i = 0;
    double prob = 1.0;
    double twoThirds = 0.4 / 0.6;
    LOG.debug("twoThirds: " + twoThirds);
    for (ShapeSequence shapeSequence : shapeSequences) {
        LOG.debug("sequence " + i + " decisions:");
        for (Decision decision : shapeSequence.getDecisions()) LOG.debug("" + decision.getProbability());
        if (i == 0) {
            prob = 1.0;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(4, shapeSequence.size());
        } else if (i == 1) {
            prob = 1.0 * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(3, shapeSequence.size());
        } else if (i == 2) {
            prob = 1.0 * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(3, shapeSequence.size());
        } else if (i == 3) {
            prob = 1.0 * twoThirds * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(2, shapeSequence.size());
        } else if (i == 4) {
            prob = 1.0 * twoThirds * twoThirds * twoThirds;
            assertEquals(prob, shapeSequence.getScore(), 0.0001);
            assertEquals(1, shapeSequence.size());
        }
        i++;
    }
}
Also used : JochreImage(com.joliciel.jochre.graphics.JochreImage) Shape(com.joliciel.jochre.graphics.Shape) Config(com.typesafe.config.Config) ArrayList(java.util.ArrayList) DecisionMaker(com.joliciel.talismane.machineLearning.DecisionMaker) SplitFeature(com.joliciel.jochre.boundaries.features.SplitFeature) BufferedImage(java.awt.image.BufferedImage) Decision(com.joliciel.talismane.machineLearning.Decision) TreeSet(java.util.TreeSet) JochreSession(com.joliciel.jochre.JochreSession) Test(org.junit.Test)

Aggregations

JochreImage (com.joliciel.jochre.graphics.JochreImage)20 Shape (com.joliciel.jochre.graphics.Shape)12 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 JochrePage (com.joliciel.jochre.doc.JochrePage)7 JochreSession (com.joliciel.jochre.JochreSession)6 JochreDocument (com.joliciel.jochre.doc.JochreDocument)6 GroupOfShapes (com.joliciel.jochre.graphics.GroupOfShapes)6 Paragraph (com.joliciel.jochre.graphics.Paragraph)6 RowOfShapes (com.joliciel.jochre.graphics.RowOfShapes)6 Config (com.typesafe.config.Config)6 BufferedImage (java.awt.image.BufferedImage)6 SourceImage (com.joliciel.jochre.graphics.SourceImage)4 TreeSet (java.util.TreeSet)4 SplitFeature (com.joliciel.jochre.boundaries.features.SplitFeature)3 Segmenter (com.joliciel.jochre.graphics.Segmenter)3 Decision (com.joliciel.talismane.machineLearning.Decision)3 DecisionMaker (com.joliciel.talismane.machineLearning.DecisionMaker)3 StringWriter (java.io.StringWriter)3 DocumentObserver (com.joliciel.jochre.doc.DocumentObserver)2