Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCR #65

Merged
merged 16 commits into from
May 14, 2020
Merged

OCR #65

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ dependencies {
compile("org.glassfish.jaxb:jaxb-runtime:2.3.2")
compile("commons-io:commons-io:2.6")
compile("org.apache.tika:tika-parsers:1.24")
compile("org.apache.pdfbox:pdfbox:2.0.1")
yuliiabuchko marked this conversation as resolved.
Show resolved Hide resolved
compile("net.sourceforge.tess4j:tess4j:3.4.0")
yuliiabuchko marked this conversation as resolved.
Show resolved Hide resolved

// Remove devtools for release
runtime('org.springframework.boot:spring-boot-devtools')
Expand Down
5 changes: 3 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Mon May 04 21:03:43 CEST 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
yuliiabuchko marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package pl.edu.uj.ii.ksi.mordor.configuration

import net.sourceforge.tess4j.Tesseract
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class TesseractConfiguration {
@Bean
fun tesseract(): Tesseract {
val tesseract = Tesseract()
tesseract.setDatapath("./src/main/resources/")
yuliiabuchko marked this conversation as resolved.
Show resolved Hide resolved
tesseract.setLanguage("pol+eng")
return tesseract
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pl.edu.uj.ii.ksi.mordor.services

import java.io.File
import java.io.IOException
import net.sourceforge.tess4j.Tesseract
import net.sourceforge.tess4j.TesseractException
import org.apache.tika.Tika
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import pl.edu.uj.ii.ksi.mordor.services.repository.RepositoryService

@Service
class AutoDetectTextExtractor(
private val tika: Tika,
private val tesseract: Tesseract
) : FileTextExtractor {

companion object {
private val logger = LoggerFactory.getLogger(RepositoryService::class.java)
}

override fun extract(file: File, maxLength: Int): String? {
try {
val tikaContent = TikaFileTextExtractor(tika).extract(file)
if (!isScanned(tikaContent)) {
return tikaContent
}
val type = tika.detect(file)
if (type == "application/pdf") {
return PDFTextExtractor(tesseract).extract(file)
}
if (type.startsWith("image")) {
return ImageTextExtractor(tesseract).extract(file)
}
} catch (e: TesseractException) {
logger.error("Tesseract is unable do process OCR", e)
} catch (e: IOException) {
logger.error("File can not be read", e)
}
return null
}

private fun isScanned(content: String?): Boolean {
if (content == null) {
return true
}
return content.trim().isEmpty()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pl.edu.uj.ii.ksi.mordor.services

import java.io.File
import javax.persistence.EntityManager
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import pl.edu.uj.ii.ksi.mordor.persistence.entities.FileContent
Expand All @@ -14,7 +15,7 @@ import pl.edu.uj.ii.ksi.mordor.services.hash.FileHashProvider
class FileEntryCreator(
private val metadataExtractor: MetadataExtractor,
private val entityManager: EntityManager,
private val fileTextExtractor: FileTextExtractor,
@Qualifier("autoDetectTextExtractor") private val fileTextExtractor: FileTextExtractor,
private val hashProvider: FileHashProvider,
private val metadataRepository: FileMetadataRepository
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pl.edu.uj.ii.ksi.mordor.services

import com.recognition.software.jdeskew.ImageDeskew
import java.awt.image.BufferedImage
import java.io.File
import javax.imageio.ImageIO
import net.sourceforge.tess4j.Tesseract
import net.sourceforge.tess4j.TesseractException
import net.sourceforge.tess4j.util.ImageHelper
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class ImageTextExtractor(private val tesseract: Tesseract) : FileTextExtractor {

val maxSkewAngle = 0.05

override fun extract(file: File, maxLength: Int): String? {
return extractTextFromBufferedImage(ImageIO.read(file))
}

private fun correctTwisted(image: BufferedImage?): BufferedImage? {
val imageSkewAngle = ImageDeskew(image).skewAngle
if (kotlin.math.abs(imageSkewAngle) > maxSkewAngle) {
return ImageHelper.rotateImage(ImageHelper.convertImageToGrayscale(image), -imageSkewAngle)
}
return ImageHelper.convertImageToGrayscale(image)
}

fun extractTextFromBufferedImage(image: BufferedImage?): String? {
try {
return tesseract.doOCR(correctTwisted(image))
.replace("\\n{2,}", "\n")
.trim { c -> c <= ' ' }
} catch (e: TesseractException) {
LoggerFactory.getLogger(this.javaClass).error("Tesseract is unable do process OCR on image", e)
yuliiabuchko marked this conversation as resolved.
Show resolved Hide resolved
}
return null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pl.edu.uj.ii.ksi.mordor.services

import java.awt.image.BufferedImage
import java.io.File
import java.util.LinkedList
import net.sourceforge.tess4j.Tesseract
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject
import org.springframework.stereotype.Service

@Service
class PDFTextExtractor(private val tesseract: Tesseract) : FileTextExtractor {

override fun extract(file: File, maxLength: Int): String? {
val extracted = StringBuilder()
val bufferedImages = formatPDF(file)

if (!bufferedImages.isEmpty()) {
for (image in bufferedImages) {
val text: String? = ImageTextExtractor(tesseract).extractTextFromBufferedImage(image)
if (text != null) {
extracted.append(text)
}
}
}
return extracted.toString()
}

private fun formatPDF(pdfFile: File): LinkedList<BufferedImage?> {
val bufferedImages = LinkedList<BufferedImage?>()
val doc: PDDocument = PDDocument.load(pdfFile)
for (page in doc.pages) {
val resources = page.resources
for (xObjectName in resources.xObjectNames) {
val xObject = resources.getXObject(xObjectName)
if (xObject is PDImageXObject) {
bufferedImages.add(xObject.image)
}
}
}
doc.close()
yuliiabuchko marked this conversation as resolved.
Show resolved Hide resolved
return bufferedImages
}
}
Binary file added src/main/resources/tessdata/eng.traineddata
Binary file not shown.
Binary file added src/main/resources/tessdata/pol.traineddata
Binary file not shown.