All Products
Search
Document Center

Content Moderation:Image OCR

Last Updated:Dec 29, 2025

This topic describes how to use the Go SDK image OCR API to detect text information from images.

Features

In addition to text in common images, the general OCR feature can recognize text on structured cards. For more information about the parameters, see Image OCR detection API.

Note
  • This SDK supports only image URLs. It does not support local files or binary data.

  • Supported URL types: public HTTP or HTTPS URLs up to 2,048 characters long.

Prerequisites

Go dependencies are installed. For more information, see Installation.

Note

You must use the required Go version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.

Submit a synchronous image detection task

Operation

Description

Supported region

ImageSyncScanRequest

Submits synchronous OCR tasks with the scenes parameter set to ocr to recognize text in images.

  • cn-shanghai

  • cn-beijing

  • cn-shenzhen

  • ap-southeast-1

Sample code

package main

import (
   "encoding/json"
   "fmt"
   "github.com/aliyun/alibaba-cloud-sdk-go/services/green"
   "strconv"
)

func main() {
   /**
    * Note: Reuse the instantiated client to improve detection performance. This avoids repeated connections.
    * Common ways to obtain environment variables:
    *     Obtain the AccessKey ID of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
    *     Obtain the AccessKey secret of a RAM user: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
    */
   client, _err := green.NewClientWithAccessKey(
        "cn-shanghai", 
        "Obtain the AccessKey ID of a RAM user from an environment variable.", 
        "Obtain the AccessKey secret of a RAM user from an environment variable.")
   if err != nil {
      fmt.Println(err.Error())
      return
   }

   task1 := map[string]interface{}{"dataId": "The ID of the data to detect.", "url": "The URL of the image to detect."}
   // Example: Recognize the front of an ID card.
   cardExtras := map[string]interface{}{"card": "id-card-front"}
   // Example: Recognize the back of an ID card.
   // cardExtras := map[string]interface{}{"card": "id-card-back"}
   // scenes: The detection scenario.
   content, _ := json.Marshal(
      map[string]interface{}{
         "tasks": task1, "scenes": [...]string{"ocr"}, "bizType": "The business scenario.", "extras": cardExtras,
      },
   )

   request := green.CreateImageSyncScanRequest()
   request.SetContent(content)
   response, _err := client.ImageSyncScan(request)
   if _err != nil {
      fmt.Println(_err.Error())
      return
   }
   if response.GetHttpStatus() != 200 {
      fmt.Println("response not success. status:" + strconv.Itoa(response.GetHttpStatus()))
   }
   fmt.Println(response.GetHttpContentString())
}