mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 14:40:12 +01:00
🎨 改进 ocr 后台任务
This commit is contained in:
parent
c789f59b67
commit
98230a6f90
1 changed files with 29 additions and 26 deletions
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue