ocr_space

package module
v0.0.0-...-5062ce6 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

OCR_Space

OCR library is based on OCR.Space API, meant to read text from pdf files and images with Go language

Index

  1. Get your API key
  2. Installation
  3. Update
  4. Basic Usage
  5. Example Code
1. Get your API key

You can get your personal API key here (we will need this later)

2. Installation

go get -t github.com/ranghetto/go_ocr_space

3. Update

Delete the folder situated in $GO_PATH/src/github.com/ranghetto/go_ocr_space and then run this command:

go get -t github.com/ranghetto/go_ocr_space

4. Basic Usage

You need at first to create a configuration:

package main

import(
	/*
	Remember to run your program from your $GO_PATH/src/name_of_your_folder
	or to provide the right path to this library that is situated
	in $GO_PATH/src/github.com/ranghetto/go_ocr_space
	*/
	
	ocr "github.com/ranghetto/go_ocr_space"
	//Other libraries...
)

func main(){
	
	config := ocr.InitConfig("yourApiKeyHere", "eng")
	//More code here...
}

The first parameter is your API key as a string and the second one is the code of the language you want read from file or image. Here a list of all available languages and their code*:

  • Arabic = ara
  • Bulgarian = bul
  • Chinese(Simplified) = chs
  • Chinese(Traditional) = cht
  • Croatian = hrv
  • Czech = cze
  • Danish = dan
  • Dutch = dut
  • English = eng
  • Finnish = fin
  • French = fre
  • German = ger
  • Greek = gre
  • Hungarian = hun
  • Korean = kor
  • Italian = ita
  • Japanese = jpn
  • Polish = pol
  • Portuguese = por
  • Russian = rus
  • Slovenian = slv
  • Spanish = spa
  • Swedish = swe
  • Turkish = tur

Now we can go ahead and start reading some text; there are three method that allow you to do it:

config.ParseFromUrl("https://example.com/image.png")

config.ParseFromLocal("path/to/the/image.jpg")

config.ParseFromBase64("data:image/jpeg;base64,873hf9qehq98efwuehf...")

Method names are self explanatory.

Remember: .ParseFromBase64 need as parameter a valid Base64 format like data:<file>/<extension>;base64,<image> where:

  • <file> is application in case of a pdf file or image in case of an image
  • <extension> is the extension of the file you encode. Only valid are pdf, jpg, png and gif
  • <image> is the actual encode of your file

So basically these methods will give you back the whole struct complete of all parameters that OCR.Space provides to you.

If you are only interested in the output text call .justText() method at the end of one of the three methods mentioned above.

5. Example Code

package main

import (
	"fmt"
	ocr "github.com/ranghetto/go_ocr_space"
)

func main() {
	//this is a demo api key 
	apiKey:="helloworld"

    //setting up the configuration 
    config := ocr.InitConfig(apiKey , "eng")

    //actual converting the image from the url (struct content) 
    result, err := config.ParseFromUrl("https://www.azquotes.com/picture-quotes/quote-maybe-we-should-all-just-listen-to-records-and-quit-our-jobs-jack-white-81-40-26.jpg")
    if err != nil {
    	fmt.Println(err)
    }
    //printing the just the parsed text
	fmt.Println(result.JustText())
}
Example output

Maybe we should all just listen to 
records and quit our jobs 
Jack White 
AZ QUOTES

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiOptions

type ApiOptions struct {
	Url        string
	HTTPClient *http.Client
}

type File

type File struct {
	Name    string
	Content []byte
}

type Filetype

type Filetype string
const (
	FiletypePDF Filetype = "PDF"
	FiletypeGIF Filetype = "GIF"
	FiletypePNG Filetype = "PNG"
	FiletypeJPG Filetype = "JPG"
	FiletypeTIF Filetype = "TIF"
	FiletypeBMP Filetype = "BMP"
)

type Lang

type Lang string
const (
	LangArabic             Lang = "ara"
	LangBulgarian          Lang = "bul"
	LangChineseSimplified  Lang = "chs"
	LangChineseTraditional Lang = "cht"
	LangCroatian           Lang = "hrv"
	LangCzech              Lang = "cze"
	LangDanish             Lang = "dan"
	LangDutch              Lang = "dut"
	LangEnglish            Lang = "eng"
	LangFinnish            Lang = "fin"
	LangFrench             Lang = "fre"
	LangGerman             Lang = "ger"
	LangGreek              Lang = "gre"
	LangHungarian          Lang = "hun"
	LangKorean             Lang = "kor"
	LangItalian            Lang = "ita"
	LangJapanese           Lang = "jpn"
	LangPolish             Lang = "pol"
	LangPortuguese         Lang = "por"
	LangRussian            Lang = "rus"
	LangSlovenian          Lang = "slv"
	LangSpanish            Lang = "spa"
	LangSwedish            Lang = "swe"
	LangTurkish            Lang = "tur"
)

