How to Convert a Scanned Image into Text using Go
While our phones & cameras can now perform many of the essential functions previously performed exclusively by scanning machines, they still can’t guarantee a perfect quality output for Optical Character Recognition (OCR) services. If you have a scanner available to you, scanning documents is still the way to go — and our Scanned Image to Text API can convert these scanned documents to text with extremely high fidelity.
This API is intended exclusively for use with scanned images of documents (we offer a different API solution for documents photographed by any type of smartphone or camera), and it provides the option to increase the fault tolerance of your scan by setting a ‘recognitionMode’ field to values including Basic (not resilient to rotation, skew or low-quality images), Normal (highly fault tolerant) and Advanced (highest quality & most fault-tolerant).
In addition, this API supports dozens of common languages — including English, Arabic, Chinese & more, which can be enabled by entering your desired language’s three-letter abbreviation into the appropriate field — and offers an optional preprocessing feature (automatically enabled; disable with the string value ‘None’) which enhances the quality of the scanned image prior to performing the scan.
Using the complementary Golang code examples provided below, you can easily take advantage of this API & include its service within your OCR applications. You can use it completely free by registering a free account on our website (this will yield a free-tier API key valid for 800 API calls per month).
Simply copy the below code into your file, include your API key string & file inputs in their respective fields, and you’re good to go:
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/ocr/image/toText"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("imageFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("recognitionMode", "<string>")
req.Header.Add("language", "<string>")
req.Header.Add("preprocessing", "<string>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
Please note that higher fault tolerance settings will increase the number of API calls used per document. Basic mode will use 1–2 API calls, Normal mode will use 26–30 API calls, and Advanced (the default setting) will use 28–30 API calls per document.