A method for merging power signal model training files
By merging labeled Chinese training model files, the problems of low recognition rate and long processing time in the prior art Chinese training model are solved, and more efficient text recognition and lower workload are achieved.
Patent Information
- Application Number
- CN202011638466.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-31
- Publication Date
- 2025-02-11
- Estimated Expiration
- 2040-12-31
AI Technical Summary
In the prior art, the recognition rate of Chinese training models is low and the training file processing time is long, resulting in low recognition efficiency.
A power signal model training file merging method is adopted to generate the merged text model by selecting .box text files and .tif image files that have been manually marked, setting training parameters, automatically naming the files, and calling the tesseract command to generate the merged text model.
By merging multiple models, the training workload is reduced, the accuracy of text recognition is improved, and the processing time is shortened.
Smart Images

Figure CN112686319B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of Chinese training model training methods, and in particular relates to a method for merging power signal model training files. Background Art
[0002] The recognition rate of the Chinese training model that comes with the Tesseract text recognition engine is low. It is a common practice for users to improve the text recognition rate by retraining the commonly used texts. Since training requires a lot of adjustments to the position of the text in the image and the size of the text box, it brings a huge workload for text training. If the training files are not merged, the processing time is long and the recognition efficiency is low. Summary of the invention
[0003] The technical problem to be solved by the present invention is to provide a method for merging power signal model training files to solve the problems existing in the prior art.
[0004] The technical solution adopted by the present invention is: a method for merging power signal model training files, the method comprising the following steps:
[0005] 1) Select the manually marked and generated .box text file and the image file with the suffix .tif;
[0006] 2) Set the training parameters for each file, including the training language and page mode parameters. The default training language is Chinese.
[0007] 3) According to the selected file name and the model name to be generated, the image file and box file with the suffix .tif are automatically named to form a standardized training file. The standardized naming of the image file with the suffix .tif is as follows:
[0008]
[0009] The corresponding text file with the suffix .box is named as follows:
[0010]
[0011] 4) Based on the names of all image files with the suffix .tif, use the name between the first “.” and the second “.” as the font name, with each font being one line, and write them into a text file named font_properties in the following format.
[0012] font 0 0 0 0 0
[0013] 5) Call the tesseract command to generate a text file with the suffix .tr for each file with the suffix .tif. The command is as follows:
[0014] tesseract power.font.exp0.tif power.font.exp0–psm 6nobatch box.train
[0015] 6) Combine all text file names with the suffix .box into a string separated by spaces, use it as the parameter of the unicharset_extractor command, call and execute the unicharset_extractor command; combine all text file names with the suffix .tr into a string separated by spaces, use it as the parameter of the shapeclustering, mftraining, and cntraing commands, call and execute the shapeclustering, mftraining, and cntraing commands;
[0016] 7) Finally, call the combine_tessdata command of tessract to generate the .traineddata file, which is the final merged text model.
[0017] The method for generating the .box text file and the image file with the suffix .tif in step 1) comprises the following steps:
[0018] 1) Read txt or excel files;
[0019] 2) Set the parameters of the training text font, size, and model name;
[0020] 3) Read the selected txt or excel file line by line, get the total number of lines, recorded as num_lines, and the number of words in the line with the most words among these lines, recorded as max_length;
[0021] 4) Calculate the width and height of the generated text image based on the set character spacing (gap), line spacing (linespacing), page margin (padding), maximum image width, single text width (width), and single text height (height);
[0022] 5) If the calculated image width is greater than the set maximum image width, the longest image width is used as the width of the image to be generated, and the image height is recalculated based on the character spacing (gap), line spacing (linespacing), and page margin (padding);
[0023] 6) Based on the image size calculated in step 5), record it as imgsize, call Qt's QImage class to generate a completely white image, and save it to an image file with the suffix .tif;
[0024] 7) Draw individual words in order on a completely white picture;
[0025] 8) According to the position of each character and the length and width of the rectangle recorded in step 7), the pixel values of the image in the rectangular frame are scanned in four directions: from top to bottom, from bottom to top, from left to right, and from right to left;
[0026] 9) Convert the coordinates of the minimum circumscribed rectangle of the text in step 8) [(x, y), (end_x, endy)] to the text coordinate system trained by Tesseract [(t_x, t_y), (t_end_x, t_endy)]. The conversion formula is as follows:
[0027] t_x=x
[0028] t_y=height_image–y-(endy-y)
[0029] t_endx=x+width_image
[0030] t_endy=height_image–y;
[0031] 10) Perform coordinate transformation calculation for each character according to step 9), and write and save the data of each character into a text file with the suffix .box in the following format, with each character data occupying one line:
[0032] Text t_x t_y t_endx t_endy.
[0033] Beneficial effects of the present invention: Compared with the prior art, the present invention merges manually labeled text images in actual applications and text images generated by the present invention through a multi-model merging method, and adjusts the recognized erroneous text through manually labeled data in actual applications, thereby reducing the training workload and improving the accuracy of text recognition. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1 This is a flow chart of the model training method;
[0035] Figure 2 A flow chart of the combined method;
[0036] Figure 3 Schematic diagram of coordinate system transformation. DETAILED DESCRIPTION
[0037] The present invention is further described below in conjunction with specific drawings and embodiments.
[0038] Example 1: Figure 1As shown, an efficient model training method includes the following steps:
[0039] 1) The station names and status names in the power dispatching master station system are converted into txt or execl format files, and the txt or excel files are read; due to the large number of Chinese characters, this step organizes the characters to be recognized in practical applications, which reduces the number of characters to be recognized and the size of the generated model, thereby improving the recognition speed of the characters;
[0040] 2) Set the font, size, and model name parameters of the training text to improve the accuracy of text recognition;
[0041] 3) Read the selected txt or excel file line by line, obtain the total number of text lines (denoted as num_lines) and the number of words in the line with the most words (denoted as max_length); This step is mainly used to calculate the generation size of the training image and obtain the initial data in the following steps;
[0042] 4) Calculate the width and height of the generated text image based on the set character spacing (gap), line spacing (linespacing), page margin (padding), maximum image width, single text width (width), and single text height (height). The calculation formula is as follows:
[0043] Image width (width_image) = padding*2+max_length*(gap+width)
[0044] Image height (height_image) = padding*2+num_lines*(linespacing+height)
[0045] 5) If the calculated image width is greater than the set maximum image width, the longest image width is used as the width of the image to be generated, and the image height is recalculated based on the character spacing (gap), line spacing (linespacing), and page margin (padding). The calculation process is as follows:
[0046] a) Calculate the number of words max_words_num that can fit in one line of the image
[0047] words_num=(width_image–padding*2) / (gap+width)
[0048] max_word_length takes the smallest integer greater than or equal to words_num
[0049] b) Calculate the number of text lines and image height
[0050] lines_word = total number of words / max_word_length
[0051] The number of text lines (num_lines) is the smallest integer greater than or equal to lines_word (rounded up)
[0052] Image height = padding*2+num_lines*(linespacing+height)
[0053] 6) Based on the image size calculated in step 5) (referred to as imgsize), call the QImage class of Qt (a cross-platform C++ graphical user interface application development framework) to generate a completely white image. The calling code is as follows:
[0054] QImage img(imgsize,QImage::Format_RGB888);
[0055] img.fill(QColor(255,255,255));
[0056] 7) Draw individual words in order on the all-white image. The drawing steps are as follows:
[0057] a) Set the first text position (horizontally startx, vertically starty). The initial position values are as follows:
[0058] startx=padding; starty=padding;
[0059] b) Draw the first character in the center of a rectangle with the coordinates (startx, starty) as the starting point, the width of a single character (width) as the width of the rectangle, and the height of a single character (height) as the height of the rectangle.
[0060] c) Calculate the startx and starty of the next character, and the values are as follows:
[0061] startx=startx+width+gap
[0062] starty=padding
[0063] If (startx + page margin) > image width, then:
[0064] starty = starty + the number of lines of text drawn * (height + linespacing)
[0065] startx=padding
[0066] d) Record and save the startx, starty, width, and height of each character, and repeat steps ac until the image is completely drawn.
[0067] 8) According to the position of each character and the length and width of the rectangle recorded in step 7), the pixel values of the image in the rectangular frame are scanned from top to bottom, from bottom to top, from left to right, and from right to left. This step calculates the circumscribed rectangle of the text by scanning in four directions, which is faster than the method of calculating the circumscribed rectangle by scanning the text from top to bottom and from left to right, and the calculation speed is improved;
[0068] a) Scan the pixels of the text rectangle from top to bottom in rows until a whole row of pixels is found that is not white. The number of the row is recorded as y.
[0069] b) Scan the pixels of the text rectangle from left to right in columns until a whole column of pixels containing non-white pixels is found. The number of the column is recorded as y.
[0070] c) Scan the pixels of the text rectangle from bottom to top in rows until a whole row of pixels containing non-white pixels is found, and record the row number as end_y.
[0071] d) Scan the pixels of the text rectangle from right to left in columns until all the pixels in the entire column are non-white. The column number is recorded as end_x.
[0072] e) The data calculated according to ad) is used as the minimum enclosing rectangle of the text, the coordinates of the upper left corner vertex of the rectangle are (x, y), and the coordinates of the lower right corner vertex are (end_x, endy);
[0073] 9) Since the QImage image coordinate system is inconsistent with the text coordinate system used for Tesseract training (see the figure below), and Tesseract requires the coordinates of the lower left and upper right corners of the text minimum enclosing rectangle as training data, the coordinates of the text minimum enclosing rectangle [(x, y), (end_x, endy)] in step 8) are converted to the text coordinate system [(t_x, t_y), (t_end_x, t_endy)] for Tesseract training, such as Figure 3 As shown, the conversion formula is as follows:
[0074] t_x=x
[0075] t_y=height_image–y-(endy-y)
[0076] t_endx=x+width_image
[0077] t_endy=height_image–y
[0078] 10) Calculate each character according to 9), and write and save the data of each character into a text file with the suffix .box in the following format, with each character data occupying one line:
[0079] Text t_x t_y t_endx t_endy
[0080] The .box file generated in this step is the necessary character position file in Tesseract training. It is automatically generated by the training tool, which is more convenient than the traditional method of manually adjusting the position of the text circumscribed rectangle.
[0081] 11) According to the setting parameters, such as training language, paging mode, etc., use the tesseract command to generate a text file with the suffix .tr. The command is as follows:
[0082] tesseract power.font.exp0.tif power.font.exp0–psm 6nobatch box.train
[0083] The .tr file generated in this step is the necessary character feature file for tesseract training. It is generated by automatically calling the tesseract command through the tool, which is more convenient than the traditional method of manually calling the command to generate.
[0084] 12) Read all files with suffixes of .tif, .box, and .tr in the manually marked folder, and check whether a text file named font_properties exists in the folder. If it does not exist, use the name between the first "." and the second "." as the font name based on the names of all image files with the suffix of .tif, filter out duplicate font names, and write each font as a line in the text file named font_properties in the following format. The format is as follows:
[0085] font 0 0 0 0 0
[0086] For example, if the image file with the suffix .tif is named power.userfont.exp0.tif, the content written into the font_properties file is:
[0087] userfont 0 0 0 0 0
[0088] The generated font_properties file is a necessary font file for tesseract training. It is faster and more convenient than the traditional method of manually searching for different tif file names, manually creating font_properties files, and manually entering fonts.
[0089] 13) Execute the unicharset_extractor, shapeclustering, mftraining, and cntraing training commands of Tesseract in sequence to generate a file with the suffix .traineddata, which is the text model file; the tool automatically calls and executes the above commands in sequence, which is faster and more convenient than the traditional method of manually entering the above commands in sequence and executing them;
[0090] 14) After the training is completed, the training tool automatically calls the tesseract command to recognize the image generated in step 7), and compares the recognition result with the input text, prompting the recognition error text, text recall rate and accuracy rate; the recall rate and accuracy rate of the text are automatically detected by the training tool, which is faster and more convenient than the traditional method that requires manual search and calculation of error text;
[0091] 15) If the user wants to continue to improve the text recognition rate, he can continue to use the image tool to convert the actual application pictures containing incorrect text into picture files with the suffix .tif, and use the marking tool to manually mark them to generate .box text files, add them to the manual marking folder, and re-train.
[0092] Example 2: Figure 2 As shown, a method for merging power signal model training files comprises the following steps:
[0093] 1) Selecting a manually marked and generated .box text file and a picture file with a suffix of .tif, which may be the .box text file and the picture file with a suffix of .tif generated in steps 1) to 10) of Example 1, or may be adjusted by other methods or tools; the picture files in .tif format under different paths may be selected, which is more convenient than the traditional method of manually copying the merged files to the same path;
[0094] 2) Set the training parameters for each file, including the training language and page mode parameters. The default training language is Chinese. Different parameters can be set for each selected file, which is more intuitive and convenient than the traditional method of entering parameters and executing commands for each file separately.
[0095] 3) According to the selected file name and the model name to be generated, the image file and box file with the suffix .tif are automatically named to form a standardized training file. The standardized naming of the image file with the suffix .tif is as follows:
[0096]
[0097] The corresponding text file with the suffix .box is named as follows:
[0098]
[0099] Automatically rename .tif and .box files through the merge tool, which is faster and more convenient than the traditional method of manually renaming and copying them to the same folder;
[0100] 4) Based on the names of all image files with the suffix .tif, use the name between the first “.” and the second “.” as the font name, with each font in one line, and write them into a text file named font_properties in the following format;
[0101] font 0 0 0 0 0
[0102] Font_properties is a necessary font file required for tesseract training model. The present invention automatically obtains the font name of each .tif through the merging tool and writes it into the font_properties file. Compared with the traditional method that requires manual font judgment, manual creation of the font_properties file, and writing the font into the file, the operation is more convenient and less prone to errors.
[0103] 5) Call the tesseract command to generate a text file with the suffix .tr for each file with the suffix .tif. The command is as follows:
[0104] tesseract power.font.exp0.tif power.font.exp0–psm 6nobatch box.train
[0105] The .tr format file is a necessary character feature file required for the Tesseract training model. The present invention automatically calls the command execution according to the number of files through the merging tool, which is faster and more convenient than the traditional method that requires manual operation according to the number of files and file names.
[0106] 6) Combine all text file names with the suffix .box into a string separated by spaces, use it as the parameter of the unicharset_extractor command, call and execute the unicharset_extractor command; combine all text file names with the suffix .tr into a string separated by spaces, use it as the parameter of the shapeclustering, mftraining, and cntraing commands, call and execute the shapeclustering, mftraining, and cntraing commands;
[0107] The merge tool automatically combines the execution command statements and automatically calls the training commands, which is faster and more convenient than the traditional method that requires manual writing of inspection commands and execution commands in sequence;
[0108] 7) Finally, call the combine_tessdata command of tessract to generate the .traineddata file, which is the final merged text model.
[0109] The above description is only a specific implementation mode of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A method for merging power signal model training files, characterized in that: The method comprises the following steps: Select the manually marked .box text file and the image file with the suffix .tif; Set the training parameters for each file, including the training language and page mode parameters. The default training language is Chinese. According to the selected file name and the model name to be generated, the image file and box file with the suffix .tif are automatically named to form a standardized training file. The standardized naming of the image file with the suffix .tif is as follows: Model name power. Font name font. Serial number name exp0. Format name tif; The corresponding text file with the suffix .box is named as follows: Model name power. Font name font. Serial number name exp0. Format name box; According to the names of all image files with the suffix .tif, the name between the first ".." and the second ".." is used as the font name, and each font is written in one line in a text file named font_properties in the following format; font 0 0 0 0 0 Call the tesseract command to generate a text file with the suffix .tr for each file with the suffix .tif. The command is as follows: tesseract power.font.exp0.tif power.font.exp0 –psm 6 nobatch box.train Combine all text file names with the suffix .box into a string separated by spaces, use it as the parameter of the unicharset_extractor command, call and execute the unicharset_extractor command; combine all text file names with the suffix .tr into a string separated by spaces, use it as the parameter of the shapeclustering, mftraining, and cntraing commands, call and execute the shapeclustering, mftraining, and cntraing commands; Finally, call the combine_tessdata command of tesseract to generate the .traineddata file, which is the final merged text model; The method for generating the .box text file and the image file with the suffix .tif in step 1) includes the following steps: 1) Read txt or excel files; 2) Set the font, size, and model name parameters of the training text; 3) Read the selected txt or excel file line by line, get the total number of text lines, recorded as num_lines, and the number of words in the line with the most words among these lines, recorded as max_length; 4) Calculate the width and height of the text image based on the set character spacing gap, line spacing linespacing, page margin padding, maximum image width, single text width width, and single text height height; 5) If the calculated image width is greater than the set maximum image width, the longest image width is used as the width of the image to be generated, and the image height is recalculated based on the character spacing gap, line spacing linespacing, and page margin padding; 6) Based on the image size calculated in step 5), record it as imgsize, call Qt's QImage class to generate a completely white image, and save it to an image file with the suffix .tif; 7) Draw individual words in order on a completely white picture; 8) According to the position of each character and the length and width of the rectangle recorded in step 7), the pixel values of the image in the rectangular frame are scanned in four directions: from top to bottom, from bottom to top, from left to right, and from right to left; 9) Convert the coordinates of the minimum circumscribed rectangle of the text in step 8) [(x, y), (end_x, endy)] to the text coordinate system trained by Tesseract [(t_x, t_y), (t_end_x, t_endy)]. The conversion formula is as follows: t_x = x t_y = height_image – y – (endy – y) t_endx = x + width_image t_endy = height_image – y; 10) Perform coordinate transformation calculation for each character according to step 9), and write and save the data of each character into a text file with the suffix .box in the following format, with each character data occupying one line: Text t_x t_y t_endx t_endy.
2. The method for merging power signal model training files according to claim 1, characterized in that: The suffix .tif is converted from an image using an image tool, and manually labeled using a labeling tool to generate a .box text file, added to the manually labeled folder, and re-trained.
Citation Information
Patent Citations
HTML file image-text integrating display method
CN103942314A
Deep learning-based quota invoice value recognition system
CN108133212A