gocr

package module
v0.0.0-...-b9d741b Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2017 License: Apache-2.0 Imports: 20 Imported by: 0

README

gocr

gocr is a go based OCR module

Available Scanner

  • Linear Mean Scanner
  • Circular Scanner (Currently the best)

Available predictor

  • NNPredictor (kNN with k = 1)
  • CNNPredictor (Need to install tensorflow first) (Doesn't upport custom train)

Future Improvement

  • Sauvola Segmentation
  • Better character detection
  • Better symbol detection
  • Spelling Correction

How to use

To use OCR we have some generated Model that we train using tensorflow. We use Convolutional Neural Network as the model architecture and produce good detection.

You can load and use the model to CNN Predictor like this example below:

d, _ := os.Getwd()

image, _ := gocr.ReadImage(d + "/imagetext_3.png")
s := gocr.NewCNNPredictorFromDir(modelPath + "tensor_4/")

// Define the image size
s.InputHeight, s.InputWidth = 64, 64

strings := gocr.ScanToStrings(s, image)
for _, s := range strings {
  fmt.Println(s)
}

However you can also use your own train data. Currently the predictor that support custom training only NNPredictor. Training takes csv file that have file image path and string representation.

ie:

a.gif,a
b.gif,b
A1.gif,A
B1.gif,B

Train the sample data to model and scan image

d, _ := os.Getwd()

// Load the sample data and save it in model.cbor file
err := gocr.TrainAverage(d+"/English/Fnt/", d+"/English/")
if err != nil {
  panic(err)
}

image, _ := gocr.ReadImage(d + "/imagetext_3.png")
s := gocr.NewCNNPredictorFromDir(modelPath + "sample1/model.cbor")

strings := gocr.ScanToStrings(s, image)
for _, s := range strings {
  fmt.Println(s)
}

License

gocr is released under the Apache 2.0 License. se LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CirucularScan

func CirucularScan(image ImageMatrix) ([][]*Square, [][]ImageMatrix)

func Col2im

func Col2im(x *mat64.Dense, d, r, c int) []*mat64.Dense

func Convolve

func Convolve(x, k [][]*mat64.Dense, p, s int) [][]*mat64.Dense

func EuclideanDistance

func EuclideanDistance(m1, m2 ImageMatrix) float64

Find the distance of 2 give Dense using Euclidean Distance

func Im2col

func Im2col(x []*mat64.Dense, p, s, kd, kr, kc int) *mat64.Dense

func ImageMatrixToImage

func ImageMatrixToImage(imageArray ImageMatrix, outPath string, mul int) error

Convert ImageMatrix to Image and save it to given path

func LeakyRelu

func LeakyRelu(x float64, deriv bool) float64

func LinearScan

func LinearScan(data ImageMatrix) [][]ImageMatrix

Scan the DarkSquare and return it as []mat64.Dense for each line And [][]mat64.Dense for each characther

func MaxPool

func MaxPool(x *mat64.Dense, fr, fc int) (*mat64.Dense, *mat64.Dense)

func Prepare

func Prepare(targetPath string) error

Download the dataset from here and save it in chars74k_dataset/EnglishFnt Also copy index.csv to extracted folder in targetPath

func ReadCSV

func ReadCSV(path string) ([][]string, error)

Read CSV from given path and return array [][]string

func ReadImage

func ReadImage(path string) (image.Image, error)

Read image in given path Can open and decode some type of image (.png, .jpg, .gif)

func Relu

func Relu(x float64, deriv bool) float64

func Reshape2DTo2D

func Reshape2DTo2D(x *mat64.Dense, r, c int) *mat64.Dense

func ReshapeKernel

func ReshapeKernel(x [][]*mat64.Dense) *mat64.Dense

func ScanToStrings

func ScanToStrings(p Predictor, image image.Image) []string

func Sigmoid

func Sigmoid(x float64, deriv bool) float64

func Train

func Train(sampleFolderPath string, modelPath string) error

Train read the image file from sample path convert it to model and save it in given model path The train folder should include index.csv and images that inside the index.csv

Types

type CNNPredictor

type CNNPredictor struct {
	InputHeight int
	InputWidth  int
	// contains filtered or unexported fields
}

func NewCNNPredictor

func NewCNNPredictor(graph *tf.Graph, labels []string) *CNNPredictor

func NewCNNPredictorFromDir

func NewCNNPredictorFromDir(dir string) *CNNPredictor

func (*CNNPredictor) Predicts

func (p *CNNPredictor) Predicts(images ImageMatrixs) []string

type Coordinate

type Coordinate struct {
	// contains filtered or unexported fields
}

func NewCoordinate

func NewCoordinate(r, c int) *Coordinate

func (*Coordinate) DistanceTo

func (c *Coordinate) DistanceTo(c2 *Coordinate) float64

func (*Coordinate) E

func (c *Coordinate) E() *Coordinate

func (*Coordinate) HorizontalDistanceTo

func (c *Coordinate) HorizontalDistanceTo(c2 *Coordinate) float64

func (*Coordinate) IsInside

func (c *Coordinate) IsInside(s *Square) bool

func (*Coordinate) N

func (c *Coordinate) N() *Coordinate

func (*Coordinate) NE

func (c *Coordinate) NE() *Coordinate

func (*Coordinate) NW

func (c *Coordinate) NW() *Coordinate

func (*Coordinate) S

func (c *Coordinate) S() *Coordinate

func (*Coordinate) SE

func (c *Coordinate) SE() *Coordinate

func (*Coordinate) SW

func (c *Coordinate) SW() *Coordinate

func (*Coordinate) VerticalDistanceTo

func (c *Coordinate) VerticalDistanceTo(c2 *Coordinate) float64

func (*Coordinate) W

func (c *Coordinate) W() *Coordinate

type Groups

type Groups struct {
	// contains filtered or unexported fields
}

type ImageMatrix

type ImageMatrix [][]uint8

func AdaptiveThres

func AdaptiveThres(im ImageMatrix, bs int) ImageMatrix

func ImageToGraysclaeArray

func ImageToGraysclaeArray(src image.Image) ImageMatrix

Convert image to grayscale 2D array

func NewImageMatrix

func NewImageMatrix(r, c int) ImageMatrix

func NewImageMatrixWithDefaultValue

func NewImageMatrixWithDefaultValue(r, c int, v uint8) ImageMatrix

func OtsuThresh

func OtsuThresh(im ImageMatrix) ImageMatrix

Thresholding Using Otsu's Method

func PadAndResize

func PadAndResize(matrix ImageMatrix, dr, dc int) ImageMatrix

Adding pad to make a square matrix Then resize it to given row length and column length

func SauvolaBinarization

func SauvolaBinarization(imageArr ImageMatrix) ImageMatrix

Binarize the given imageArr using Best algorithm based on this paper https://pdfs.semanticscholar.org/6347/5461213fdaa24e418c33454c72bdbbe8f8b4.pdf is Sauvola Sauvola Reference: http://www.mediateam.oulu.fi/publications/pdf/24.p TODO: Implement Sauvola algorithm

func Threshold

func Threshold(im ImageMatrix, thrs uint8) ImageMatrix

Basic Thresholding

func (ImageMatrix) At

func (i ImageMatrix) At(r, c int) uint8

func (ImageMatrix) AtCoordinate

func (i ImageMatrix) AtCoordinate(c *Coordinate) uint8

func (ImageMatrix) Col

func (i ImageMatrix) Col(c int) ImageVector

func (ImageMatrix) Dims

func (i ImageMatrix) Dims() (int, int)

func (ImageMatrix) Erode

func (im ImageMatrix) Erode()

Erode using 4 Neighborhood

func (ImageMatrix) Historgram

func (im ImageMatrix) Historgram() []int

func (ImageMatrix) NNInterpolation

func (i ImageMatrix) NNInterpolation(tr, tc int) ImageMatrix

func (ImageMatrix) Pad

func (i ImageMatrix) Pad(top, bottom, left, right int, value uint8) ImageMatrix

func (ImageMatrix) Row

func (i ImageMatrix) Row(r int) ImageVector

func (ImageMatrix) Set

func (i ImageMatrix) Set(r, c int, value uint8)

func (ImageMatrix) SetSquare

func (im ImageMatrix) SetSquare(s *Square, si ImageMatrix)

func (ImageMatrix) Slice

func (i ImageMatrix) Slice(sr, er, sc, ec int) ImageMatrix

func (ImageMatrix) SliceSquare

func (im ImageMatrix) SliceSquare(s *Square) ImageMatrix

type ImageMatrixs

type ImageMatrixs []ImageMatrix

func (ImageMatrixs) Average

func (is ImageMatrixs) Average() ImageMatrix

type ImageVector

type ImageVector []uint8

func NewImageVector

func NewImageVector(l int) ImageVector

func (ImageVector) Sum

func (v ImageVector) Sum() uint64

type Marker

type Marker struct {
	Start int
	End   int
}

func MarkersOfMatrix

func MarkersOfMatrix(data ImageMatrix, threshold float64, direction int) []Marker

Find Marker of DarkSquare from given Matrix Direction 0 means it will iterate every rows Direction 1 means it will iterate every columns

type Model

type Model struct {
	Name        string
	ModelImages []ModelImage
}

type ModelImage

type ModelImage struct {
	Label string
	Data  ImageMatrix
}

type NNPredictor

type NNPredictor struct {
	// contains filtered or unexported fields
}

================================= Nearest Neighbor Predictor =================================

func NewNNPredictor

func NewNNPredictor(model *Model) *NNPredictor

func NewNNPredictorFromFile

func NewNNPredictorFromFile(path string) *NNPredictor

func (*NNPredictor) Predicts

func (p *NNPredictor) Predicts(images ImageMatrixs) []string

type Predictor

type Predictor interface {
	Predicts(ImageMatrixs) []string
	// contains filtered or unexported methods
}

type Square

type Square struct {
	// contains filtered or unexported fields
}

func NewSquare

func NewSquare(coor1, coor2 *Coordinate) *Square

func (*Square) Area

func (s *Square) Area() int

func (*Square) AverageDistanceTo

func (s *Square) AverageDistanceTo(s2 *Square) float64

func (*Square) AverageHorizontalDistanceTo

func (s *Square) AverageHorizontalDistanceTo(s2 *Square) float64

func (*Square) AverageVerticalDistanceTo

func (s *Square) AverageVerticalDistanceTo(s2 *Square) float64

func (*Square) DistancesTo

func (s *Square) DistancesTo(s2 *Square) []float64

func (*Square) Expand

func (s *Square) Expand(c *Coordinate)

func (*Square) FarthestDistanceTo

func (s *Square) FarthestDistanceTo(s2 *Square) float64

func (*Square) FarthestHorizontalDistanceTo

func (s *Square) FarthestHorizontalDistanceTo(s2 *Square) float64

func (*Square) FarthestVerticalDistanceTo

func (s *Square) FarthestVerticalDistanceTo(s2 *Square) float64

func (*Square) Height

func (s *Square) Height() int

func (*Square) HorizontalDistancesTo

func (s *Square) HorizontalDistancesTo(s2 *Square) []float64

func (*Square) Include

func (s *Square) Include(r, c int) bool

func (*Square) Merge

func (s *Square) Merge(a2 *Square)

func (*Square) NearestDistanceTo

func (s *Square) NearestDistanceTo(s2 *Square) float64

func (*Square) NearestHorizontalDistanceTo

func (s *Square) NearestHorizontalDistanceTo(s2 *Square) float64

func (*Square) NearestVerticalDistanceTo

func (s *Square) NearestVerticalDistanceTo(s2 *Square) float64

func (*Square) VerticalDistancesTo

func (s *Square) VerticalDistancesTo(s2 *Square) []float64

func (*Square) Width

func (s *Square) Width() int

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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