Skip to content

Commit

Permalink
Merge pull request #255 from OCR4all/ocrd_library
Browse files Browse the repository at this point in the history
Ocrd library
  • Loading branch information
maxnth authored Jul 14, 2021
2 parents af4bb73 + ff84e75 commit d675aab
Show file tree
Hide file tree
Showing 24 changed files with 1,313 additions and 140 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@
<version>4.3.0-2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>

<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/de/uniwue/web/communication/DirectRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package de.uniwue.web.communication;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import de.uniwue.web.model.PageAnnotations;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Communication object for the library to request the editor
*
*/
public class DirectRequest {
private String imagemapString;
private String customFlag;
private String customFolder;

@JsonCreator
public DirectRequest(String imagemapString,
String customFlag,
String customFolder) {
this.imagemapString = imagemapString;
this.customFlag = customFlag;
if(customFolder != null) {
this.customFolder = customFolder;
} else {
this.customFolder = "";
}
}

public String getImagemapString() {
return imagemapString;
}

public String getCustomFlag() {
return customFlag;
}

public String getCustomFolder() {
return customFolder;
}

public void setImagemapString(String imagemapString) {
this.imagemapString = imagemapString;
}

public void setCustomFlag(String customFlag) {
this.customFlag = customFlag;
}

public void setCustomFolder(String customFolder) {
this.customFolder = customFolder;
}
}
64 changes: 51 additions & 13 deletions src/main/java/de/uniwue/web/controller/DataController.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ private void init() {
@RequestMapping(value = "data/book", method = RequestMethod.POST)
public @ResponseBody Book getBook(@RequestParam("bookid") int bookID) {
FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
config.getListSetting("imagefilter"), fileManager.checkFlat());
if(fileManager.checkFlat()) {
return database.getBook(bookID);
} else {
return database.getBook(fileManager.getNonFlatBookName(),fileManager.getNonFlatBookId(),fileManager.getLocalImageMap(), fileManager.getLocalXmlMap());
}

return database.getBook(bookID);
}

/**
Expand All @@ -85,16 +89,26 @@ private void init() {
@RequestMapping(value = "data/page/annotations", method = RequestMethod.POST)
public @ResponseBody PageAnnotations getAnnotations(@RequestParam("bookid") int bookID, @RequestParam("pageid") int pageID) {
FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
config.getListSetting("imagefilter"), fileManager.checkFlat());

final Book book = database.getBook(bookID);
final Page page = book.getPage(pageID);
final File annotationsPath = fileManager.getAnnotationPath(book.getName(), page.getName());
Book book;
Page page;
File annotationsPath;

if(fileManager.checkFlat()) {
book = database.getBook(bookID);
page = book.getPage(pageID);
annotationsPath = fileManager.getAnnotationPath(book.getName(), page.getName());
} else {
book = database.getBook(fileManager.getNonFlatBookName(),fileManager.getNonFlatBookId(),fileManager.getLocalImageMap(), fileManager.getLocalXmlMap());
page = book.getPage(pageID);
annotationsPath = new File(fileManager.getLocalXmlMap().get(page.getXmlName().split("\\.")[0]));
}

if (annotationsPath.exists()) {
return PageXMLReader.loadPageAnnotationsFromDisc(annotationsPath);
} else {
return new PageAnnotations(page.getName(), page.getWidth(), page.getHeight(),
return new PageAnnotations(page.getName(), page.getXmlName(), page.getWidth(), page.getHeight(),
page.getId(), page.getOrientation(), false);
}
}
Expand All @@ -109,17 +123,27 @@ private void init() {
produces = "application/json", consumes = "application/json")
public @ResponseBody List<PageAnnotations> getBatchAnnotations(@RequestBody BatchLoadRequest batchLoadRequest) {
FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
config.getListSetting("imagefilter"), fileManager.checkFlat());

final Book book = database.getBook(batchLoadRequest.getBookid());
Book book;
if(fileManager.checkFlat()) {
book = database.getBook(batchLoadRequest.getBookid());
} else {
book = database.getBook(fileManager.getNonFlatBookName(),fileManager.getNonFlatBookId(),fileManager.getLocalImageMap(), fileManager.getLocalXmlMap());
}
List<PageAnnotations> pageAnnotations = new ArrayList<>();
for( int pageID : batchLoadRequest.getPages()) {
Page page = book.getPage(pageID);
File annotationsPath = fileManager.getAnnotationPath(book.getName(), page.getName());
File annotationsPath;
if(fileManager.checkFlat()) {
annotationsPath = fileManager.getAnnotationPath(book.getName(), page.getName());
} else {
annotationsPath = new File(fileManager.getLocalXmlMap().get(page.getXmlName().split("\\.")[0]));
}
if (annotationsPath.exists()) {
pageAnnotations.add(PageXMLReader.loadPageAnnotationsFromDisc(annotationsPath));
} else {
pageAnnotations.add( new PageAnnotations(page.getName(), page.getWidth(), page.getHeight(),
pageAnnotations.add( new PageAnnotations(page.getName(), page.getXmlName(), page.getWidth(), page.getHeight(),
page.getId(), page.getOrientation(), false));
}
}
Expand All @@ -136,8 +160,12 @@ private void init() {
@RequestMapping(value = "data/status/all/annotations", method = RequestMethod.POST)
public @ResponseBody Collection<Integer> getAnnotationAllStatus(@RequestParam("bookid") int bookID) {
FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
return database.getPagesWithAnnotations(bookID);
config.getListSetting("imagefilter"), fileManager.checkFlat());
if(fileManager.checkFlat()) {
return database.getPagesWithAnnotations(bookID);
} else {
return database.getPagesWithAnnotations(fileManager.getNonFlatBookName(),fileManager.getNonFlatBookId(), fileManager.getLocalImageMap(), fileManager.getLocalXmlMap());
}
}

/**
Expand Down Expand Up @@ -192,4 +220,14 @@ private void init() {
String ocr4allMode = config.getSetting("ocr4all");
return ocr4allMode.equals("enable");
}

/**
* Returns whether LAREX is configured to be used direct request mode or not.
*/
@RequestMapping(value = "config/directrequest", method = RequestMethod.POST, headers = "Accept=*/*",
produces = "application/json")
public @ResponseBody Boolean isDirectRequest() {
String directrequestMode = config.getSetting("directrequest");
return directrequestMode.equals("enable");
}
}
120 changes: 75 additions & 45 deletions src/main/java/de/uniwue/web/controller/FileController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

