In this post, i am going to show how i extract text from image (base64) by using Go and Azure Cognitive Services. I think the base64 input is be useful, so we do not need to upload image to get the URL for extracting.

Register Cognitive Services.

After registering, we will have the Key1 and Endpoint. Here, i only use Free Tier.

My helpful screenshot

Go code

type respDat struct {
	Language    string `json:"language"`
	Orientation string `json:"orientation"`
	Regions     []struct {
		BoundingBox string `json:"boundingBox"`
		Lines       []struct {
			BoundingBox string `json:"boundingBox"`
			Words       []struct {
				BoundingBox string `json:"boundingBox"`
				Text        string `json:"text"`
			} `json:"words"`
		} `json:"lines"`
	} `json:"regions"`
	TextAngle int `json:"textAngle"`
}

func readText(base64Data string) {
	subscriptionKey := "your key"
	const uri = yourendpoint + "vision/v2.1/ocr"
	client := &http.Client{
		Timeout: time.Second * 2,
	}

	// The actual image starts after the ","
	i := strings.Index(base64Data, ",")
	if i < 0 {
		log.Fatal("no comma")
	}

	// pass reader to NewDecoder
	dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(base64Data[i+1:]))

	// Create the Post request, passing the image base64 in the request body
	req, err := http.NewRequest("POST", uri, dec)
	if err != nil {
		fmt.Println(err)
		panic(err)
	}

	// Add headers
	req.Header.Add("Content-Type", "application/octet-stream")
	req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)

	// Send the request and retrieve the response
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		panic(err)
	}

	defer resp.Body.Close()

	// Read the response body.
	// Note, data is a byte array
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}

	// Parse the Json data
	var f respDat
	json.Unmarshal(body, &f)
	
	fmt.Println(f)
}

I am thinking about the different way is uploading the image to the Azure and after using it, it will be deleted , but i have not figured out yet.