Azure ocr with Go

These are options to make request with OCR (Azure Cognitive Services) in GO:

  1. Read from file :
file, err := os.Open("img.png")
if err != nil {
	panic(err)
}
defer file.Close()

client := &http.Client{}
req, err := http.NewRequest("POST", "https://endpoint/vision/v3.0/ocr", file)
req.Header.Add("Content-Type", "application/octet-stream")
req.Header.Add("Ocp-Apim-Subscription-Key", "key")

//Handle Error
if err != nil {
        log.Fatalf("An Error Occured %v", err)
}
resp, err := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))

2. Read from base64 string:

sEnc, _ := b64.StdEncoding.DecodeString(base64string)

client := &http.Client{}
req, err := http.NewRequest("POST", "https://endpoint/vision/v3.0/ocr", bytes.NewReader([]byte(sEnc)))
req.Header.Add("Content-Type", "application/octet-stream")
req.Header.Add("Ocp-Apim-Subscription-Key", "key")

//Handle Error
if err != nil {
	log.Fatalf("An Error Occured %v", err)
}
resp, err := client.Do(req)
defer resp.Body.Close()

body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))

Leave a comment