百度OCR文字识别go语言示例

使用go实现百度OCR文字识别接口

百度OCR文字识别提供了每天50000次免费接口的调用,但是没有提供go语言示例。下面是使用go写的一个示例

package baidu

import (
	"compress/gzip"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"os"
	"strings"
)

const (
	// 客户端凭证类型,固定为client_credentials
	grantType string = "client_credentials"
	// 应用的API Key
	apiKey string = ""
	// 应用的Secret Key
	secretKey string = ""
	// 授权服务地址
	tokenUrl string = "https://aip.baidubce.com/oauth/2.0/token"
	// 文字识别(高精度)API接口地址
	accurateBasicUrl string = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
)

type accessToken struct {
	RefreshToken string `json:"refresh_token"`
	ExpiresIn uint32 `json:"expires_in"`
	Scope string `json:"scope"`
	SessionKey string `json:"session_key"`
	AccessToken string `json:"access_token"`
	SessionSecret string `json:"session_secret"`
}

type WordsResult struct {
	Words string `json:"words"`
}

type Words struct {
	WordsResult []WordsResult `json:"words_result"`
	LogId uint64 `json:"log_id"`
	WordsResultNum uint32 `json:"words_result_num"`
}

// 获取access_token
// access_token有效期一般是30天
// 官方文档:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
func getAccessToken() (data accessToken) {
	requestUrl := fmt.Sprintf("%s?grant_type=%s&client_id=%s&client_secret=%s", tokenUrl, grantType, apiKey, secretKey)
	response, e := http.Get(requestUrl)
	defer response.Body.Close()
	printError(e)
	s := unCoding(response)
	e = json.Unmarshal([]byte(s), &data)
	printError(e)
	return
}

// 文字识别(高精度)
// 官方文档:https://cloud.baidu.com/doc/OCR/s/1k3h7y3db
// img 图片地址
func AccurateBasic(img string) (data Words) {
	requestUrl := fmt.Sprintf("%s?access_token=%s", accurateBasicUrl, getAccessToken().AccessToken)
	f, e := os.Open(img)
	printError(e)
	defer f.Close()
	d, e := ioutil.ReadAll(f)
	printError(e)
	const base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	var coder = base64.NewEncoding(base64Table)
	imgString := coder.EncodeToString(d)
	printError(e)
	values := url.Values{"image": {imgString}}
	response ,e := http.Post(requestUrl, "application/x-www-form-urlencoded", strings.NewReader(values.Encode()))
	defer response.Body.Close()
	s := unCoding(response)
	e = json.Unmarshal([]byte(s), &data)
	printError(e)
	return
}

func unCoding(r *http.Response) (body string) {
	if r.StatusCode == 200 {
		switch r.Header.Get("Content-Encoding") {
		case "gzip":
			reader, _ := gzip.NewReader(r.Body)
			for {
				buf := make([]byte, 1024)
				n, err := reader.Read(buf)
				if err != nil && err != io.EOF {
					panic(err)
				}
				if n == 0 {
					break
				}
				body += string(buf)
			}
		default:
			bodyByte, _ := ioutil.ReadAll(r.Body)
			body = string(bodyByte)
		}
	} else {
		bodyByte, _ := ioutil.ReadAll(r.Body)
		body = string(bodyByte)
	}
	return
}

func printError(e error) {
	if e != nil {
		log.Println(e)
		os.Exit(1)
	}
}

测试一下

func main() {
	fmt.Println(baidu.AccurateBasic("./test.jpg"))
}

图片如下

在这里插入图片描述

识别如下

在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值