type OCREngineVer

type OCREngineVer int
const (
	OCREngineV1 OCREngineVer = 1
	OCREngineV2 OCREngineVer = 2
)

type OCRSpaceAPI

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

func InitAPI

func InitAPI(apiKey string, options ApiOptions) OCRSpaceAPI

func (*OCRSpaceAPI) ParseFromBase64

func (a *OCRSpaceAPI) ParseFromBase64(baseString string, params Params) (*OCRText, error)

func (*OCRSpaceAPI) ParseFromLocal

func (a *OCRSpaceAPI) ParseFromLocal(file File, params Params) (*OCRText, error)

func (*OCRSpaceAPI) ParseFromUrl

func (a *OCRSpaceAPI) ParseFromUrl(fileUrl string, params Params) (*OCRText, error)

type OCRText

type OCRText struct {
	ParsedResults []struct {
		TextOverlay struct {
			Lines []struct {
				Words []struct {
					WordText string  `json:"WordText"`
					Left     float64 `json:"Left"`
					Top      float64 `json:"Top"`
					Height   float64 `json:"Height"`
					Width    float64 `json:"Width"`
				} `json:"Words"`

				MaxHeight float64 `json:"MaxHeight"`
				MinTop    float64 `json:"MinTop"`
			} `json:"Lines"`

			HasOverlay bool   `json:"HasOverlay"`
			Message    string `json:"Message"`
		} `json:"TextOverlay"`

		TextOrientation   string `json:"TextOrientation"`
		FileParseExitCode int    `json:"FileParseExitCode"`
		ParsedText        string `json:"ParsedText"`
		ErrorMessage      string `json:"ErrorMessage"`
		ErrorDetails      string `json:"ErrorDetails"`
	} `json:"ParsedResults"`

	OCRExitCode                  int      `json:"OCRExitCode"`
	IsErroredOnProcessing        bool     `json:"IsErroredOnProcessing"`
	ErrorMessage                 []string `json:"ErrorMessage"`
	ErrorDetails                 string   `json:"ErrorDetails"`
	ProcessingTimeInMilliseconds string   `json:"ProcessingTimeInMilliseconds"`
	SearchablePDFURL             string   `json:"SearchablePDFURL"`
}

func (OCRText) JustText

func (ocr OCRText) JustText() string

type Params

type Params struct {
	// Language used for OCR.
	// Default = eng
	Language Lang `url:"language,omitempty"`

	// If true, returns the coordinates of the bounding boxes for each word.
	// If false, the OCR'ed text is returned only as a text block (this makes the JSON reponse smaller).
	// Default = False
	IsOverlayRequired bool `url:"isOverlayRequired"`

	// Overwrites the automatic file type detection based on content-type.
	// Supported image file formats are png, jpg (jpeg), gif, tif (tiff) and bmp.
	// For document ocr, the api supports the Adobe PDF format. Multi-page TIFF files are supported.
	Filetype Filetype `url:"filetype,omitempty"`

	// If set to true, the api autorotates the image correctly and sets the TextOrientation parameter in the JSON response.
	// If the image is not rotated, then TextOrientation=0, otherwise it is the degree of the rotation, e. g. "270".
	// Default = False
	DetectOrientation bool `url:"detectOrientation"`

	// If true, API generates a searchable PDF.
	// This parameter automatically sets isOverlayRequired = true.
	// Default = False
	IsCreateSearchablePDF bool `url:"isCreateSearchablePdf"`

	// If true, the text layer is hidden (not visible)
	// Default = False
	IsSearchablePDFHideTextLayer bool `url:"isSearchablePdfHideTextLayer"`

	// If set to true, the api does some internal upscaling.
	// This can improve the OCR result significantly, especially for low-resolution PDF scans.
	// Default = False
	Scale bool `url:"scale"`

	// If set to true, the OCR logic makes sure that the parsed text result is always returned line by line.
	// This switch is recommended for table OCR, receipt OCR, invoice processing and all other type of input documents that have a table like structure.
	// Default = False
	IsTable bool `url:"isTable"`

	// OCR engine version: 1 or 2
	// Default = 1
	OCREngine *OCREngineVer `url:"OCREngine,omitempty"`
}

Jump to

Keyboard shortcuts

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