Search in sources :

Example 31 with Shape

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

the class OriginalBoundaryDetector method findBoundaries.

@Override
public List<ShapeSequence> findBoundaries(GroupOfShapes group) {
    List<ShapeSequence> shapeSequences = new ArrayList<ShapeSequence>();
    ShapeSequence emptySequence = new ShapeSequence();
    for (Shape shape : group.getShapes()) {
        emptySequence.addShape(shape);
    }
    shapeSequences.add(emptySequence);
    return shapeSequences;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) ArrayList(java.util.ArrayList)

Example 32 with Shape

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

the class SplitEvaluator method evaluate.

public FScoreCalculator<String> evaluate(JochreCorpusShapeReader shapeReader, ShapeSplitter shapeSplitter) {
    FScoreCalculator<String> fScoreCalculator = new FScoreCalculator<String>();
    while (shapeReader.hasNext()) {
        Shape shape = shapeReader.next();
        // check if shape is wide enough to bother with
        double widthRatio = (double) shape.getWidth() / (double) shape.getXHeight();
        double heightRatio = (double) shape.getHeight() / (double) shape.getXHeight();
        if (widthRatio >= minWidthRatio || shape.getSplits().size() > 0) {
            LOG.debug("Testing " + shape);
            List<Split> guessedSplits = new ArrayList<Split>();
            if (widthRatio >= minWidthRatio && heightRatio >= minHeightRatio) {
                List<ShapeSequence> shapeSequences = shapeSplitter.split(shape);
                ShapeSequence splitShapes = shapeSequences.get(0);
                if (splitShapes.getScore() > minProbabilityForDecision) {
                    for (ShapeInSequence splitShapeInSequence : splitShapes) {
                        Shape splitShape = splitShapeInSequence.getShape();
                        if (splitShape.getRight() != shape.getRight()) {
                            Split guessedSplit = new Split(shape, jochreSession);
                            guessedSplit.setPosition(splitShape.getRight() - shape.getLeft());
                            guessedSplits.add(guessedSplit);
                        }
                    }
                }
            } else {
                LOG.debug("Insufficient width or height");
                LOG.debug("widthRatio: " + widthRatio);
                LOG.debug("heightRatio: " + heightRatio);
            }
            Set<Split> splitsNotFound = new HashSet<Split>();
            Set<Split> wrongSplitGuesses = new HashSet<Split>(guessedSplits);
            Set<Split> remainingSplitGuesses = new HashSet<Split>(guessedSplits);
            if (shape.getSplits().size() > 0) {
                for (Split split : shape.getSplits()) {
                    LOG.debug("true split: " + split + ", right=" + (shape.getLeft() + split.getPosition()));
                    boolean foundSplit = false;
                    for (Split splitGuess : remainingSplitGuesses) {
                        int diff = split.getPosition() - splitGuess.getPosition();
                        if (diff < 0)
                            diff = 0 - diff;
                        if (diff <= tolerance) {
                            LOG.debug("Found split: " + splitGuess);
                            fScoreCalculator.increment("YES", "YES");
                            wrongSplitGuesses.remove(splitGuess);
                            foundSplit = true;
                            break;
                        }
                    }
                    if (!foundSplit)
                        splitsNotFound.add(split);
                    remainingSplitGuesses = wrongSplitGuesses;
                }
                for (Split split : splitsNotFound) {
                    LOG.debug("Didn't find split: " + split);
                    if (widthRatio >= minWidthRatio)
                        fScoreCalculator.increment("YES", "NO");
                    else
                        fScoreCalculator.increment("YES", "NARROW");
                }
                for (Split guess : wrongSplitGuesses) {
                    LOG.debug("Bad guess: " + guess);
                    fScoreCalculator.increment("NO", "YES");
                }
            } else {
                if (wrongSplitGuesses.size() == 0) {
                    fScoreCalculator.increment("NO", "NO");
                } else {
                    for (Split guess : wrongSplitGuesses) {
                        LOG.debug("Bad guess: " + guess);
                        fScoreCalculator.increment("NO", "YES");
                    }
                }
            }
        }
    }
    return fScoreCalculator;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) FScoreCalculator(com.joliciel.jochre.stats.FScoreCalculator) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 33 with Shape

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

the class TrainingCorpusShapeMerger method merge.

@Override
public Shape merge(Shape shape1, Shape shape2) {
    int left = shape1.getLeft() < shape2.getLeft() ? shape1.getLeft() : shape2.getLeft();
    int top = shape1.getTop() < shape2.getTop() ? shape1.getTop() : shape2.getTop();
    int right = shape1.getRight() > shape2.getRight() ? shape1.getRight() : shape2.getRight();
    int bottom = shape1.getBottom() > shape2.getBottom() ? shape1.getBottom() : shape2.getBottom();
    Shape mergedShape = shape1.getJochreImage().getShape(left, top, right, bottom);
    String letter = "";
    if (shape1.getLetter().length() == 0)
        letter = shape2.getLetter();
    else if (shape2.getLetter().length() == 0)
        letter = shape1.getLetter();
    else
        letter = shape1.getLetter().substring(shape1.getLetter().indexOf("|") + 1);
    mergedShape.setLetter(letter);
    return mergedShape;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape)

Example 34 with Shape

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

the class SlopeDifferenceFeature method checkInternal.

@Override
public FeatureResult<Double> checkInternal(Split split, RuntimeEnvironment env) {
    FeatureResult<Double> result = null;
    FeatureResult<Integer> contourDistanceResult = contourDistanceFeature.check(split, env);
    if (contourDistanceResult != null) {
        int contourDistance = contourDistanceResult.getOutcome();
        int[][] verticalContour = split.getShape().getVerticalContour();
        int x = split.getPosition();
        Shape shape = split.getShape();
        int topStart = verticalContour[x][0];
        int bottomStart = verticalContour[x][1];
        SimpleRegression topRightRegression = new SimpleRegression();
        SimpleRegression bottomRightRegression = new SimpleRegression();
        SimpleRegression topLeftRegression = new SimpleRegression();
        SimpleRegression bottomLeftRegression = new SimpleRegression();
        topRightRegression.addData(x, topStart);
        topLeftRegression.addData(x, topStart);
        bottomRightRegression.addData(x, bottomStart);
        bottomLeftRegression.addData(x, bottomStart);
        for (int i = 1; i <= contourDistance; i++) {
            if (x + i < shape.getWidth()) {
                topRightRegression.addData(x + i, verticalContour[x + i][0]);
                bottomRightRegression.addData(x + i, verticalContour[x + i][1]);
            }
            if (x - i >= 0) {
                topLeftRegression.addData(x - i, verticalContour[x - i][0]);
                bottomLeftRegression.addData(x - i, verticalContour[x - i][1]);
            }
        }
        // get the slopes
        double topRightSlope = topRightRegression.getSlope();
        double bottomRightSlope = bottomRightRegression.getSlope();
        double topLeftSlope = topLeftRegression.getSlope();
        double bottomLeftSlope = bottomLeftRegression.getSlope();
        // convert slopes to angles
        double topRightAngle = Math.atan(topRightSlope);
        double bottomRightAngle = Math.atan(bottomRightSlope);
        double topLeftAngle = Math.atan(topLeftSlope);
        double bottomLeftAngle = Math.atan(bottomLeftSlope);
        // calculate the right & left-hand differences
        double rightDiff = Math.abs(topRightAngle - bottomRightAngle);
        double leftDiff = Math.abs(topLeftAngle - bottomLeftAngle);
        // normalise the differences from 0 to 1
        rightDiff = rightDiff / Math.PI;
        leftDiff = leftDiff / Math.PI;
        double product = rightDiff * leftDiff;
        if (LOG.isTraceEnabled()) {
            LOG.trace("topRightAngle: " + topRightAngle);
            LOG.trace("bottomRightAngle: " + bottomRightAngle);
            LOG.trace("topLeftAngle: " + topLeftAngle);
            LOG.trace("bottomLeftAngle: " + bottomLeftAngle);
            LOG.trace("rightDiff: " + rightDiff);
            LOG.trace("leftDiff: " + leftDiff);
            LOG.trace("product: " + product);
        }
        result = this.generateResult(product);
    }
    return result;
}
Also used : Shape(com.joliciel.jochre.graphics.Shape) SimpleRegression(org.apache.commons.math.stat.regression.SimpleRegression)

Example 35 with Shape

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

the class ImageController method save.

