tesseract  5.0.0-alpha-619-ge9db
tesstrain.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # (C) Copyright 2014, Google Inc.
4 # (C) Copyright 2018, James R Barlow
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # This script provides an easy way to execute various phases of training
16 # Tesseract. For a detailed description of the phases, see
17 # https://github.com/tesseract-ocr/tesseract/wiki/TrainingTesseract
18 
19 import logging
20 import os
21 import sys
22 
23 if (sys.version_info.major < 3) or (sys.version_info.major == 3 and sys.version_info.minor < 6):
24  raise Exception("Must be using Python minimum version 3.6!")
25 
26 sys.path.insert(0, os.path.dirname(__file__))
27 from tesstrain_utils import (
28  parse_flags,
29  initialize_fontconfig,
30  phase_I_generate_image,
31  phase_UP_generate_unicharset,
32  phase_E_extract_features,
33  make_lstmdata,
34  cleanup,
35 )
36 import language_specific
37 
38 log = logging.getLogger()
39 
40 
42  log.setLevel(logging.DEBUG)
43  console = logging.StreamHandler()
44  console.setLevel(logging.INFO)
45  console_formatter = logging.Formatter(
46  "[%(asctime)s] %(levelname)s - %(message)s", datefmt="%H:%M:%S"
47  )
48  console.setFormatter(console_formatter)
49  log.addHandler(console)
50 
51 
52 def setup_logging_logfile(logfile):
53  logfile = logging.FileHandler(logfile, encoding='utf-8')
54  logfile.setLevel(logging.DEBUG)
55  logfile_formatter = logging.Formatter(
56  "[%(asctime)s] - %(levelname)s - %(name)s - %(message)s"
57  )
58  logfile.setFormatter(logfile_formatter)
59  log.addHandler(logfile)
60  return logfile
61 
62 
63 def main():
65  ctx = parse_flags()
66  logfile = setup_logging_logfile(ctx.log_file)
67  if not ctx.linedata:
68  log.error("--linedata_only is required since only LSTM is supported")
69  sys.exit(1)
70 
71  log.info(f"=== Starting training for language {ctx.lang_code}")
72  ctx = language_specific.set_lang_specific_parameters(ctx, ctx.lang_code)
73 
75  phase_I_generate_image(ctx, par_factor=8)
77 
78  if ctx.linedata:
79  phase_E_extract_features(ctx, ["--psm", "6", "lstm.train"], "lstmf")
80  make_lstmdata(ctx)
81 
82  log.removeHandler(logfile)
83  logfile.close()
84  cleanup(ctx)
85  log.info("All done!")
86  return 0
87 
88 
89 if __name__ == "__main__":
90  main()
91 
92 # _rc0 = subprocess.call(["tlog","\n=== Starting training for language '"+str(LANG_CODE.val)+"'"],shell=True)
93 # _rc0 = subprocess.call(["source",os.popen("dirname "+__file__).read().rstrip("\n")+"/language-specific.sh"],shell=True)
94 # _rc0 = subprocess.call(["set_lang_specific_parameters",str(LANG_CODE.val)],shell=True)
95 # _rc0 = subprocess.call(["initialize_fontconfig"],shell=True)
96 # _rc0 = subprocess.call(["phase_I_generate_image","8"],shell=True)
97 # _rc0 = subprocess.call(["phase_UP_generate_unicharset"],shell=True)
98 # if (LINEDATA ):
99 # subprocess.call(["phase_E_extract_features"," --psm 6 lstm.train ","8","lstmf"],shell=True)
100 # subprocess.call(["make__lstmdata"],shell=True)
101 # subprocess.call(["tlog","\nCreated starter traineddata for language '"+str(LANG_CODE.val)+"'\n"],shell=True)
102 # subprocess.call(["tlog","\nRun lstmtraining to do the LSTM training for language '"+str(LANG_CODE.val)+"'\n"],shell=True)
103 # else:
104 # subprocess.call(["phase_D_generate_dawg"],shell=True)
105 # subprocess.call(["phase_E_extract_features","box.train","8","tr"],shell=True)
106 # subprocess.call(["phase_C_cluster_prototypes",str(TRAINING_DIR.val)+"/"+str(LANG_CODE.val)+".normproto"],shell=True)
107 # if (str(ENABLE_SHAPE_CLUSTERING.val) == "y" ):
108 # subprocess.call(["phase_S_cluster_shapes"],shell=True)
109 # subprocess.call(["phase_M_cluster_microfeatures"],shell=True)
110 # subprocess.call(["phase_B_generate_ambiguities"],shell=True)
111 # subprocess.call(["make__traineddata"],shell=True)
112 # subprocess.call(["tlog","\nCompleted training for language '"+str(LANG_CODE.val)+"'\n"],shell=True)
tesstrain_utils.parse_flags
def parse_flags(argv=None)
Definition: tesstrain_utils.py:216
tesstrain_utils.make_lstmdata
def make_lstmdata(ctx)
Definition: tesstrain_utils.py:653
tesstrain_utils.cleanup
def cleanup(ctx)
Definition: tesstrain_utils.py:288
tesstrain_utils.phase_I_generate_image
def phase_I_generate_image(ctx, par_factor=None)
Definition: tesstrain_utils.py:369
language_specific.set_lang_specific_parameters
def set_lang_specific_parameters(ctx, lang)
Definition: language_specific.py:894
tesstrain.main
def main()
Definition: tesstrain.py:63
tesstrain_utils.initialize_fontconfig
def initialize_fontconfig(ctx)
Definition: tesstrain_utils.py:295
tesstrain.setup_logging_logfile
def setup_logging_logfile(logfile)
Definition: tesstrain.py:52
tesstrain_utils.phase_E_extract_features
def phase_E_extract_features(ctx, box_config, ext)
Definition: tesstrain_utils.py:525
tesstrain_utils.phase_UP_generate_unicharset
def phase_UP_generate_unicharset(ctx)
Definition: tesstrain_utils.py:421
tesstrain.setup_logging_console
def setup_logging_console()
Definition: tesstrain.py:41