From 98230a6f90d013c8afce5888a6873062450af067 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 27 Jan 2023 15:30:14 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=20ocr=20=E5=90=8E?= =?UTF-8?q?=E5=8F=B0=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/ocr.go | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/kernel/model/ocr.go b/kernel/model/ocr.go index 899029274..e45318261 100644 --- a/kernel/model/ocr.go +++ b/kernel/model/ocr.go @@ -32,34 +32,37 @@ func autoOCRAssets() { assetsPath := util.GetDataAssetsAbsPath() assets := getUnOCRAssetsAbsPaths() - - poolSize := runtime.NumCPU() - if 4 < poolSize { - poolSize = 4 - } - waitGroup := &sync.WaitGroup{} - p, _ := ants.NewPoolWithFunc(poolSize, func(arg interface{}) { - defer waitGroup.Done() - - assetAbsPath := arg.(string) - text := util.Tesseract(assetAbsPath) - p := strings.TrimPrefix(assetAbsPath, assetsPath) - p = "assets" + filepath.ToSlash(p) - util.AssetsTextsLock.Lock() - util.AssetsTexts[p] = text - util.AssetsTextsLock.Unlock() - util.AssetsTextsChanged = true - }) - for i, assetAbsPath := range assets { - waitGroup.Add(1) - p.Invoke(assetAbsPath) - - if 63 <= i { // 一次任务中最多处理 64 张图片,防止卡顿 - break + if 0 < len(assets) { + poolSize := runtime.NumCPU() + if 4 < poolSize { + poolSize = 4 } + waitGroup := &sync.WaitGroup{} + p, _ := ants.NewPoolWithFunc(poolSize, func(arg interface{}) { + defer waitGroup.Done() + + assetAbsPath := arg.(string) + text := util.Tesseract(assetAbsPath) + p := strings.TrimPrefix(assetAbsPath, assetsPath) + p = "assets" + filepath.ToSlash(p) + util.AssetsTextsLock.Lock() + util.AssetsTexts[p] = text + util.AssetsTextsLock.Unlock() + util.AssetsTextsChanged = true + }) + + for i, assetAbsPath := range assets { + waitGroup.Add(1) + p.Invoke(assetAbsPath) + + if 63 <= i { // 一次任务中最多处理 64 张图片,防止卡顿 + break + } + } + + waitGroup.Wait() + p.Release() } - waitGroup.Wait() - p.Release() cleanNotExistAssetsTexts() }