Train Optical Character Recognition for Custom Fonts

The optical character recognition (OCR) app trains the ocr function to recognize a custom language or font. You can use this app to label character data interactively for OCR training and to generate an OCR language data file for use with the ocr function.

Open the OCR Trainer App

  • MATLAB® Toolstrip: On the Apps tab, under Image Processing and Computer Vision, click , the OCR app icon.

  • MATLAB command prompt: Enter ocrTrainer.

Train OCR

  1. In the OCR Trainer, click New Session to open the OCR Training Session Settings dialog box.

  2. Under Output Settings, enter a name for the OCR language data file and choose the output folder location for the file. The location you specify must be writable.

  3. Under Labeling Method, either label the data manually or pre-label it using optical character recognition. If you use OCR, you can select either the pre-installed English or Japanese language, or you can download additional language support files.

    Note

    To download a language support file, type visionSupportPackages in a MATLAB Command Window. Alternatively, on the MATLAB Home tab, in the Environment section, click Add-Ons > Get Add-Ons. Then use the search box to find “Computer Vision System Toolbox OCR Language Data.”

  4. Add images at any time during the training session. The trainer automatically segments the images for OCR training. Inspect the results to verify expected text segmentation. To improve the segmentation, pre-process your images using the Image Segmenter app. Once the images are added, you can inspect segmentation results from the training image view.

    To limit the OCR to a specific character set, select the Character set check box and add the characters.

    Note

    Use training images that contain text that you want OCR to recognize. Do not use training images with only a few characters. OCR training works best if training images contain blocks of many words. You can use the insertText function to automatically generate training images for a known font.

    I = zeros(500,500,3,'uint8');
    
    textLines = [
        "some training text"
        "even more stuff to learn"easy
        ]
    lineYLocation = 50;
    
    for i = 1:numel(textLines)
        I = insertText(I,[50 lineYLocation],char(textLines(i)), ...
            'Font','LucidaSansRegular',...
            'FontSize',16,'TextColor','white',...
            'BoxOpacity',0);
    
     % increment to next line
        lineYLocation = lineYLocation + 20;
    end
    figure
    imshow(I) 
  5. Remove any noisy images. To improve segmentation results, you can draw a region of interest to select a portion of an image. The display shows the original image on the left and the edited one on the right. When you are done, click Accept All.

  6. Modify the extracted samples from the character view window.

    • To correct samples, select a group of samples from the Data Browser pane and change the labels using the Character Label field.

    • To exclude a sample from training, right-click the sample and select the option to move that sample to the Unknown category. Unknown samples are listed at the top of the Data Browser pane and are not used for training.

    • If the bounding box clipped a character, double-click the character and modify it in the image it was extracted from.

  7. After correcting the samples, click Train. When the trainer completes training, the app creates an OCR language data file and saves it to the folder you specified.

App Controls

Sessions

Starts a new session, opens a saved session, or adds a session to the current one. You can also save and name the session. The sessions are saved as MAT files.

Add Images

Adds images. You can add images when you start a new session or after you accept the current collection of images.

Settings

Set or change the font display.

Edit Box

Selects the image that contains the selected character, along with the bounding boxes. You can create additional regions, merge, modify, or delete existing images. To delete an ROI, use the delete key.

Train

Creates an OCR data file from the session. To use the .traineddata file with the ocr function, set the 'Language' property for the ocr function, and follow the directions for a custom language.

Generate Function

Creates an autogenerated evaluation function for verification of training results.

Note

Before running the OCR Trainer app, check if your machine has only one Tesseract installation. If there are multiple Tesseract installations, remove the extra installations and restart MATLAB to run the OCR Trainer app. Otherwise, the app returns the error "Not enough input arguments" when you click the Train button.

See Also

|