Home >Backend Development >Golang >Golang connects to Baidu AI interface to realize text recognition function and get started quickly

Golang connects to Baidu AI interface to realize text recognition function and get started quickly

王林
王林Original
2023-08-26 15:42:291030browse

Golang connects to Baidu AI interface to realize text recognition function and get started quickly

Golang connects to Baidu AI interface to implement text recognition function, quick start

Overview:
In today's era of information explosion, text recognition technology has become a powerful Powerful tool. Baidu AI open platform provides a powerful text recognition interface. This article will introduce how to use Golang language to connect to Baidu AI interface to realize text recognition function. Through the quick start of this article, readers will be able to quickly get started and use Baidu AI's text recognition service.

Step 1: Apply for a Baidu AI Open Platform Account
First, we need to register an account on the Baidu AI Open Platform and create a text recognition application. When creating an application, you need to manually activate the "Text Recognition" service and obtain the "API Key" and "Secret Key".

Step 2: Install Golang’s HTTP request library
In Go language, we can use third-party libraries to send HTTP requests. Here we recommend using the official "net/http" library, which is simple to use and powerful. You can install the library through the following command:

go get -u github.com/go-redis/redis/v8

Step 3: Write code to call Baidu AI interface
The following sample code demonstrates how to use Golang to connect to Baidu AI interface to implement text recognition function:

package main
  
import (
    "bytes"
    "encoding/base64"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "os"
    "path/filepath"
)
  
const (
    apiKey = "YOUR_API_KEY"
    secretKey = "YOUR_SECRET_KEY"
)
  
type AccessTokenResponse struct {
    AccessToken string `json:"access_token"`
}
  
type OCRResponse struct {
    WordsResult []struct {
        Words string `json:"words"`
    } `json:"words_result"`
}
  
func main() {
    // 读取图片文件
    imageFile, err := os.Open("example.jpg")
    defer imageFile.Close()
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // 将图片文件编码为base64字符串
    fileInfo, _ := imageFile.Stat()
    size := fileInfo.Size()
    buffer := make([]byte, size)
    imageFile.Read(buffer)
    imageBase64 := base64.StdEncoding.EncodeToString(buffer)
  
    // 获取百度AI接口的access_token
    accessToken, err := getAccessToken(apiKey, secretKey)
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // 调用百度AI接口进行文字识别
    ocrResponse, err := ocr(accessToken, imageBase64)
    if err != nil {
        fmt.Println(err)
        return
    }
  
    // 提取识别结果
    for _, words := range ocrResponse.WordsResult {
        fmt.Println(words.Words)
    }
}
  
// 获取百度AI接口的access_token
func getAccessToken(apiKey string, secretKey string) (string, error) {
    client := &http.Client{}
    request, err := http.NewRequest("GET", "https://aip.baidubce.com/oauth/2.0/token", nil)
    if err != nil {
        return "", err
    }
    q := request.URL.Query()
    q.Add("grant_type", "client_credentials")
    q.Add("client_id", apiKey)
    q.Add("client_secret", secretKey)
    request.URL.RawQuery = q.Encode()
  
    response, err := client.Do(request)
    if err != nil {
        return "", err
    }
    defer response.Body.Close()
  
    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        return "", err
    }
  
    var accessTokenResponse AccessTokenResponse
    err = json.Unmarshal(body, &accessTokenResponse)
    if err != nil {
        return "", err
    }
  
    return accessTokenResponse.AccessToken, nil
}
  
// 调用百度AI接口进行文字识别
func ocr(accessToken string, imageBase64 string) (OCRResponse, error) {
    client := &http.Client{}
    request, err := http.NewRequest("POST", "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic", bytes.NewBuffer([]byte(imageBase64)))
    if err != nil {
        return OCRResponse{}, err
    }
    request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  
    q := request.URL.Query()
    q.Add("access_token", accessToken)
    request.URL.RawQuery = q.Encode()
  
    response, err := client.Do(request)
    if err != nil {
        return OCRResponse{}, err
    }
    defer response.Body.Close()
  
    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        return OCRResponse{}, err
    }
  
    var ocrResponse OCRResponse
    err = json.Unmarshal(body, &ocrResponse)
    if err != nil {
        return OCRResponse{}, err
    }
  
    return ocrResponse, nil
}

In the code, you first need to fill in the API Key and Secret Key you applied for into the apiKey and secretKey constants. Then, obtain the access_token of the Baidu AI interface through the getAccessToken function, and then call the Baidu AI interface through the ocr function for text recognition. Finally, extract the recognition results and output them.

Step 4: Run the code and obtain the recognition results
Save the above code as a Go source file (for example, main.go), and then execute the following command on the command line to run the code:

go run main.go

The code will read the example.jpg image file in the current directory and output the result to the console. Based on actual needs, you can replace the images in the examples with your own image files.

Summary:
Through the above steps, we successfully used Golang to connect to Baidu AI interface and realize the text recognition function. You can modify and extend the code according to your needs to meet more complex application scenarios. I hope this article can help you and enable you to better understand and use Baidu AI's text recognition service.

The above is the detailed content of Golang connects to Baidu AI interface to realize text recognition function and get started quickly. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn