Skip to content

Commit

Permalink
gs-cv: AbstractField: replaced private Rect with GSRect (coordinates …
Browse files Browse the repository at this point in the history
…as double instead of int)
  • Loading branch information
plassalas committed Nov 10, 2017
1 parent f1b745d commit 57823ea
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 163 deletions.
Expand Up @@ -40,8 +40,9 @@
import org.genericsystem.cv.utils.ImgFunction;
import org.genericsystem.cv.utils.ModelTools;
import org.genericsystem.cv.utils.NativeLibraryLoader;
import org.genericsystem.cv.utils.RectToolsMapper;
import org.genericsystem.kernel.Engine;
import org.opencv.core.Rect;
import org.genericsystem.reinforcer.tools.GSRect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -116,7 +117,7 @@ public static boolean registerNewFile(Root engine, Path relativeImgPath, Path ba

public static JsonObject detectFields(Path imgPath) {
try (Img deskewed = new Img(imgPath.toString())) {
List<Rect> rects = ClassifierUsingFields.detectRects(deskewed);
List<GSRect> rects = RectToolsMapper.rectToGSRect(ClassifierUsingFields.detectRects(deskewed));
DocFields fields = DocFields.of(rects);
JsonObject result = fields.toJsonObject();
return result;
Expand Down
Expand Up @@ -16,6 +16,8 @@
import org.genericsystem.cv.utils.OCRPlasty.RANSAC;
import org.genericsystem.cv.utils.OCRPlasty.Tuple;
import org.genericsystem.cv.utils.RectToolsMapper;
import org.genericsystem.reinforcer.tools.GSRect;
import org.genericsystem.reinforcer.tools.RectangleTools;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint2f;
Expand All @@ -34,7 +36,7 @@ public abstract class AbstractField {
protected static final int MIN_SIZE_CONSOLIDATION = 5;
private static final int OCR_CONFIDENCE_THRESH = 0;

protected Rect rect;
protected GSRect rect;
protected Point center;
protected Map<String, Integer> labels;
protected String consolidated;
Expand All @@ -44,10 +46,10 @@ public abstract class AbstractField {
protected int deadCounter;

public AbstractField() {
this(new Rect());
this(new GSRect());
}

public AbstractField(Rect rect) {
public AbstractField(GSRect rect) {
updateRect(rect);
this.labels = new HashMap<>();
this.consolidated = null;
Expand All @@ -65,9 +67,9 @@ public AbstractField(AbstractField other) {
this.deadCounter = other.getDeadCounter();
}

void updateRect(Rect rect) {
void updateRect(GSRect rect) {
this.rect = rect;
this.center = new Point(rect.x + rect.width / 2, rect.y + rect.height / 2);
this.center = new Point(rect.getX() + rect.getWidth() / 2, rect.getY() + rect.getHeight() / 2);
}

public void ocr(Img rootImg) {
Expand Down Expand Up @@ -124,7 +126,8 @@ public void drawOcrPerspectiveInverse(Img display, Mat homography, Scalar color,
}

public void drawRect(Img stabilizedDisplay, Scalar color, int thickness) {
drawRect(stabilizedDisplay, RectToolsMapper.decomposeClockwise(rect), color, thickness);
Point[] points = RectToolsMapper.gsPointToPoint(Arrays.asList(rect.decomposeClockwise())).toArray(new Point[0]);
drawRect(stabilizedDisplay, points, color, thickness);
}

public void drawRect(Img display, Point[] targets, Scalar color, int thickness) {
Expand All @@ -146,65 +149,61 @@ public void drawText(Img display, Point[] targets, Scalar color, int thickness)
}

protected Point[] getRectPointsWithHomography(Mat homography) {
List<Point> points = Arrays.asList(RectToolsMapper.decomposeClockwise(rect));
List<Point> points = RectToolsMapper.gsPointToPoint(Arrays.asList(rect.decomposeClockwise()));
MatOfPoint2f results = new MatOfPoint2f();
Core.perspectiveTransform(Converters.vector_Point2f_to_Mat(points), results, homography);
return results.toArray();
}

public Rect getLargeRect(Img imgRoot, double deltaW, double deltaH) {
int adjustW = 3 + Double.valueOf(Math.floor(rect.width * deltaW)).intValue();
int adjustH = 3 + Double.valueOf(Math.floor(rect.height * deltaH)).intValue();
Point tl = new Point(rect.tl().x - adjustW > 0 ? rect.tl().x - adjustW : 0, rect.tl().y - adjustH > 0 ? rect.tl().y - adjustH : 0);
Point br = new Point(rect.br().x + adjustW > imgRoot.width() ? imgRoot.width() : rect.br().x + adjustW, rect.br().y + adjustH > imgRoot.height() ? imgRoot.height() : rect.br().y + adjustH);
int adjustW = 3 + Double.valueOf(Math.floor(rect.getWidth() * deltaW)).intValue();
int adjustH = 3 + Double.valueOf(Math.floor(rect.getHeight() * deltaH)).intValue();
Point tl = new Point(rect.tl().getX() - adjustW > 0 ? rect.tl().getX() - adjustW : 0, rect.tl().getY() - adjustH > 0 ? rect.tl().getY() - adjustH : 0);
Point br = new Point(rect.br().getX() + adjustW > imgRoot.width() ? imgRoot.width() : rect.br().getX() + adjustW, rect.br().getY() + adjustH > imgRoot.height() ? imgRoot.height() : rect.br().getY() + adjustH);
return new Rect(tl, br);
}

public boolean contains(Point center) {
return Math.sqrt(Math.pow(this.center.x - center.x, 2) + Math.pow(this.center.y - center.y, 2)) <= 10;
}

public boolean isOverlapping(Rect otherRect) {
return RectToolsMapper.isOverlapping(this.rect, otherRect);
public boolean isOverlapping(GSRect otherRect) {
return this.rect.isOverlapping(otherRect);
}

public boolean isOverlapping(AbstractField other) {
return isOverlapping(other.getRect());
}

public boolean isIn(AbstractField other) {
return RectToolsMapper.getInsider(rect, other.getRect()).map(r -> r.equals(rect) ? true : false).orElse(false);
return rect.getInsider(other.getRect()).map(r -> r.equals(rect) ? true : false).orElse(false);
}

public boolean overlapsMoreThanThresh(Rect otherRect, double overlapThreshold) {
return RectToolsMapper.inclusiveArea(this.rect, otherRect) > overlapThreshold;
public boolean overlapsMoreThanThresh(GSRect otherRect, double overlapThreshold) {
return this.rect.inclusiveArea(otherRect) > overlapThreshold;
}

public boolean isClusteredWith(Rect otherRect, double epsilon) {
return RectToolsMapper.isInCluster(this.rect, otherRect, epsilon);
public boolean isClusteredWith(GSRect otherRect, double epsilon) {
return RectangleTools.isInCluster(this.rect, otherRect, epsilon);
}

public boolean isClusteredWith(Rect otherRect, double epsilon, int sides) {
return RectToolsMapper.isInCluster(this.rect, otherRect, epsilon, sides);
public boolean isClusteredWith(GSRect otherRect, double epsilon, int sides) {
return RectangleTools.isInCluster(this.rect, otherRect, epsilon, sides);
}

public boolean overlapsMoreThanThresh(AbstractField other, double overlapThreshold) {
return overlapsMoreThanThresh(other.getRect(), overlapThreshold);
}

public boolean isOnDisplay(Img display) {
Rect imgRect = new Rect(0, 0, display.width(), display.height());
return RectToolsMapper.isOverlapping(imgRect, this.rect);
GSRect imgRect = new GSRect(0, 0, display.width(), display.height());
return imgRect.isOverlapping(this.rect);
}

public boolean isConsolidated() {
return consolidated != null;
}

// public boolean needOcr() {
// return ThreadLocalRandom.current().nextBoolean();
// }

public void incrementDeadCounter() {
deadCounter++;
}
Expand Down Expand Up @@ -233,7 +232,7 @@ public Point getCenter() {
return center;
}

public Rect getRect() {
public GSRect getRect() {
return rect;
}

Expand Down
Expand Up @@ -7,9 +7,9 @@
import java.util.stream.Stream;

import org.genericsystem.cv.Img;
import org.genericsystem.reinforcer.tools.GSRect;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;

public abstract class AbstractFields<F extends AbstractField> implements Iterable<F> {
Expand All @@ -35,12 +35,12 @@ protected List<F> findClusteredFields(F field, double epsilon) {
return fields.stream().filter(f -> f.isClusteredWith(field.getRect(), epsilon)).collect(Collectors.toList());
}

protected List<F> findPossibleMatches(Rect rect, double epsilon) {
protected List<F> findPossibleMatches(GSRect rect, double epsilon) {
return fields.stream().filter(f -> f.isClusteredWith(rect, epsilon)).collect(Collectors.toList());
}

// like findPossibleMatches(Rect, double) but will match only a number of sides (e.g., 3 instead of 4 sides)
protected List<F> findPossibleMatches(Rect rect, double epsilon, int sides) {
protected List<F> findPossibleMatches(GSRect rect, double epsilon, int sides) {
return fields.stream().filter(f -> f.isClusteredWith(rect, epsilon, sides)).collect(Collectors.toList());
}

Expand Down
Expand Up @@ -4,9 +4,9 @@

import org.genericsystem.cv.Img;
import org.genericsystem.cv.utils.ModelTools;
import org.genericsystem.reinforcer.tools.GSRect;
import org.opencv.core.Core;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

Expand All @@ -18,14 +18,14 @@ public DocField() {
super();
}

public DocField(int num, Rect rect) {
public DocField(int num, GSRect rect) {
super(rect);
this.num = num;
this.uid = ModelTools.generateZoneUID(rect);
}

public void writeNum(Img img, String text, double fontScale, Scalar color, int thickness) {
Imgproc.putText(img.getSrc(), text, new Point(rect.tl().x, rect.br().y), Core.FONT_HERSHEY_PLAIN, fontScale, color, thickness);
Imgproc.putText(img.getSrc(), text, new Point(rect.tl().getX(), rect.br().getY()), Core.FONT_HERSHEY_PLAIN, fontScale, color, thickness);
}

public void annotateImage(Img annotated, double fontScale, Scalar color, int thickness) {
Expand All @@ -45,7 +45,7 @@ public String getUid() {

// The private setters are needed by Jackson to serialize/de-serialize the JSON objects

protected void setRect(Rect rect) {
protected void setRect(GSRect rect) {
updateRect(rect);
this.uid = ModelTools.generateZoneUID(rect);
}
Expand Down
Expand Up @@ -8,7 +8,7 @@

import org.genericsystem.cv.Img;
import org.genericsystem.cv.utils.ParallelTasks;
import org.opencv.core.Rect;
import org.genericsystem.reinforcer.tools.GSRect;
import org.opencv.core.Scalar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,7 +30,7 @@ public DocFields(List<DocField> fields) {
super(fields);
}

public static DocFields of(List<Rect> rects) {
public static DocFields of(List<GSRect> rects) {
DocFields fields = new DocFields();
fields.buildFields(rects);
return fields;
Expand All @@ -56,7 +56,7 @@ public JsonObject toJsonObject() {
return new JsonObject().put(FIELDS, fields);
}

public void buildFields(List<Rect> rects) {
public void buildFields(List<GSRect> rects) {
int[] counter = new int[] { 0 };
fields = rects.stream().map(rect -> new DocField(counter[0]++, rect)).collect(Collectors.toList());
}
Expand Down
7 changes: 3 additions & 4 deletions gs-cv/src/main/java/org/genericsystem/cv/retriever/Field.java
Expand Up @@ -9,9 +9,8 @@
import java.util.stream.Collectors;

import org.genericsystem.cv.Img;
import org.genericsystem.cv.utils.RectToolsMapper;
import org.genericsystem.reinforcer.tools.GSRect;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;

public class Field extends AbstractField {
Expand All @@ -24,7 +23,7 @@ public class Field extends AbstractField {
private static final double CONFIDENCE_THRESHOLD = 0.92;
private boolean locked = false;

public Field(Rect rect) {
public Field(GSRect rect) {
super(rect);
this.parent = null;
this.children = new HashSet<>();
Expand Down Expand Up @@ -95,7 +94,7 @@ public boolean removeChild(Field child) {
}

public boolean containsChild(Field field) {
return children.stream().anyMatch(child -> RectToolsMapper.inclusiveArea(child.getRect(), field.getRect()) > 0.95);
return children.stream().anyMatch(child -> child.getRect().inclusiveArea(field.getRect()) > 0.95);
}

public boolean addChildren(Collection<Field> children) {
Expand Down

0 comments on commit 57823ea

Please sign in to comment.