How to Detect and Un-Rotate a Document (for OCR) using Golang
Handheld photographs of physical documents are often skewed (rotated) at various odd angles, which makes them difficult for an OCR service to accurately read and subsequently break down into plain text. Thankfully, our Detect and Un-rotate Image API can automatically detect and correct these slight imperfections in a document image, enabling downstream OCR services to perform their function with much higher accuracy.
To use this API for free, you just need to copy & paste the Golang code examples provided below and authenticate the service with a free-tier Cloudmersive API key (you can get one by registering a free account on our website).
Simply add the below code examples to your file, include your file path & API key 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/preprocessing/image/unrotate"
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("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))
}