added tesseract threads

This commit is contained in:
ston1th 2019-05-05 21:07:34 +02:00
commit 84d48a6eea
4 changed files with 41 additions and 21 deletions

View file

@ -30,6 +30,7 @@ const (
defRunGroup = "docstore" defRunGroup = "docstore"
defTimeout = 600 defTimeout = 600
tesseractThreads = 1
) )
var ( var (
@ -71,8 +72,11 @@ func initServer(cfg core.Config) (err error) {
log.InitLogger(cfg) log.InitLogger(cfg)
cfg.DataDir = filepath.Join(cfg.RunDir, core.DataDir) cfg.DataDir = filepath.Join(cfg.RunDir, core.DataDir)
debugCfg := cfg
debugCfg.Secret = "***hidden***"
log.Debugf("docstore config: %+v", debugCfg)
scanner, err := scan.New(cfg.DataDir, cfg.Langs, time.Second*time.Duration(cfg.CMDTimeout)) scanner, err := scan.New(cfg.DataDir, cfg.Langs, cfg.TesseractThreads, time.Second*time.Duration(cfg.CMDTimeout))
if err != nil { if err != nil {
return errors.New("scanner: " + err.Error()) return errors.New("scanner: " + err.Error())
} }
@ -255,6 +259,12 @@ func serverFlags() []cli.Flag {
Usage: "timeout in seconds for executed teseract and pdftotext commands", Usage: "timeout in seconds for executed teseract and pdftotext commands",
Destination: &config.CMDTimeout, Destination: &config.CMDTimeout,
}, },
cli.IntFlag{
Name: "threads",
Value: tesseractThreads,
Usage: "tesseract thread limit (1-4)",
Destination: &config.TesseractThreads,
},
cli.BoolFlag{ cli.BoolFlag{
Name: "foreground, f", Name: "foreground, f",
Usage: "do not fork into the background", Usage: "do not fork into the background",

View file

@ -44,6 +44,7 @@ type Config struct {
Secret string `json:"secret,omitempty"` Secret string `json:"secret,omitempty"`
Langs []string `json:"langs,omitempty"` Langs []string `json:"langs,omitempty"`
CMDTimeout int `json:"cmd_timeout,omitempty"` CMDTimeout int `json:"cmd_timeout,omitempty"`
TesseractThreads int `json:"tesseract_threads,omitempty"`
Foreground bool `json:"foreground,omitempty"` Foreground bool `json:"foreground,omitempty"`
SecureCookie bool `json:"secure_cookie,omitempty"` SecureCookie bool `json:"secure_cookie,omitempty"`
Debug bool `json:"debug,omitempty"` Debug bool `json:"debug,omitempty"`

View file

@ -13,11 +13,15 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
"runtime"
"time" "time"
) )
const pdfExt = ".pdf" const (
pdfExt = ".pdf"
minThreads = 1
maxThreads = 4
)
type PDF struct { type PDF struct {
cmd string cmd string
@ -46,7 +50,7 @@ type Scanner struct {
p PDF p PDF
} }
func New(base string, langs []string, timeout time.Duration) (s *Scanner, err error) { func New(base string, langs []string, threads int, timeout time.Duration) (s *Scanner, err error) {
var ( var (
tcmd string tcmd string
pcmd string pcmd string
@ -77,11 +81,16 @@ func New(base string, langs []string, timeout time.Duration) (s *Scanner, err er
lang += "+" lang += "+"
} }
} }
if threads > maxThreads {
threads = maxThreads
} else if threads < minThreads {
threads = minThreads
}
args := []string{"-l", lang, "--oem", tesseract_oem} args := []string{"-l", lang, "--oem", tesseract_oem}
s = &Scanner{ s = &Scanner{
base, base,
timeout, timeout,
Tesseract{tcmd, args, []string{fmt.Sprintf("OMP_THREAD_LIMIT=%d", runtime.NumCPU())}}, Tesseract{tcmd, args, []string{fmt.Sprintf("OMP_THREAD_LIMIT=%d", threads)}},
PDF{pcmd, []string{"-layout", "-nopgbrk", "-eol", "unix"}}, PDF{pcmd, []string{"-layout", "-nopgbrk", "-eol", "unix"}},
} }
return return

View file

@ -81,7 +81,7 @@ const (
<td>File</td> <td>File</td>
<td><div class="btn-group"> <td><div class="btn-group">
{{if $item.Scan}} {{if $item.Scan}}
<a href="#" class="btn btn-sm btn-warning">Scanning..</a> <a href="#" class="btn btn-sm btn-warning">Scanning</a>
{{else}} {{else}}
{{if not $item.Flag}} {{if not $item.Flag}}
<a href="/index{{$item.Abs}}" class="btn btn-sm btn-success">Index</a> <a href="/index{{$item.Abs}}" class="btn btn-sm btn-success">Index</a>
@ -109,7 +109,7 @@ const (
</ol> </ol>
<div class="btn-group btn-breadcrumb"> <div class="btn-group btn-breadcrumb">
{{if .Data.Scan}} {{if .Data.Scan}}
<a href="#" class="btn btn-sm btn-warning">Scanning..</a> <a href="#" class="btn btn-sm btn-warning">Scanning</a>
{{else}} {{else}}
{{if not .Data.Flag}} {{if not .Data.Flag}}
<a href="/index{{.Data.Name}}" class="btn btn-sm btn-success">Index</a> <a href="/index{{.Data.Name}}" class="btn btn-sm btn-success">Index</a>