gosseract

package
v0.0.0-...-d0c75a4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2018 License: MIT, MIT Imports: 3 Imported by: 0

README

🎉 v2 is released! It contains breaking change. If you still want to use v1, please replace github.com/otiai10/gosseract with github.com/otiai10/gosseract/v1/gosseract and it is exactly the same thing as v1 implementation_

Gosseract-OCR

Build Status codecov Go Report Card GoDoc

Golang OCR package, by using Tesseract C++ library.

OCR Server

Do you just want OCR server, or see the working example of this package? Yes, there is already-made server application, which is seriously easy to deploy!

👉 https://github.com/otiai10/ocrserver

Example

package main

import (
	"fmt"
	"github.com/otiai10/gosseract"
)

func main() {
	client := gosseract.NewClient()
	defer client.Close()
	client.SetImage("path/to/image.png")
	text, _ := client.Text()
	fmt.Println(text)
	// Hello, World!
}

Install

  1. tesseract-ocr, including library and headers
  2. go get -t github.com/otiai10/gosseract

Check Dockerfile for more detail of installation, or you can just try by docker run -it --rm otiai10/gosseract.

Test

In case you have tesseract-ocr on your local, you can just hit

% go test .

Otherwise, if you DON'T want to install tesseract-ocr on your local, kick ./test/runtime which is using Docker and Vagrant to test the source code on some runtimes.

% ./test/runtime --driver docker
% ./test/runtime --driver vagrant

Check ./test/runtimes for more information about runtime tests.

Issues

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

Version returns the version of Tesseract-OCR

Types

type Client

type Client struct {

	// Trim specifies characters to trim, which would be trimed from result string.
	// As results of OCR, text often contains unnecessary characters, such as newlines, on the head/foot of string.
	// If `Trim` is set, this client will remove specified characters from the result.
	Trim bool

	// TessdataPrefix can indicate directory path to `tessdata`.
	// It is set `/usr/local/share/tessdata/` or something like that, as default.
	// TODO: Implement and test
	TessdataPrefix *string

	// Languages are languages to be detected. If not specified, it's gonna be "eng".
	Languages []string

	// ImagePath is just path to image file to be processed OCR.
	ImagePath string

	// Variables is just a pool to evaluate "tesseract::TessBaseAPI->SetVariable" in delay.
	// TODO: Think if it should be public, or private property.
	Variables map[string]string

	// PageSegMode is a mode for page layout analysis.
	// See https://github.com/otiai10/gosseract/issues/52 for more information.
	PageSegMode *PageSegMode
	// contains filtered or unexported fields
}

Client is argument builder for tesseract::TessBaseAPI.

func NewClient

func NewClient() *Client

NewClient construct new Client. It's due to caller to Close this client.

Example
client := NewClient()
// Never forget to defer Close. It is due to caller to Close this client.
defer client.Close()
Output:

func (*Client) Close

func (c *Client) Close() (err error)

Close frees allocated API. This MUST be called for ANY client constructed by "NewClient" function.

func (*Client) SetImage

func (c *Client) SetImage(imagepath string) *Client

SetImage sets path to image file to be processed OCR.

Example
client := NewClient()
defer client.Close()

client.SetImage("./test/data/001-gosseract.png")
// See "ExampleClient_Text" for more practical usecase ;)
Output:

func (*Client) SetLanguage

func (c *Client) SetLanguage(langs ...string) *Client

SetLanguage sets languages to use. English as default.

func (*Client) SetPageSegMode

func (c *Client) SetPageSegMode(mode PageSegMode) *Client

SetPageSegMode sets "Page Segmentation Mode" (PSM) to detect layout of characters. See official documentation for PSM here https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#page-segmentation-method

func (*Client) SetVariable

func (c *Client) SetVariable(key, value string) *Client

SetVariable sets parameters, representing tesseract::TessBaseAPI->SetVariable. See official documentation here https://zdenop.github.io/tesseract-doc/classtesseract_1_1_tess_base_a_p_i.html#a2e09259c558c6d8e0f7e523cbaf5adf5

func (*Client) SetWhitelist

func (c *Client) SetWhitelist(whitelist string) *Client

SetWhitelist sets whitelist chars. See official documentation for whitelist here https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#dictionaries-word-lists-and-patterns

Example
client := NewClient()
defer client.Close()
client.SetImage("./test/data/002-confusing.png")

client.SetWhitelist("IO-")
text1, _ := client.Text()

client.SetWhitelist("10-")
text2, _ := client.Text()

fmt.Println(text1, text2)
Output:

IO- IOO 10-100

func (*Client) Text

func (c *Client) Text() (string, error)

Text finally initialize tesseract::TessBaseAPI, execute OCR and extract text detected as string.

Example
client := NewClient()
defer client.Close()

client.SetImage("./test/data/001-gosseract.png")

text, err := client.Text()
fmt.Println(text, err)
Output:

otiai10 / gosseract <nil>

type PageSegMode

type PageSegMode int

PageSegMode represents tesseract::PageSegMode. See https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#page-segmentation-method and https://github.com/tesseract-ocr/tesseract/blob/a18620cfea33d03032b71fe1b9fc424777e34252/ccstruct/publictypes.h#L158-L183 for more information.

const (
	// PSM_OSD_ONLY - Orientation and script detection (OSD) only.
	PSM_OSD_ONLY PageSegMode = iota
	// PSM_AUTO_OSD - Automatic page segmentation with OSD.
	PSM_AUTO_OSD
	// PSM_AUTO_ONLY - Automatic page segmentation, but no OSD, or OCR.
	PSM_AUTO_ONLY
	// PSM_AUTO - (DEFAULT) Fully automatic page segmentation, but no OSD.
	PSM_AUTO
	// PSM_SINGLE_COLUMN - Assume a single column of text of variable sizes.
	PSM_SINGLE_COLUMN
	// PSM_SINGLE_BLOCK_VERT_TEXT - Assume a single uniform block of vertically aligned text.
	PSM_SINGLE_BLOCK_VERT_TEXT
	// PSM_SINGLE_BLOCK - Assume a single uniform block of text.
	PSM_SINGLE_BLOCK
	// PSM_SINGLE_LINE - Treat the image as a single text line.
	PSM_SINGLE_LINE
	// PSM_SINGLE_WORD - Treat the image as a single word.
	PSM_SINGLE_WORD
	// PSM_CIRCLE_WORD - Treat the image as a single word in a circle.
	PSM_CIRCLE_WORD
	// PSM_SINGLE_CHAR - Treat the image as a single character.
	PSM_SINGLE_CHAR
	// PSM_SPARSE_TEXT - Find as much text as possible in no particular order.
	PSM_SPARSE_TEXT
	// PSM_SPARSE_TEXT_OSD - Sparse text with orientation and script det.
	PSM_SPARSE_TEXT_OSD
	// PSM_RAW_LINE - Treat the image as a single text line, bypassing hacks that are Tesseract-specific.
	PSM_RAW_LINE

	// PSM_COUNT - Just a number of enum entries. This is NOT a member of PSM ;)
	PSM_COUNT
)

Directories

Path Synopsis
v1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL