Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Vanessa 2023-02-06 17:47:54 +08:00
commit 3695cad55e
2 changed files with 8 additions and 22 deletions

View file

@ -1,21 +1,18 @@
package model
import (
"github.com/siyuan-note/siyuan/kernel/task"
"io"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/88250/gulu"
"github.com/dustin/go-humanize"
"github.com/panjf2000/ants/v2"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/task"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -33,15 +30,7 @@ func autoOCRAssets() {
assetsPath := util.GetDataAssetsAbsPath()
assets := getUnOCRAssetsAbsPaths()
if 0 < len(assets) {
poolSize := runtime.NumCPU()
if 2 < poolSize {
poolSize = 2
}
waitGroup := &sync.WaitGroup{}
p, _ := ants.NewPoolWithFunc(poolSize, func(arg interface{}) {
defer waitGroup.Done()
assetAbsPath := arg.(string)
for i, assetAbsPath := range assets {
text := util.Tesseract(assetAbsPath)
p := strings.TrimPrefix(assetAbsPath, assetsPath)
p = "assets" + filepath.ToSlash(p)
@ -49,19 +38,11 @@ func autoOCRAssets() {
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 张图片,防止卡顿
if 16 <= i { // 一次任务中最多处理 16 张图片,防止卡顿
break
}
}
waitGroup.Wait()
p.Release()
}
cleanNotExistAssetsTexts()

View file

@ -64,12 +64,17 @@ func IsTesseractExtractable(p string) bool {
return strings.HasSuffix(lowerName, ".png") || strings.HasSuffix(lowerName, ".jpg") || strings.HasSuffix(lowerName, ".jpeg")
}
// tesseractOCRLock 用于 Tesseract OCR 加锁串行执行提升稳定性 https://github.com/siyuan-note/siyuan/issues/7265
var tesseractOCRLock = sync.Mutex{}
func Tesseract(imgAbsPath string) string {
if ContainerStd != Container || !TesseractEnabled {
return ""
}
defer logging.Recover()
tesseractOCRLock.Lock()
defer tesseractOCRLock.Unlock()
if !IsTesseractExtractable(imgAbsPath) {
return ""