Import redaction fixes

This commit is contained in:
deiflaender 2022-02-08 14:37:03 +01:00
parent b09f036253
commit 350de7f385
3 changed files with 31 additions and 33 deletions

View File

@ -58,7 +58,7 @@ public class RedactionExtractorService {
Map<Integer, List<ImportedRedaction>> importedRedactionsMap = new HashMap<>();
for (int pageNumber = 1; pageNumber <= pageCount; pageNumber++) {
PDPage page = pdfDocument.getPage(pageNumber - 1);
BufferedImage image = pageConvertToImage(pdfDocument, page, 144, ImageType.GRAY);
BufferedImage image = pageConvertToImage(pdfDocument, page, 200, ImageType.GRAY);
Mat imageAsMat = bufferedImage2Mat(image);
@ -74,7 +74,7 @@ public class RedactionExtractorService {
Mat blurred = new Mat();
Imgproc.GaussianBlur(imageAsMatINv,blurred, new Size(5, 5), 1);
Mat thresh = new Mat();
Imgproc.threshold(blurred, thresh, 250, 255, Imgproc.THRESH_BINARY);
Imgproc.threshold(blurred, thresh, 240, 255, Imgproc.THRESH_BINARY);
Mat hierachy = new Mat();
@ -85,12 +85,12 @@ public class RedactionExtractorService {
System.out.println(contours);
System.out.println(hierachy.size(3));
var min_nomralized_area=600;
var min_nomralized_area=800;
hierachy = hierachy.row(0).row(0);
System.out.println("Cont" +contours.size() + "Hier" + hierachy.cols());
if(hierachy.rows() > 0 && hierachy.row(0).rows() > 0) {
hierachy = hierachy.row(0).row(0);
System.out.println("Cont" + contours.size() + "Hier" + hierachy.cols());
// for(int j = 0; j<= hierachy.cols(); j++){
// System.out.println("----> Col " + j);
@ -99,36 +99,31 @@ public class RedactionExtractorService {
// }
// }
int i = 0;
List<MatOfPoint> filterd = new ArrayList<>();
for (MatOfPoint contour : contours) {
if (isLikelyRedaction(contour, hierachy.get(0, i), min_nomralized_area)) {
filterd.add(contour);
}
i++;
}
Imgproc.drawContours(imageAsMat, filterd, -1, new Scalar(180), 2);
int i = 0;
List<MatOfPoint> filterd = new ArrayList<>();
for(MatOfPoint contour: contours){
if(isLikelyRedaction(contour, hierachy.get(0,i), min_nomralized_area)){
filterd.add(contour);
}
i++;
}
List<ImportedRedaction> importedRedactions = new ArrayList<>();
for (MatOfPoint contour : filterd) {
Rect rect = Imgproc.boundingRect(contour);
importedRedactions.add(new ImportedRedaction(UUID.randomUUID().toString(), List.of(new Rectangle(new Point((float) (rect.x * page.getCropBox()
.getWidth() / image.getWidth()), (float) (page.getCropBox().getHeight() - rect.y * page.getCropBox()
.getWidth() / image.getWidth())), rect.width * page.getCropBox()
.getWidth() / image.getWidth(), -rect.height * page.getCropBox()
.getWidth() / image.getWidth(), pageNumber))));
}
Imgproc.drawContours(imageAsMat, filterd, -1, new Scalar(180), 2);
List<ImportedRedaction> importedRedactions = new ArrayList<>();
for(MatOfPoint contour : filterd){
Rect rect = Imgproc.boundingRect(contour);
importedRedactions.add(new ImportedRedaction(UUID.randomUUID().toString(), List.of(
new Rectangle(new Point((float) (rect.x * page.getCropBox().getWidth() / image.getWidth()),
(float) (page.getCropBox().getHeight() - rect.y * page.getCropBox().getWidth() / image.getWidth())),
rect.width * page.getCropBox().getWidth() / image.getWidth(),
-rect.height * page.getCropBox().getWidth() / image.getWidth(),
1))));
}
importedRedactionsMap.put(1, importedRedactions);
importedRedactionsMap.put(pageNumber, importedRedactions);
}
// BufferedImage gray = new BufferedImage(imageAsMat.width(), imageAsMat.height(), BufferedImage.TYPE_BYTE_GRAY);
// byte[] data = ((DataBufferByte) gray.getRaster().getDataBuffer()).getData();
@ -141,6 +136,8 @@ public class RedactionExtractorService {
// System.out.println(contours);
}
pdfDocument.close();
return new ImportedRedactions(importedRedactionsMap);
}

View File

@ -567,15 +567,16 @@ public class RedactionIntegrationTest {
public void testImportRedactions() {
nu.pattern.OpenCV.loadShared();
ClassPathResource pdfFileResource = new ClassPathResource("files/redest_man_page.pdf");
ClassPathResource pdfFileResource = new ClassPathResource("files/Metolachlor/S-Metolachlor_RAR_01_Volume_1_2018-09-06_redacted1.pdf");
ImportedRedactions importedRedactions = redactionExtractorService.extractRedaction(pdfFileResource.getInputStream());
String outputFileName = OsUtils.getTemporaryDirectory() + "/Annotated.pdf";
ClassPathResource pdfFileResource2 = new ClassPathResource("files/Metolachlor/S-Metolachlor_RAR_01_Volume_1_2018-09-06.pdf");
AnalyzeRequest request = prepareStorage(pdfFileResource.getInputStream());
AnalyzeRequest request = prepareStorage(pdfFileResource2.getInputStream());
storageService.storeObject(RedactionStorageService.StorageIdUtils.getStorageId(TEST_DOSSIER_ID, TEST_FILE_ID, FileType.IMPORTED_REDACTIONS), objectMapper.writeValueAsBytes(importedRedactions));
analyzeService.analyzeDocumentStructure(new StructureAnalyzeRequest(request.getDossierId(), request.getFileId()));