Skip to content
This repository has been archived by the owner on Dec 4, 2019. It is now read-only.

Commit

Permalink
优化:baidu-ocr ak从cache中获取
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzl committed Jan 12, 2018
1 parent 8e39db0 commit c57bd4a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
23 changes: 23 additions & 0 deletions cache/cache.go
@@ -0,0 +1,23 @@
package cache

import (
"time"

cache "github.com/patrickmn/go-cache"
)

func init() {
//初始化cache
GetCache()
}

var c *cache.Cache

//GetCache 获取cache对象
func GetCache() *cache.Cache {
if c != nil {
return c
}
c = cache.New(5*time.Minute, 10*time.Minute)
return c
}
14 changes: 8 additions & 6 deletions image.go
Expand Up @@ -11,12 +11,14 @@ import (
)

func saveImage(png image.Image, cfg *config.Config) error {
screenshotPath := fmt.Sprintf("%s/screenshot.png", proto.ImagePath)
err := util.SavePNG(screenshotPath, png)
if err != nil {
return fmt.Errorf("保存截图失败,%v", err)
}
log.Debugf("保存完整截图成功,%s", screenshotPath)
go func() {
screenshotPath := fmt.Sprintf("%sscreenshot.png", proto.ImagePath)
err := util.SavePNG(screenshotPath, png)
if err != nil {
log.Errorf("保存截图失败,%v", err)
}
log.Debugf("保存完整截图成功,%s", screenshotPath)
}()

//裁剪图片
questionImg, answerImg, err := cutImage(png, cfg)
Expand Down
21 changes: 15 additions & 6 deletions ocr/baidu.go
Expand Up @@ -7,8 +7,11 @@ import (
"regexp"
"strings"
"sync"
"time"

"github.com/silenceper/qanswer/cache"
"github.com/silenceper/qanswer/config"
"github.com/silenceper/qanswer/proto"
"github.com/silenceper/qanswer/util"
)

Expand All @@ -17,7 +20,6 @@ type Baidu struct {
apiKey string
secretKey string

accessToken string
sync.RWMutex
}

Expand Down Expand Up @@ -56,7 +58,7 @@ func (baidu *Baidu) GetText(imgPath string) (string, error) {

postData := url.Values{}
postData.Add("image", base64Data)
body, err := util.PostForm(uri, postData, 5)
body, err := util.PostForm(uri, postData, 6)
if err != nil {
return "", err
}
Expand All @@ -69,7 +71,7 @@ func (baidu *Baidu) GetText(imgPath string) (string, error) {
replaceRe, _ := regexp.Compile(`^[1-9]{0,1}`)
for k, words := range wordResults.WordsResult {
//TIPS: 去除第一个数字 1-9
//虽然有12个字符,但是 10-12 与最后的数字识别混在一起了
//虽然有12个数字,但是 10-12 与最后的数字识别混在一起了
if k == 0 {
words.Words = replaceRe.ReplaceAllString(words.Words, "")
}
Expand All @@ -82,8 +84,11 @@ func (baidu *Baidu) GetText(imgPath string) (string, error) {
func (baidu *Baidu) getAccessToken() (accessToken string, err error) {
baidu.Lock()
defer baidu.Unlock()
if baidu.accessToken != "" {
accessToken = baidu.accessToken

c := cache.GetCache()
cacheAccessToken, found := c.Get(proto.BaiduAccessTokenKey)
if found {
accessToken = cacheAccessToken.(string)
return
}
uri := fmt.Sprintf("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s", baidu.apiKey, baidu.secretKey)
Expand All @@ -98,6 +103,10 @@ func (baidu *Baidu) getAccessToken() (accessToken string, err error) {
return
}
accessToken = res.AccessToken
baidu.accessToken = accessToken
if accessToken != "" {
//set cache
c.Set(proto.BaiduAccessTokenKey, accessToken, time.Second*time.Duration((res.ExpiresIn-100)))
}

return
}
2 changes: 2 additions & 0 deletions proto/const.go
Expand Up @@ -10,4 +10,6 @@ const (
ImagePath = "./images/"
QuestionImage = ImagePath + "question.png"
AnswerImage = ImagePath + "answer.png"

BaiduAccessTokenKey = "qanswer_baidu_access_token"
)
6 changes: 6 additions & 0 deletions vendor/vendor.json
Expand Up @@ -46,6 +46,12 @@
"revision": "63ad056c2e74a796684a8e9312a839aa5c76c8f3",
"revisionTime": "2017-12-03T14:31:49Z"
},
{
"checksumSHA1": "JVGDxPn66bpe6xEiexs1r+y6jF0=",
"path": "github.com/patrickmn/go-cache",
"revision": "a3647f8e31d79543b2d0f0ae2fe5c379d72cedc0",
"revisionTime": "2017-07-22T04:01:10Z"
},
{
"checksumSHA1": "oBjdFvDUZpioGsB6URqe16hDQUg=",
"origin": "github.com/fatih/color/vendor/golang.org/x/sys/unix",
Expand Down

0 comments on commit c57bd4a

Please sign in to comment.