Class TesseractOCRParser
- java.lang.Object
-
- org.apache.tika.parser.AbstractParser
-
- org.apache.tika.parser.AbstractExternalProcessParser
-
- org.apache.tika.parser.ocr.TesseractOCRParser
-
- All Implemented Interfaces:
Serializable
,Initializable
,Parser
public class TesseractOCRParser extends AbstractExternalProcessParser implements Initializable
TesseractOCRParser powered by tesseract-ocr engine. To enable this parser, create aTesseractOCRConfig
object and pass it through a ParseContext. Tesseract-ocr must be installed and on system path or the path to its root folder must be provided:TesseractOCRConfig config = new TesseractOCRConfig();
//Needed if tesseract is not on system path
config.setTesseractPath(tesseractFolder);
parseContext.set(TesseractOCRConfig.class, config);
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static Property
IMAGE_MAGICK
static Property
IMAGE_ROTATION
static Property
PSM0_ORIENTATION
static Property
PSM0_ORIENTATION_CONFIDENCE
static Property
PSM0_PAGE_NUMBER
static Property
PSM0_ROTATE
static Property
PSM0_SCRIPT
static Property
PSM0_SCRIPT_CONFIDENCE
static String
TESS_META
-
Constructor Summary
Constructors Constructor Description TesseractOCRParser()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
checkInitialization(InitializableProblemHandler problemHandler)
String
getColorspace()
TesseractOCRConfig
getDefaultConfig()
int
getDensity()
int
getDepth()
String
getFilter()
String
getImageMagickPath()
static String
getImageMagickProg()
Set<String>
getLangs()
String
getLanguage()
long
getMaxFileSizeToOcr()
long
getMinFileSizeToOcr()
List<String>
getOtherTesseractSettings()
String
getOutputType()
String
getPageSegMode()
int
getResize()
Set<MediaType>
getSupportedTypes(ParseContext context)
Returns the set of media types supported by this parser when used with the given parse context.String
getTessdataPath()
String
getTesseractPath()
static String
getTesseractProg()
int
getTimeout()
boolean
hasTesseract()
protected boolean
hasWarned()
void
initialize(Map<String,Param> params)
boolean
isApplyRotation()
boolean
isEnableImagePreprocessing()
boolean
isPreloadLangs()
boolean
isPreserveInterwordSpacing()
boolean
isSkipOCR()
void
parse(Image image, ContentHandler handler, Metadata metadata, ParseContext context)
void
parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext parseContext)
Parses a document stream into a sequence of XHTML SAX events.void
setApplyRotation(boolean applyRotation)
void
setColorspace(String colorspace)
void
setDensity(int density)
void
setDepth(int depth)
void
setEnableImagePreprocessing(boolean enableImagePreprocessing)
void
setFilter(String filter)
void
setImageMagickPath(String imageMagickPath)
Set the path to the ImageMagick executable directory, needed if it is not on system path.void
setLanguage(String language)
void
setMaxFileSizeToOcr(long maxFileSizeToOcr)
void
setMinFileSizeToOcr(long minFileSizeToOcr)
void
setOtherTesseractSettings(List<String> settings)
void
setOutputType(String outputType)
void
setPageSegMode(String pageSegMode)
void
setPreloadLangs(boolean preloadLangs)
If set totrue
and if tesseract is found, this will load the langs that result from --list-langs.void
setPreserveInterwordSpacing(boolean preserveInterwordSpacing)
void
setResize(int resize)
void
setSkipOCR(boolean skipOCR)
void
setTessdataPath(String tessdataPath)
Set the path to the 'tessdata' folder, which contains language files and config files.void
setTesseractPath(String tesseractPath)
Set the path to the Tesseract executable's directory, needed if it is not on system path.void
setTimeout(int timeout)
Set default timeout in seconds.protected void
warn()
-
Methods inherited from class org.apache.tika.parser.AbstractExternalProcessParser
register, release
-
Methods inherited from class org.apache.tika.parser.AbstractParser
parse
-
-
-
-
Field Detail
-
TESS_META
public static final String TESS_META
- See Also:
- Constant Field Values
-
IMAGE_ROTATION
public static final Property IMAGE_ROTATION
-
IMAGE_MAGICK
public static final Property IMAGE_MAGICK
-
PSM0_PAGE_NUMBER
public static final Property PSM0_PAGE_NUMBER
-
PSM0_ORIENTATION
public static final Property PSM0_ORIENTATION
-
PSM0_ROTATE
public static final Property PSM0_ROTATE
-
PSM0_ORIENTATION_CONFIDENCE
public static final Property PSM0_ORIENTATION_CONFIDENCE
-
PSM0_SCRIPT
public static final Property PSM0_SCRIPT
-
PSM0_SCRIPT_CONFIDENCE
public static final Property PSM0_SCRIPT_CONFIDENCE
-
-
Method Detail
-
getImageMagickProg
public static String getImageMagickProg()
-
getTesseractProg
public static String getTesseractProg()
-
getSupportedTypes
public Set<MediaType> getSupportedTypes(ParseContext context)
Description copied from interface:Parser
Returns the set of media types supported by this parser when used with the given parse context.- Specified by:
getSupportedTypes
in interfaceParser
- Parameters:
context
- parse context- Returns:
- immutable set of media types
-
hasTesseract
public boolean hasTesseract() throws TikaConfigException
- Throws:
TikaConfigException
-
parse
public void parse(Image image, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException
- Throws:
IOException
SAXException
TikaException
-
parse
public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext parseContext) throws IOException, SAXException, TikaException
Description copied from interface:Parser
Parses a document stream into a sequence of XHTML SAX events. Fills in related document metadata in the given metadata object.The given document stream is consumed but not closed by this method. The responsibility to close the stream remains on the caller.
Information about the parsing context can be passed in the context parameter. See the parser implementations for the kinds of context information they expect.
- Specified by:
parse
in interfaceParser
- Parameters:
stream
- the document stream (input)handler
- handler for the XHTML SAX events (output)metadata
- document metadata (input and output)parseContext
- parse context- Throws:
IOException
- if the document stream could not be readSAXException
- if the SAX events could not be processedTikaException
- if the document could not be parsed
-
initialize
public void initialize(Map<String,Param> params) throws TikaConfigException
- Specified by:
initialize
in interfaceInitializable
- Parameters:
params
- params to use for initialization- Throws:
TikaConfigException
-
checkInitialization
public void checkInitialization(InitializableProblemHandler problemHandler) throws TikaConfigException
- Specified by:
checkInitialization
in interfaceInitializable
- Parameters:
problemHandler
- if there is a problem and no custom initializableProblemHandler has been configured via Initializable parameters, this is called to respond.- Throws:
TikaConfigException
-
hasWarned
protected boolean hasWarned()
-
warn
protected void warn()
-
getTesseractPath
public String getTesseractPath()
-
setTesseractPath
@Field public void setTesseractPath(String tesseractPath)
Set the path to the Tesseract executable's directory, needed if it is not on system path.Note that if you set this value, it is highly recommended that you also set the path to (and including) the 'tessdata' folder using
setTessdataPath(java.lang.String)
.
-
getTessdataPath
public String getTessdataPath()
-
setTessdataPath
@Field public void setTessdataPath(String tessdataPath)
Set the path to the 'tessdata' folder, which contains language files and config files. In some cases (such as on Windows), this folder is found in the Tesseract installation, but in other cases (such as when Tesseract is built from source), it may be located elsewhere. Make sure to include the 'tessdata' folder in this path: '/blah/de/blah/tessdata'
-
getImageMagickPath
public String getImageMagickPath()
-
setImageMagickPath
@Field public void setImageMagickPath(String imageMagickPath)
Set the path to the ImageMagick executable directory, needed if it is not on system path.- Parameters:
imageMagickPath
- to ImageMagick executable directory.
-
setOtherTesseractSettings
@Field public void setOtherTesseractSettings(List<String> settings) throws TikaConfigException
- Throws:
TikaConfigException
-
setSkipOCR
@Field public void setSkipOCR(boolean skipOCR)
-
isSkipOCR
public boolean isSkipOCR()
-
getLanguage
public String getLanguage()
-
getPageSegMode
public String getPageSegMode()
-
setMaxFileSizeToOcr
@Field public void setMaxFileSizeToOcr(long maxFileSizeToOcr)
-
getMaxFileSizeToOcr
public long getMaxFileSizeToOcr()
-
setMinFileSizeToOcr
@Field public void setMinFileSizeToOcr(long minFileSizeToOcr)
-
getMinFileSizeToOcr
public long getMinFileSizeToOcr()
-
setTimeout
@Field public void setTimeout(int timeout)
Set default timeout in seconds. This can be overridden per parse withTikaTaskTimeout
sent in via theParseContext
at parse time.- Parameters:
timeout
-
-
getTimeout
public int getTimeout()
-
getOutputType
public String getOutputType()
-
setPreserveInterwordSpacing
@Field public void setPreserveInterwordSpacing(boolean preserveInterwordSpacing)
-
isPreserveInterwordSpacing
public boolean isPreserveInterwordSpacing()
-
setEnableImagePreprocessing
@Field public void setEnableImagePreprocessing(boolean enableImagePreprocessing)
-
isEnableImagePreprocessing
public boolean isEnableImagePreprocessing()
-
setDensity
@Field public void setDensity(int density)
-
getDensity
public int getDensity()
-
setDepth
@Field public void setDepth(int depth)
-
getDepth
public int getDepth()
-
getColorspace
public String getColorspace()
-
getFilter
public String getFilter()
-
setResize
@Field public void setResize(int resize)
-
getResize
public int getResize()
-
setApplyRotation
@Field public void setApplyRotation(boolean applyRotation)
-
isApplyRotation
public boolean isApplyRotation()
-
setPreloadLangs
@Field public void setPreloadLangs(boolean preloadLangs)
If set totrue
and if tesseract is found, this will load the langs that result from --list-langs. At parse time, the parser will verify that tesseract has the requested lang available.If set to
false
(the default) and tesseract is found, if a user requests a language that tesseract does not have data for, a TikaException will be thrown with tesseract's native exception message, which is a bit less readable.- Parameters:
preloadLangs
-
-
isPreloadLangs
public boolean isPreloadLangs()
-
getDefaultConfig
public TesseractOCRConfig getDefaultConfig()
-
-