RED-5381: Fixed calculation of bodyTextFrame

This commit is contained in:
deiflaender 2022-10-17 16:56:00 +02:00
parent c23dd86b6d
commit 4be5487e64
7 changed files with 91 additions and 168 deletions

View File

@ -1,8 +1,5 @@
package com.iqser.red.service.redaction.v1.model;
import com.dslplatform.json.JsonAttribute;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -18,100 +15,4 @@ public class Rectangle {
private int page;
@JsonIgnore
@JsonAttribute(ignore = true)
public float getX1(int dir, float pageWidth, float pageHeight) {
if (dir == 90) {
return topLeft.getX();
}
else if (dir == 180) {
return pageWidth - topLeft.getX() + width;
}
else if (dir == 270) {
return topLeft.getY() + height;
}
else {
return topLeft.getX();
}
}
@JsonIgnore
@JsonAttribute(ignore = true)
public float getX2(int dir, float pageWidth, float pageHeight) {
if (dir == 90) {
return topLeft.getX() + width;
}
else if (dir == 180) {
return pageWidth - topLeft.getX();
}
else if (dir == 270) {
return topLeft.getY() ;
}
else {
return topLeft.getX() + width;
}
}
@JsonIgnore
@JsonAttribute(ignore = true)
public float getY1(int dir, float pageWidth, float pageHeight) {
if (dir == 90 ) {
return topLeft.getY();
}
else if (dir == 180) {
return pageHeight - topLeft.getY() + height;
}
else if (dir == 270) {
return pageHeight - topLeft.getX() ;
}
else {
return topLeft.getY();
}
}
@JsonIgnore
@JsonAttribute(ignore = true)
public float getY2(int dir, float pageWidth, float pageHeight) {
if (dir == 90) {
return topLeft.getY() + height;
}
else if (dir == 180 ) {
return pageHeight - topLeft.getY();
}
else if (dir == 270 ) {
return pageHeight -topLeft.getX() - width;
}
else {
return topLeft.getY() + height;
}
}
}

View File

@ -61,7 +61,7 @@ public class BlockificationService {
|| isSplittedByRuling(minX, minY, word.getX1(), word.getY2(), verticalRulingLines, word.getDir().getDegrees(), cropbox.getWidth(), cropbox.getHeight());
boolean splitByDir = prev != null && !prev.getDir().equals(word.getDir());
if (prev != null && (lineSeparation || startFromTop || xIsBeforeFirstX || splitByX || splitByDir || startFromTop || splittedByRuling)) {
if (prev != null && (lineSeparation || startFromTop || xIsBeforeFirstX || splitByX || splitByDir || splittedByRuling)) {
Orientation prevOrientation = null;
if (!chunkBlockList1.isEmpty()) {
@ -332,32 +332,20 @@ public class BlockificationService {
if (documentFontSizeCounter.getMostPopular() != null) {
if (textBlock.getMostPopularWordFontSize() >= documentFontSizeCounter.getMostPopular()) {
if (textBlock.getX1() < minX) {
minX = textBlock.getX1();
}
if (textBlock.getX2() > maxX) {
maxX = textBlock.getX2();
}
if (textBlock.getY1() < minY) {
minY = textBlock.getY1();
}
if (textBlock.getY2() > maxY) {
maxY = textBlock.getY2();
}
}
}
}
if (container instanceof Table) {
Table table = (Table) container;
for (List<Cell> row : table.getRows()) {
for (Cell cell : row) {
if (cell == null || cell.getTextBlocks() == null) {
continue;
}
for (TextBlock textBlock : cell.getTextBlocks()) {
if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() != 0){
if (textBlock.getY1() < minX) {
minX = textBlock.getY1();
}
if (textBlock.getY2() > maxX) {
maxX = textBlock.getY2();
}
if (textBlock.getX1() < minY) {
minY = textBlock.getX1();
}
if (textBlock.getX2() > maxY) {
maxY = textBlock.getX2();
}
} else {
if (textBlock.getX1() < minX) {
minX = textBlock.getX1();
}
@ -374,30 +362,56 @@ public class BlockificationService {
}
}
}
if (container instanceof Table) {
Table table = (Table) container;
for (List<Cell> row : table.getRows()) {
for (Cell cell : row) {
if (cell == null || cell.getTextBlocks() == null) {
continue;
}
for (TextBlock textBlock : cell.getTextBlocks()) {
if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() != 0){
if (textBlock.getY1() < minX) {
minX = textBlock.getMinY();
}
if (textBlock.getY2() > maxX) {
maxX = textBlock.getY2();
}
if (textBlock.getX1() < minY) {
minY = textBlock.getX1();
}
if (textBlock.getX2() > maxY) {
maxY = textBlock.getX2();
}
} else {
if (textBlock.getX1() < minX) {
minX = textBlock.getX1();
}
if (textBlock.getX2() > maxX) {
maxX = textBlock.getX2();
}
if (textBlock.getY1() < minY) {
minY = textBlock.getY1();
}
if (textBlock.getY2() > maxY) {
maxY = textBlock.getY2();
}
}
}
}
}
}
}
System.out.println("Page: " + page.getPageNumber() + " Landscape: " + page.isLandscape()+ " MinX: " + minX + " MaxX: " + maxX + " MinY: " +minY + " MaxY: " + maxY);
}
return new Rectangle(new Point(minX, minY), maxX - minX, maxY - minY, 0);
}
private void sortRotatedSequences(List<TextPositionSequence> sequences) {
List<TextPositionSequence> rotatedWords = new ArrayList<>();
Iterator<TextPositionSequence> itty = sequences.iterator();
while (itty.hasNext()) {
var pos = itty.next();
if (pos.getTextPositions().get(0).getDir() == 270) {
rotatedWords.add(pos);
itty.remove();
}
}
if (!rotatedWords.isEmpty() && !sequences.isEmpty()) {
rotatedWords.sort(Comparator.comparing(TextPositionSequence::getX1));
}
sequences.addAll(rotatedWords);
}
private double round(float value, int decimalPoints) {

View File

@ -34,19 +34,21 @@ public class ClassificationService {
for (Page page : document.getPages()) {
Rectangle btf = page.isLandscape() ? landscapeBodyTextFrame : bodyTextFrame;
if (!page.isLandscape() && (page.getRotation() == 90 || page.getRotation() == 270)){
var width = btf.getX2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getX1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
var height = btf.getY2(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight()) - btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
var x1= btf.getX1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
var y1 = btf.getY1(page.getRotation(), page.getCropBox().getWidth(), page.getCropBox().getHeight());
if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() == 270) {
page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getY(),page.getCropBox().getHeight() - btf.getTopLeft().getX()), btf.getHeight(), -btf.getWidth(), 0));
}
else if(page.getCropBox().getWidth() > page.getCropBox().getHeight() && page.getRotation() != 0) {
page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getY(), btf.getTopLeft().getX()), btf.getHeight(), btf.getWidth(), 0));
}
else if(page.getRotation() == 180){
page.setBodyTextFrame(new Rectangle(new Point( btf.getTopLeft().getX(), page.getCropBox().getHeight() - btf.getTopLeft().getY()), btf.getWidth(), -btf.getHeight(), 0));
}
else {
page.setBodyTextFrame(btf);
}
page.setBodyTextFrame(new Rectangle(new Point(x1, y1), width, height,0));
} else if (page.getRotation() == 180) {
page.setBodyTextFrame(new Rectangle(new Point(btf.getTopLeft().getX(), page.getCropBox().getHeight() - btf.getTopLeft().getY() -btf.getHeight()), btf.getWidth(), btf.getHeight(),0));
} else {
page.setBodyTextFrame(btf);
}

