Text Detection C++ Demo¶
The demo shows an example of using neural networks to detect and recognize printed text rotated at any angle in various environment. You can use the following pre-trained models with the demo:
text-detection-0003, which is a detection network for finding text.
text-detection-0004, which is a lightweight detection network for finding text.
horizontal-text-detection-0001, which is a detection network that works much faster than models above, but it is applicable to finding more or less horizontal text only.
text-recognition-0012, which is a recognition network for recognizing text.
text-recognition-0014, which is a recognition network for recognizing text. You should add option
-tr_pt_firstand specify output layer name via
-tr_o_blb_nmoption for this model (see model description for details).
text-recognition-0015, which is a recognition network for recognizing text. You should add options
-m_tr_ss "?0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"(supported symbols set),
-tr_o_blb_nm "logits"(to specify output name) and
-dt simple(to specify decoder type). You can also specify
-loweroption to convert predicted text to lower-case. See model description for details.
text-recognition-resnet-fc, which is a recognition network for recognizing text. You should add option
handwritten-score-recognition-0001, which is a recognition network for recognizing handwritten score marks like
How It Works¶
On startup, the application reads command line parameters and loads one network to the Inference Engine for execution. Upon getting an image, it performs inference of text detection and prints the result as four points (
y4) for each text bounding box.
If text recognition model is provided, the demo prints recognized text as well.
By default, Open Model Zoo demos expect input with BGR channels order. If you trained your model to work with RGB order, you need to manually rearrange the default channels order in the demo application or reconvert your model using the Model Optimizer tool with the
--reverse_input_channels argument specified. For more information about the argument, refer to When to Reverse Input Channels section of Converting a Model Using General Conversion Parameters.
Preparing to Run¶
For demo input image or video files, refer to the section Media Files Available for Demos in the Open Model Zoo Demos Overview. The list of models supported by the demo is in
<omz_dir>/demos/text_detection_demo/cpp/models.lst file. This file can be used as a parameter for Model Downloader and Converter to download and, if necessary, convert models to OpenVINO Inference Engine format (*.xml + *.bin).
An example of using the Model Downloader:
python3 <omz_dir>/tools/downloader/downloader.py --list models.lst
An example of using the Model Converter:
python3 <omz_dir>/tools/downloader/converter.py --list models.lst
decoder_type = ctc
decoder_type = simple
Running the application with the
-h option yields the following usage message:
text_detection_demo [OPTION] Options: -h Print a usage message. -i Required. An input to process. The input must be a single image, a folder of images, video file or camera id. -loop Optional. Enable reading the input in a loop. -o "<path>" Optional. Name of the output file(s) to save. -limit "<num>" Optional. Number of frames to store in output. If 0 is set, all frames are stored. -m_td "<path>" Required. Path to the Text Detection model (.xml) file. -m_tr "<path>" Required. Path to the Text Recognition model (.xml) file. -dt "<type>" Optional. Type of the decoder, either 'simple' for SimpleDecoder or 'ctc' for CTC greedy and CTC beam search decoders. Default is 'ctc' -m_tr_ss "<value>" Optional. Symbol set for the Text Recognition model. -tr_pt_first Optional. Specifies if pad token is the first symbol in the alphabet. Default is false -lower Optional. Set this flag to convert recognized text to lowercase -out_enc_hidden_name "<value>" Optional. Name of the text recognition model encoder output hidden blob -out_dec_hidden_name "<value>" Optional. Name of the text recognition model decoder output hidden blob -in_dec_hidden_name "<value>" Optional. Name of the text recognition model decoder input hidden blob -features_name "<value>" Optional. Name of the text recognition model features blob -in_dec_symbol_name "<value>" Optional. Name of the text recognition model decoder input blob (prev. decoded symbol) -out_dec_symbol_name "<value>" Optional. Name of the text recognition model decoder output blob (probability distribution over tokens) -tr_o_blb_nm "<value>" Optional. Name of the output blob of the model which would be used as model output. If not stated, first blob of the model would be used. -cc Optional. If it is set, then in case of absence of the Text Detector, the Text Recognition model takes a central image crop as an input, but not full frame. -w_td "<value>" Optional. Input image width for Text Detection model. -h_td "<value>" Optional. Input image height for Text Detection model. -thr "<value>" Optional. Specify a recognition confidence threshold. Text detection candidates with text recognition confidence below specified threshold are rejected. -cls_pixel_thr "<value>" Optional. Specify a confidence threshold for pixel classification. Pixels with classification confidence below specified threshold are rejected. -link_pixel_thr "<value>" Optional. Specify a confidence threshold for pixel linkage. Pixels with linkage confidence below specified threshold are not linked. -max_rect_num "<value>" Optional. Maximum number of rectangles to recognize. If it is negative, number of rectangles to recognize is not limited. -d_td "<device>" Optional. Specify the target device for the Text Detection model to infer on (the list of available devices is shown below). The demo will look for a suitable plugin for a specified device. By default, it is CPU. -d_tr "<device>" Optional. Specify the target device for the Text Recognition model to infer on (the list of available devices is shown below). The demo will look for a suitable plugin for a specified device. By default, it is CPU. -l "<absolute_path>" Optional. Absolute path to a shared library with the CPU kernels implementation for custom layers. -c "<absolute_path>" Optional. Absolute path to the GPU kernels implementation for custom layers. -no_show Optional. If it is true, then detected text will not be shown on image frame. By default, it is false. -r Optional. Output Inference results as raw values. -u Optional. List of monitors to show initially. -b Optional. Bandwidth for CTC beam search decoder. Default value is 0, in this case CTC greedy decoder will be used.
Running the application with the empty list of options yields the usage message given above and an error message.
For example, use the following command line command to run the application:
./text_detection_demo \ -i <path_to_image>/sample.jpg \ -m_td <path_to_model>/text-detection-0004.xml \ -m_tr <path_to_model>/text-recognition-0014.xml \ -dt ctc \ -tr_pt_first \ -tr_o_blb_nm "logits"
text-recognition-0015 you should use
simple decoder for
-dt option. For other models use
ctc decoder (default decoder). In case of
text-recognition-0015 model, specify path to
text-recognition-0015-encoder models for
-m_tr key and decoder part will be found automatically as shown on example below:
./text_detection_demo \ -i <path_to_image>/sample.jpg \ -m_td <path_to_model>/text-detection-0003.xml \ -m_tr <path_to_model>/text-recognition-0015/text-recognition-0015-encoder/<precision>/text-recognition-0015-encoder.xml \ -dt simple \ -tr_pt_first \ -tr_o_blb_nm "logits" \ -m_tr_ss "?0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
> NOTE : If you provide a single image as an input, the demo processes and renders it quickly, then exits. To continuously visualize inference results on the screen, apply the
loop option, which enforces processing a single image in a loop.
You can save processed results to a Motion JPEG AVI file or separate JPEG or PNG files using the
To save processed results in an AVI file, specify the name of the output file with
aviextension, for example:
To save processed results as images, specify the template name of the output image file with
pngextension, for example:
-o output_%03d.jpg. The actual file names are constructed from the template at runtime by replacing regular expression
%03dwith the frame number, resulting in the following:
output_001.jpg, and so on. To avoid disk space overrun in case of continuous input stream, like camera, you can limit the amount of data stored in the output file(s) with the
limitoption. The default value is 1000. To change it, you can apply the
-limit Noption, where
Nis the number of frames to store.
> NOTE : Windows* systems may not have the Motion JPEG codec installed by default. If this is the case, you can download OpenCV FFMPEG back end using the PowerShell script provided with the OpenVINO install package and located at
<INSTALL_DIR>/opencv/ffmpeg-download.ps1. The script should be run with administrative privileges if OpenVINO is installed in a system protected folder (this is a typical case). Alternatively, you can save results as images.
The demo uses OpenCV to display the resulting frame with detections rendered as bounding boxes and text.