Expand Down Expand Up @@ -98,33 +97,55 @@ private void init() {
* Request an image of a book, by book name and image name.
* Use resize to get a downscaled preview image, with a width of 300px.
*/
@RequestMapping(value = "/images/books/{book}/{image}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getImage(@PathVariable("book") final String book,
@PathVariable("image") final String image,
@RequestMapping(value = "loadImage/{imageEnc}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getImage(@PathVariable("imageEnc") final String imageEnc,
@RequestParam(value = "resize", defaultValue = "false") boolean doResize) throws IOException {
try {
// Find file with image name
final File directory = new File(fileManager.getLocalBooksPath() + File.separator + book);
final String imageName = image.replace(".png", "");

final File[] matchingFiles = directory.listFiles((File dir, String name) -> {
final int extStart = name.lastIndexOf(".");
if (extStart > 0 && name.substring(0, extStart).equals(imageName)) {
final String extension = name.substring(extStart + 1);
return Arrays.asList("png", "jpg", "jpeg", "tif", "tiff").contains(extension);
} else {
return false;
}
}
);

assert matchingFiles != null;
if (matchingFiles.length == 0)
throw new IOException("File does not exist");

File imageFile;
byte[] imageBytes = null;
String image = java.net.URLDecoder.decode(imageEnc, StandardCharsets.UTF_8.name()).replaceAll("‡","/");
if(image.startsWith("\"")) { image = image.substring(1); }
if(fileManager.checkFlat()) {
// Find file with image name
final File directory = new File(fileManager.getLocalBooksPath() + File.separator);
final String imageName = image.replace(".png", "");

final File[] matchingFiles = directory.listFiles((File dir, String name) -> {
final int extStart = name.lastIndexOf(".");
if (extStart > 0 && name.substring(0, extStart).equals(imageName)) {
final String extension = name.substring(extStart + 1);
return Arrays.asList("png", "jpg", "jpeg", "tif", "tiff").contains(extension);
} else {
return false;
}
}
);
assert matchingFiles != null;
if (matchingFiles.length == 0)
throw new IOException("File does not exist");
imageFile = matchingFiles[0];
} else {
List<String> supportedImageExt = Arrays.asList(".png", ".jpg", ".jpeg", ".tif", ".tiff");
List<String> foundImages = new LinkedList<>();
Map<String, String> localImageMap = fileManager.getLocalImageMap();
for ( String ext: supportedImageExt) {
String imgWithExt = new File(image).getName() + ext;
if(localImageMap.containsKey(imgWithExt)) {
foundImages.add(localImageMap.get(imgWithExt));
}
}
if(foundImages.size() == 0) {
for(String imgPath : localImageMap.values()) {
File imgFile = new File(imgPath);
if(imgFile.getAbsolutePath().contains(image)) {
foundImages.add(imgPath);
}
}
}

File imageFile = matchingFiles[0];
imageFile = new File(foundImages.get(0));
}

if (doResize) {
// load Mat
Expand Down Expand Up @@ -170,6 +191,7 @@ public ResponseEntity<byte[]> getImage(@PathVariable("book") final String book,

return new ResponseEntity<byte[]>(imageBytes, headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand All @@ -192,14 +214,13 @@ public ResponseEntity<byte[]> getImage(@PathVariable("book") final String book,
@RequestMapping(value = "file/export/annotations", method = RequestMethod.POST, headers = "Accept=*/*", produces = "application/json", consumes = "application/json")
public @ResponseBody ResponseEntity<byte[]> exportXML(@RequestBody ExportRequest request) {
try {
final String xmlName = request.getSegmentation().getName() + ".xml";
String xmlPath = fileManager.getLocalXmlMap().get(request.getSegmentation().getXmlName().split("\\.")[0]);
Integer bookId = request.getBookid();

File xmlFile = getXMLFilePath(xmlName, bookId);
final Document pageXML = PageXMLWriter.getPageXML(request.getSegmentation(), request.getVersion(), new File(xmlPath));

final Document pageXML = PageXMLWriter.getPageXML(request.getSegmentation(), request.getVersion(), xmlFile);
saveDocument(pageXML, xmlPath, bookId);

saveDocument(pageXML, xmlName, bookId);
byte[] docBytes = convertDocumentToByte(pageXML);
return convertByteToResponse(docBytes, request.getSegmentation().getName() + ".xml", "application/xml");
} catch (Exception e) {
Expand All @@ -225,7 +246,12 @@ public ResponseEntity<byte[]> getImage(@PathVariable("book") final String book,
Document pageXML = PageXMLWriter.getPageXML(segmentations.get(i), request.getVersion(), xmlFile);
filenames.add(xmlName);
docs.add(pageXML);
saveDocument(pageXML, xmlName, request.getBookid());
if(fileManager.checkFlat()) {
saveDocument(pageXML, xmlName, request.getBookid());
} else {
String xmlPath = fileManager.getLocalXmlMap().get(segmentations.get(i).getName() + ".xml");
saveDocument(pageXML, xmlPath, request.getBookid());
}
}

if(request.getDownload()) {
Expand Down Expand Up @@ -265,7 +291,7 @@ public ResponseEntity<byte[]> getImage(@PathVariable("book") final String book,
@RequestParam("bookID") int bookID) {
SegmentationSettings settings = null;
FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
config.getListSetting("imagefilter"),fileManager.checkFlat());
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
Expand Down Expand Up @@ -366,22 +392,26 @@ private File getXMLFilePath(String xmlName, Integer bookid){
switch (config.getSetting("localsave")) {
case "bookpath":
database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));

String bookdir = fileManager.getLocalBooksPath() + File.separator
+ database.getBookName(bookid);
return new File(bookdir + File.separator + xmlName);
config.getListSetting("imagefilter"), fileManager.checkFlat());
if( fileManager.checkFlat()) {
String bookdir = fileManager.getLocalBooksPath() + File.separator
+ database.getBookName(bookid);
if(!bookdir.endsWith(File.separator)) { bookdir += File.separator; }
return new File(bookdir + xmlName);
} else {
return new File(xmlName);
}
case "savedir":
database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));

String savedir = fileManager.getSaveDir();
if (savedir != null && !savedir.equals("")) {
savedir = savedir + File.separator + database.getBookName(bookid);
return new File(savedir + File.separator + xmlName);
if( fileManager.checkFlat()) {
String savedir = config.getSetting("savedir");
if (savedir != null && !savedir.equals("")) {
if(!savedir.endsWith(File.separator)) { savedir += File.separator; }
return new File(savedir + xmlName);
} else {
System.err.println("Warning: Save dir is not set. File could not been saved.");
}
} else {
System.err.println("Warning: Save dir is not set. File could not been saved.");
return null;
return new File(fileManager.getLocalXmlMap().get(xmlName.split("\\.")[0]));
}
case "none":
case "default":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class ImageProcessingController {
public @ResponseBody Region combinecontours(@RequestBody ContourCombineRequest combineRequest) {
if (combineRequest.getContours().size() > 0) {
FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
config.getListSetting("imagefilter"), fileManager.checkFlat());
return ImageProcessingFacade.combineContours(combineRequest.getContours(), combineRequest.getPageWidth(),
combineRequest.getPageHeight(), combineRequest.getAccuracy(), fileManager, database);
} else
Expand All @@ -70,7 +70,7 @@ public class ImageProcessingController {
@RequestParam("pageid") int pageID) {

FileDatabase database = new FileDatabase(new File(fileManager.getLocalBooksPath()),
config.getListSetting("imagefilter"));
config.getListSetting("imagefilter"), fileManager.checkFlat());
return ImageProcessingFacade.extractContours(pageID, bookID, fileManager, database);
}

Expand Down
Loading

0 comments on commit d675aab

Please sign in to comment.