View File

@ -112,6 +112,8 @@ public class PdfSegmentationService {
float cropboxArea = cropbox.getHeight() * cropbox.getWidth();
page.setCropBoxArea(cropboxArea);
System.out.println("CropBox: w:" + cropbox.getWidth() + " h:" +cropbox.getHeight() + "MediaBox: w:" + pdPage.getMediaBox().getWidth() + " h:" +pdPage.getMediaBox().getHeight());
page.setRotation(rotation);
page.setLandscape(isLandscape);
page.setPageNumber(pageNumber);

View File

@ -79,10 +79,13 @@ public class PdfVisualisationService {
}
contentStream.setStrokingColor(Color.YELLOW);
// var widhth = analyzedPage.getBodyTextFrame().getX2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight());
// var width = analyzedPage.getBodyTextFrame().getX2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight());
// var height = analyzedPage.getBodyTextFrame().getY2(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()) - analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight());
//
// contentStream.addRect(analyzedPage.getBodyTextFrame().getX1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()), analyzedPage.getBodyTextFrame().getY1(pdPage.getRotation(), pdPage.getCropBox().getWidth(), pdPage.getCropBox().getHeight()), width, height);
contentStream.addRect(analyzedPage.getBodyTextFrame().getTopLeft().getX(), analyzedPage.getBodyTextFrame().getTopLeft().getY(), analyzedPage.getBodyTextFrame().getWidth(), analyzedPage.getBodyTextFrame().getHeight());
contentStream.addRect(analyzedPage.getBodyTextFrame().getTopLeft().getX(), analyzedPage.getBodyTextFrame().getTopLeft().getY(), analyzedPage.getBodyTextFrame().getWidth(), analyzedPage.getBodyTextFrame().getHeight());
contentStream.stroke();
contentStream.close();
@ -97,17 +100,18 @@ public class PdfVisualisationService {
contentStream.addRect(textBlock.getX1(), textBlock.getY1(), textBlock.getX2() - textBlock.getX1(), textBlock.getY2() - textBlock.getY1());
contentStream.stroke();
// if (textBlock.getClassification() != null) {
// contentStream.beginText();
//
// contentStream.setFont(PDType1Font.TIMES_ROMAN, 6f);
//
// contentStream.newLineAtOffset(textBlock.getX1(), textBlock.getY2() + 2);
// contentStream.showText(textBlock.getClassification() + textBlock.getOrientation() + "-->" + textBlock.getSequences().get(0).getDir());
//
// contentStream.endText();
//
//
if (textBlock.getClassification() != null) {
contentStream.beginText();
contentStream.setNonStrokingColor(Color.BLUE);
contentStream.setFont(PDType1Font.TIMES_ROMAN, 9f);
contentStream.newLineAtOffset(textBlock.getX1(), textBlock.getY2() + 2);
contentStream.showText(textBlock.getClassification() + textBlock.getOrientation() + "-->" + textBlock.getSequences().get(0).getDir());
contentStream.endText();
// contentStream.setNonStrokingColor(Color.BLUE);
// contentStream.setFont(PDType1Font.TIMES_ROMAN, 2f);
//
@ -131,7 +135,7 @@ public class PdfVisualisationService {
//
//
// contentStream.endText();
// }
}
}

View File

@ -1098,7 +1098,7 @@ public class RedactionIntegrationTest {
System.out.println("classificationTest");
AnalyzeRequest request = prepareStorage("files/Metolachlor/S-Metolachlor_RAR_01_Volume_1_2018-09-06.pdf");
AnalyzeRequest request = prepareStorage("files/Primicarb/74 Pirimicarb_RAR_01_Volume_1_2017-12-04.pdf");
RedactionRequest redactionRequest = RedactionRequest.builder()
.dossierId(request.getDossierId())