void save() {
    try {
        Comboitem selectedItem = cmbStatus.getSelectedItem();
        ImageStatus imageStatus = ImageStatus.forId((Integer) selectedItem.getValue());
        currentImage.setImageStatus(imageStatus);
        if (currentUser.getRole().equals(UserRole.ADMIN)) {
            User owner = (User) cmbOwner.getSelectedItem().getValue();
            currentImage.setOwner(owner);
        }
        GraphicsDao graphicsDao = GraphicsDao.getInstance(jochreSession);
        graphicsDao.saveJochreImage(currentImage);
        for (Paragraph paragraph : currentImage.getParagraphs()) {
            LOG.trace("Paragraph " + paragraph.getIndex() + ", " + paragraph.getRows().size() + " rows");
            for (RowOfShapes row : paragraph.getRows()) {
                List<List<String>> letterGroups = this.getLetterGroups(row);
                LOG.trace("Row " + row.getIndex() + ", " + row.getGroups().size() + " groups, " + letterGroups.size() + " letter groups");
                Iterator<List<String>> iLetterGroups = letterGroups.iterator();
                for (GroupOfShapes group : row.getGroups()) {
                    LOG.trace("Group " + group.getIndex() + " text : " + group.getWord());
                    boolean hasChange = false;
                    List<String> letters = null;
                    if (iLetterGroups.hasNext())
                        letters = iLetterGroups.next();
                    else
                        letters = new ArrayList<String>();
                    LOG.trace("Found " + letters.size() + " letters in text");
                    Iterator<String> iLetters = letters.iterator();
                    for (Shape shape : group.getShapes()) {
                        String currentLetter = shape.getLetter();
                        if (currentLetter == null)
                            currentLetter = "";
                        String newLetter = "";
                        if (iLetters.hasNext())
                            newLetter = iLetters.next();
                        if (newLetter.startsWith("[") && newLetter.endsWith("]")) {
                            newLetter = newLetter.substring(1, newLetter.length() - 1);
                        }
                        LOG.trace("currentLetter:  " + currentLetter + ", newLetter: " + newLetter);
                        if (!currentLetter.equals(newLetter)) {
                            LOG.trace("newLetter: " + newLetter);
                            shape.setLetter(newLetter);
                            shape.save();
                            hasChange = true;
                        }
                    }
                    if (hasChange)
                        LOG.trace("Group text after : " + group.getWord());
                }
            // next group
            }
        // next row
        }
        // next paragraph
        Messagebox.show(Labels.getLabel("button.saveComplete"));
    } catch (Exception e) {
        LOG.error("Failure in save", e);
        throw new RuntimeException(e);
    }
}
Also used : User(com.joliciel.jochre.security.User) Shape(com.joliciel.jochre.graphics.Shape) ImageStatus(com.joliciel.jochre.graphics.ImageStatus) ArrayList(java.util.ArrayList) RowOfShapes(com.joliciel.jochre.graphics.RowOfShapes) Paragraph(com.joliciel.jochre.graphics.Paragraph) GraphicsDao(com.joliciel.jochre.graphics.GraphicsDao) GroupOfShapes(com.joliciel.jochre.graphics.GroupOfShapes) Comboitem(org.zkoss.zul.Comboitem) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Shape (com.joliciel.jochre.graphics.Shape)74 ArrayList (java.util.ArrayList)22 GroupOfShapes (com.joliciel.jochre.graphics.GroupOfShapes)14 JochreImage (com.joliciel.jochre.graphics.JochreImage)13 Paragraph (com.joliciel.jochre.graphics.Paragraph)9 RowOfShapes (com.joliciel.jochre.graphics.RowOfShapes)9 Decision (com.joliciel.talismane.machineLearning.Decision)8 Test (org.junit.Test)8 JochreSession (com.joliciel.jochre.JochreSession)7 JochrePage (com.joliciel.jochre.doc.JochrePage)7 Config (com.typesafe.config.Config)7 TreeSet (java.util.TreeSet)7 JochreDocument (com.joliciel.jochre.doc.JochreDocument)6 BufferedImage (java.awt.image.BufferedImage)6 ShapeInSequence (com.joliciel.jochre.boundaries.ShapeInSequence)5 ShapeSequence (com.joliciel.jochre.boundaries.ShapeSequence)5 GraphicsDao (com.joliciel.jochre.graphics.GraphicsDao)5 RuntimeEnvironment (com.joliciel.talismane.machineLearning.features.RuntimeEnvironment)5 SplitFeature (com.joliciel.jochre.boundaries.features.SplitFeature)4 JochreException (com.joliciel.jochre.utils.JochreException)4