Tesseract  3.02
tesseract-ocr/training/combine_tessdata.cpp
Go to the documentation of this file.
00001 
00002 // File:        combine_tessdata
00003 // Description: Creates a unified traineddata file from several
00004 //              data files produced by the training process.
00005 // Author:      Daria Antonova
00006 // Created:     Wed Jun 03 11:26:43 PST 2009
00007 //
00008 // (C) Copyright 2009, Google Inc.
00009 // Licensed under the Apache License, Version 2.0 (the "License");
00010 // you may not use this file except in compliance with the License.
00011 // You may obtain a copy of the License at
00012 // http://www.apache.org/licenses/LICENSE-2.0
00013 // Unless required by applicable law or agreed to in writing, software
00014 // distributed under the License is distributed on an "AS IS" BASIS,
00015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016 // See the License for the specific language governing permissions and
00017 // limitations under the License.
00018 //
00020 
00021 #include "tessdatamanager.h"
00022 
00023 // Main program to combine/extract/overwrite tessdata components
00024 // in [lang].traineddata files.
00025 //
00026 // To combine all the individual tessdata components (unicharset, DAWGs,
00027 // classifier templates, ambiguities, language configs) located at, say,
00028 // /home/$USER/temp/eng.* run:
00029 //
00030 //   combine_tessdata /home/$USER/temp/eng.
00031 //
00032 // The result will be a combined tessdata file /home/$USER/temp/eng.traineddata
00033 //
00034 // Specify option -e if you would like to extract individual components
00035 // from a combined traineddata file. For example, to extract language config
00036 // file and the unicharset from tessdata/eng.traineddata run:
00037 //
00038 //   combine_tessdata -e tessdata/eng.traineddata
00039 //   /home/$USER/temp/eng.config /home/$USER/temp/eng.unicharset
00040 //
00041 // The desired config file and unicharset will be written to
00042 // /home/$USER/temp/eng.config /home/$USER/temp/eng.unicharset
00043 //
00044 // Specify option -o to overwrite individual components of the given
00045 // [lang].traineddata file. For example, to overwrite language config
00046 // and unichar ambiguities files in tessdata/eng.traineddata use:
00047 //
00048 //   combine_tessdata -o tessdata/eng.traineddata
00049 //   /home/$USER/temp/eng.config /home/$USER/temp/eng.unicharambigs
00050 //
00051 // As a result, tessdata/eng.traineddata will contain the new language config
00052 // and unichar ambigs, plus all the original DAWGs, classifier teamples, etc.
00053 //
00054 // Note: the file names of the files to extract to and to overwrite from should
00055 // have the appropriate file suffixes (extensions) indicating their tessdata
00056 // component type (.unicharset for the unicharset, .unicharambigs for unichar
00057 // ambigs, etc). See k*FileSuffix variable in ccutil/tessdatamanager.h.
00058 //
00059 // Specify option -u to unpack all the components to the specified path:
00060 //
00061 // combine_tessdata -u tessdata/eng.traineddata /home/$USER/temp/eng.
00062 //
00063 // This will create  /home/$USER/temp/eng.* files with individual tessdata
00064 // components from tessdata/eng.traineddata.
00065 //
00066 int main(int argc, char **argv) {
00067   int i;
00068   if (argc == 2) {
00069     printf("Combining tessdata files\n");
00070     STRING output_file = argv[1];
00071     output_file += kTrainedDataSuffix;
00072     if (!tesseract::TessdataManager::CombineDataFiles(
00073         argv[1], output_file.string())) {
00074       char* last = &argv[1][strlen(argv[1])-1];
00075       printf("Error combining tessdata files into %s\n",
00076              output_file.string());
00077       if (*last != '.')
00078         printf("Hint: the prefix is missing a period (.)\n");
00079     }
00080   } else if (argc >= 4 && (strcmp(argv[1], "-e") == 0 ||
00081                            strcmp(argv[1], "-u") == 0)) {
00082     // Initialize TessdataManager with the data in the given traineddata file.
00083     tesseract::TessdataManager tm;
00084     tm.Init(argv[2], 0);
00085     printf("Extracting tessdata components from %s\n", argv[2]);
00086     if (strcmp(argv[1], "-e") == 0) {
00087       for (i = 3; i < argc; ++i) {
00088         if (tm.ExtractToFile(argv[i])) {
00089           printf("Wrote %s\n", argv[i]);
00090         } else {
00091           printf("Not extracting %s, since this component"
00092                  " is not present\n", argv[i]);
00093         }
00094       }
00095     } else {  // extract all the components
00096       for (i = 0; i < tesseract::TESSDATA_NUM_ENTRIES; ++i) {
00097         STRING filename = argv[3];
00098         filename += tesseract::kTessdataFileSuffixes[i];
00099         if (tm.ExtractToFile(filename.string())) {
00100           printf("Wrote %s\n", filename.string());
00101         }
00102       }
00103     }
00104     tm.End();
00105   } else if (argc >= 4 && strcmp(argv[1], "-o") == 0) {
00106     // Rename the current traineddata file to a temporary name.
00107     const char *new_traineddata_filename = argv[2];
00108     STRING traineddata_filename = new_traineddata_filename;
00109     traineddata_filename += ".__tmp__";
00110     if (rename(new_traineddata_filename, traineddata_filename.string()) != 0) {
00111       tprintf("Failed to create a temporary file %s\n",
00112               traineddata_filename.string());
00113       exit(1);
00114     }
00115 
00116     // Initialize TessdataManager with the data in the given traineddata file.
00117     tesseract::TessdataManager tm;
00118     tm.Init(traineddata_filename.string(), 0);
00119 
00120     // Write the updated traineddata file.
00121     tm.OverwriteComponents(new_traineddata_filename, argv+3, argc-3);
00122     tm.End();
00123   } else {
00124     printf("Usage for combining tessdata components:\n"
00125            "%s language_data_path_prefix (e.g. tessdata/eng.)\n", argv[0]);
00126     printf("Usage for extracting tessdata components:\n"
00127            "%s -e traineddata_file [output_component_file...]\n", argv[0]);
00128     printf("Usage for overwriting tessdata components:\n"
00129            "%s -o traineddata_file [input_component_file...]\n", argv[0]);
00130     printf("Usage for unpacking all tessdata components:\n"
00131            "%s -u traineddata_file output_path_prefix"
00132            " (e.g. /tmp/eng.)\n", argv[0]);
00133     return 1;
00134   }
00135 }