Skip to content

Tutorial for Text Line Orientation Classification Module

I. Overview

The text line orientation classification module primarily distinguishes the orientation of text lines and corrects them using post-processing. In processes such as document scanning and license/certificate photography, to capture clearer images, the capture device may be rotated, resulting in text lines in various orientations. Standard OCR pipelines cannot handle such data well. By utilizing image classification technology, the orientation of text lines can be predetermined and adjusted, thereby enhancing the accuracy of OCR processing.

II. Supported Model List

ModelModel Download Link Top-1 Accuracy (%) GPU Inference Time (ms)
[Normal Mode / High-Performance Mode]
CPU Inference Time (ms) Model Size (M) Description
PP-LCNet_x0_25_textline_oriInference Model/Trained Model 95.54 - - 0.32 Text line classification model based on PP-LCNet_x0_25, with two classes: 0 degrees and 180 degrees

Test Environment Description:

  • Performance Test Environment
  • Test Dataset: PaddleX Self-built Dataset, Covering Multiple Scenarios Such as Documents and Certificates, Containing 1000 Images.
  • Hardware Configuration:

    • GPU: NVIDIA Tesla T4
    • CPU: Intel Xeon Gold 6271C @ 2.60GHz
    • Other Environments: Ubuntu 20.04 / cuDNN 8.6 / TensorRT 8.5.2.2
  • Inference Mode Description

Mode GPU Configuration CPU Configuration Acceleration Technology Combination
Normal Mode FP32 Precision / No TRT Acceleration FP32 Precision / 8 Threads PaddleInference
High-Performance Mode Optimal combination of pre-selected precision types and acceleration strategies FP32 Precision / 8 Threads Pre-selected optimal backend (Paddle/OpenVINO/TRT, etc.)

III. Quick Integration

❗ Before quick integration, please install the PaddleX wheel package first. For details, please refer to the PaddleX Local Installation Guide

After completing the installation of the wheel package, you can perform inference for the text line orientation classification module with just a few lines of code. You can switch models under this module at will, and you can also integrate the model inference of the text line orientation classification module into your project. Before running the following code, please download the example image to your local machine. If the download link is not working, please check the validity of the URL and try again.

from paddlex import create_model
model = create_model(model_name="PP-LCNet_x0_25_textline_ori")
output = model.predict("textline_rot180_demo.jpg",  batch_size=1)
for res in output:
    res.print(json_format=False)
    res.save_to_img("./output/demo.png")
    res.save_to_json("./output/res.json")

After running, the result obtained is:

{'res': {'input_path': 'test_imgs/textline_rot180_demo.jpg', 'class_ids': [1], 'scores': [1.0], 'label_names': ['180_degree']}}

The meanings of the running results parameters are as follows:

  • input_path:Indicates the path of the input image.
  • class_ids:Indicates the class ID of the prediction result.
  • scores:Indicates the confidence score of the prediction result.
  • label_names:Indicates the class name of the prediction result. The visualization image is as follows:

The explanations for the methods, parameters, etc., are as follows:

  • create_model instantiates a text recognition model (here, PP-LCNet_x0_25_textline_ori is used as an example), and the specific explanations are as follows:
Parameter Parameter Description Parameter Type Options Default Value
model_name Name of the model str None PP-LCNet_x0_25_textline_ori
model_dir Path to store the model str None None
  • The model_name must be specified. After specifying model_name, the default model parameters built into PaddleX are used. If model_dir is specified, the user-defined model is used.

  • The predict() method of the text recognition model is called for inference prediction. The predict() method has parameters input and batch_size, which are explained as follows:

Parameter Parameter Description Parameter Type Options Default Value
input Data to be predicted, supporting multiple input types Python Var/str/list
  • Python variable, such as image data represented by numpy.ndarray
  • File path, such as the local path of an image file: /root/data/img.jpg
  • URL link, such as the network URL of an image file: Example
  • Local directory, the directory should contain data files to be predicted, such as the local path: /root/data/
  • List, the elements of the list should be of the above-mentioned data types, such as [numpy.ndarray, numpy.ndarray], [\"/root/data/img1.jpg\", \"/root/data/img2.jpg\"], [\"/root/data1\", \"/root/data2\"]
None
batch_size Batch size int Any integer 1
  • The prediction results are processed, and the prediction result for each sample is of type dict. It supports operations such as printing, saving as an image, and saving as a json file:
Method Method Description Parameter Parameter Type Parameter Description Default Value
print() Print the results to the terminal format_json bool Whether to format the output content using JSON indentation True
indent int Specify the indentation level to beautify the output JSON data, making it more readable, only effective when format_json is True 4
ensure_ascii bool Control whether to escape non-ASCII characters to Unicode. If set to True, all non-ASCII characters will be escaped; False retains the original characters, only effective when format_json is True False
save_to_json() Save the results as a JSON file save_path str The path to save the file. If it is a directory, the saved file name will be consistent with the input file name None
indent int Specify the indentation level to beautify the output JSON data, making it more readable, only effective when format_json is True 4
ensure_ascii bool Control whether to escape non-ASCII characters to Unicode. If set to True, all non-ASCII characters will be escaped; False retains the original characters, only effective when format_json is True False
save_to_img() Save the results as an image file save_path str The path to save the file. If it is a directory, the saved file name will be consistent with the input file name None
  • Additionally, it supports obtaining the visualization image with results and the prediction results through attributes, as follows:
Attribute Attribute Description
json Get the prediction result in json format
img Get the visualization image in dict format

For more information on using the PaddleX single-model inference API, refer to the PaddleX Single Model Python Script Usage Instructions.

IV. Custom Development

If you aim for higher accuracy with existing models, you can leverage PaddleX's custom development capabilities to develop better text line orientation classification models. Before developing a text line orientation classification model with PaddleX, ensure that you have installed PaddleX's classification-related model training capabilities. The installation process can be found in the PaddleX Local Installation Tutorial.

4.1 Data Preparation

Before model training, you need to prepare a dataset for the corresponding task module. PaddleX provides data validation functionality for each module, and only data that passes validation can be used for model training. Additionally, PaddleX provides demo datasets for each module, allowing you to complete subsequent development based on the official demo data. If you wish to use a private dataset for subsequent model training, refer to the PaddleX Image Classification Task Module Data Preparation Tutorial.

4.1.1 Demo Data Download

You can download the demo dataset to a specified folder using the following command:

wget https://paddle-model-ecology.bj.bcebos.com/paddlex/data/textline_orientation_example_data.tar -P ./dataset
tar -xf ./dataset/textline_orientation_example_data.tar -C ./dataset/

4.1.2 Data Validation

You can complete data validation with a single command:

python main.py -c paddlex/configs/modules/textline_orientation/PP-LCNet_x0_25_textline_ori.yaml \
    -o Global.mode=check_dataset \
    -o Global.dataset_dir=./dataset/textline_orientation_example_data
After executing the above command, PaddleX will validate the dataset and collect basic information about it. Upon successful execution, the log will print the message Check dataset passed !. The validation result file is saved in ./output/check_dataset_result.json, and related outputs are saved in the ./output/check_dataset directory under the current directory, including visualized sample images and sample distribution histograms.

👉 Details of Verification Results (Click to Expand)

The specific content of the verification result file is as follows:

{
  "done_flag": true,
  "check_pass": true,
  "attributes": {
    "label_file": "../../dataset/textline_orientation_example_data/label.txt",
    "num_classes": 2,
    "train_samples": 1000,
    "train_sample_paths": [
      "check_dataset/demo_img/ILSVRC2012_val_00019234_4284.jpg",
      "check_dataset/demo_img/lsvt_train_images_4655.jpg",
      "check_dataset/demo_img/lsvt_train_images_60562.jpg",
      "check_dataset/demo_img/lsvt_train_images_14013.jpg",
      "check_dataset/demo_img/ILSVRC2012_val_00011156_12950.jpg",
      "check_dataset/demo_img/ILSVRC2012_val_00016578_10192.jpg",
      "check_dataset/demo_img/26920921_2341381071.jpg",
      "check_dataset/demo_img/31979250_3394569384.jpg",
      "check_dataset/demo_img/25959328_518853598.jpg",
      "check_dataset/demo_img/ILSVRC2012_val_00018420_14077.jpg"
    ],
    "val_samples": 200,
    "val_sample_paths": [
      "check_dataset/demo_img/lsvt_train_images_79109.jpg",
      "check_dataset/demo_img/lsvt_train_images_131133.jpg",
      "check_dataset/demo_img/mtwi_train_images_65423.jpg",
      "check_dataset/demo_img/lsvt_train_images_120718.jpg",
      "check_dataset/demo_img/mtwi_train_images_58098.jpg",
      "check_dataset/demo_img/rctw_train_images_25817.jpg",
      "check_dataset/demo_img/lsvt_val_images_6336.jpg",
      "check_dataset/demo_img/lsvt_train_images_71775.jpg",
      "check_dataset/demo_img/mtwi_train_images_78064.jpg",
      "check_dataset/demo_img/mtwi_train_images_52578.jpg"
    ]
  },
  "analysis": {
    "histogram": "check_dataset/histogram.png"
  },
  "dataset_path": "./dataset/textline_orientation_example_data",
  "show_type": "image",
  "dataset_type": "ClsDataset"
}

In the above verification results, check_pass being True indicates that the dataset format meets the requirements. Explanations for other indicators are as follows:

  • attributes.num_classes: The number of classes in this dataset is 2;
  • attributes.train_samples: The number of training samples in this dataset is 1000;
  • attributes.val_samples: The number of validation samples in this dataset is 200;
  • attributes.train_sample_paths: The list of relative paths to the visualization images of the training samples in this dataset;
  • attributes.val_sample_paths: The list of relative paths to the visualization images of the validation samples in this dataset;

The dataset verification also analyzes the distribution of sample numbers across all classes in the dataset and generates a distribution histogram (histogram.png):

4.1.3 Dataset Format Conversion / Dataset Splitting (Optional)

After completing data validation, you can convert the dataset format and re-split the training/validation ratio of the dataset by modifying the configuration file or adding hyperparameters.

👉 Details on Format Conversion / Dataset Splitting (Click to Expand)

(1) Dataset Format Conversion

Text line orientation classification temporarily does not support data format conversion.

(2) Dataset Splitting

Parameters for dataset splitting can be set by modifying the fields under CheckDataset in the configuration file. Examples of some parameters in the configuration file are as follows:

  • CheckDataset:
  • split:
  • enable: Whether to re-split the dataset. When set to True, dataset splitting is performed, with a default of False;
  • train_percent: If re-splitting the dataset, you need to set the percentage of the training set, which is any integer between 0 and 100, and must sum to 100 with the value of val_percent;

For example, if you want to re-split the dataset with 90% for the training set and 10% for the validation set, you need to modify the configuration file as follows:

......
CheckDataset:
  ......
  split:
    enable: True
    train_percent: 90
    val_percent: 10
  ......

Then execute the command:

python main.py -c paddlex/configs/modules/textline_orientation/PP-LCNet_x0_25_textline_ori.yaml \
    -o Global.mode=check_dataset \
    -o Global.dataset_dir=./dataset/textline_orientation_example_data

After the data splitting is executed, the original annotation files will be renamed to xxx.bak in the original path.

The above parameters can also be set by appending command-line arguments:

python main.py -c paddlex/configs/modules/textline_orientation/PP-LCNet_x0_25_textline_ori.yaml \
    -o Global.mode=check_dataset \
    -o Global.dataset_dir=./dataset/textline_orientation_example_data \
    -o CheckDataset.split.enable=True \
    -o CheckDataset.split.train_percent=90 \
    -o CheckDataset.split.val_percent=10

4.2 Model Training

Model training can be completed with a single command. Here, the training of the text line orientation classification model (PP-LCNet_x1_0_textline_ori) is taken as an example:

python main.py -c paddlex/configs/modules/textline_orientation/PP-LCNet_x0_25_textline_ori.yaml \
    -o Global.mode=train \
    -o Global.dataset_dir=./dataset/textline_orientation_example_data
The following steps are required:

  • Specify the path to the .yaml configuration file for the model (here it is PP-LCNet_x0_25_textline_ori.yaml. When training other models, you need to specify the corresponding configuration file. The correspondence between models and configuration files can be found in the PaddleX Model List (CPU/GPU)).
  • Specify the mode as model training: -o Global.mode=train
  • Specify the path to the training dataset: -o Global.dataset_dir Other related parameters can be set by modifying the fields under Global and Train in the .yaml configuration file or by appending parameters in the command line. For example, to specify the first two GPUs for training: -o Global.device=gpu:0,1; to set the number of training epochs to 10: -o Train.epochs_iters=10. For more modifiable parameters and their detailed explanations, refer to the configuration file description for the corresponding task module of the model PaddleX Common Model Configuration Parameters.
👉 More Details (Click to Expand)
  • During model training, PaddleX automatically saves the model weights to the default directory output. If you need to specify a save path, you can set it through the -o Global.output field in the configuration file.
  • PaddleX shields you from the concepts of dynamic graph weights and static graph weights. During model training, both dynamic and static graph weights are produced, and static graph weights are selected by default for model inference.
  • After completing model training, all outputs are saved in the specified output directory (default is ./output/), typically including the following:

  • train_result.json: Training result record file, recording whether the training task was completed normally, as well as the output weight metrics, related file paths, etc.;

  • train.log: Training log file, recording changes in model metrics, loss, etc., during training;
  • config.yaml: Training configuration file, recording the hyperparameter configurations for this training;
  • .pdparams, .pdema, .pdopt.pdstate, .pdiparams, .pdmodel: Model weight-related files, including network parameters, optimizer, EMA, static graph network parameters, static graph network structure, etc.;

4.3 Model Evaluation

After completing model training, you can evaluate the specified model weights on the validation set to verify the model's accuracy. Using PaddleX for model evaluation can be done with a single command:

python main.py -c paddlex/configs/modules/textline_orientation/PP-LCNet_x0_25_textline_ori.yaml \
    -o Global.mode=evaluate \
    -o Global.dataset_dir=./dataset/textline_orientation_example_data
Similar to model training, the following steps are required:

  • Specify the path to the model's .yaml configuration file (here it is PP-LCNet_x0_25_textline_ori.yaml)
  • Specify the mode as model evaluation: -o Global.mode=evaluate
  • Specify the path to the validation dataset: -o Global.dataset_dir Other related parameters can be set by modifying the fields under Global and Evaluate in the .yaml configuration file. For details, please refer to PaddleX Common Model Configuration File Parameter Description.
👉 **More Details (Click to Expand)**

When evaluating the model, you need to specify the path to the model weights file. Each configuration file has a default weight save path built in. If you need to change it, you can set it by appending a command-line parameter, such as -o Evaluate.weight_path="./output/best_model/best_model.pdparams".

After completing the model evaluation, the following outputs are typically generated:

Upon completion of the model evaluation, an `evaluate_result.json` file will be produced, which records the evaluation results. Specifically, it records whether the evaluation task was completed normally and the model's evaluation metrics, including Top-1 Accuracy.

4.4 Model Inference and Model Integration

After completing model training and evaluation, you can use the trained model weights for inference predictions or Python integration.

4.4.1 Model Inference

Performing inference predictions through the command line requires only the following single command. Before running the following code, please download the example image locally.

python main.py -c paddlex/configs/modules/textline_orientation/PP-LCNet_x0_25_textline_ori.yaml \
    -o Global.mode=predict \
    -o Predict.model_dir="./output/best_model/inference" \
    -o Predict.input="textline_rot180_demo.jpg"
Similar to model training and evaluation, the following steps are required:

  • Specify the path to the model's .yaml configuration file (here it is PP-LCNet_x0_25_textline_ori.yaml)
  • Specify the mode as model inference prediction: -o Global.mode=predict
  • Specify the path to the model weights: -o Predict.model_dir="./output/best_model/inference"
  • Specify the path to the input data: -o Predict.input="..." Other related parameters can be set by modifying the fields under Global and Predict in the .yaml configuration file. For details, please refer to PaddleX Common Model Configuration File Parameter Description.

4.4.2 Model Integration

The model can be directly integrated into the PaddleX pipeline or into your own project.

  1. Pipeline Integration

The text line orientation classification module can be integrated into the Document Scene Information Extraction v3 Pipeline (PP-ChatOCRv3-doc). Simply replace the model path to update the text line orientation classification module.

  1. Module Integration

The weights you produce can be directly integrated into the text line orientation classification module. You can refer to the Python example code in Quick Integration and only need to replace the model with the path to your trained model.

You can also use the PaddleX high-performance inference plugin to optimize the inference process of your model and further improve efficiency. For detailed procedures, please refer to the PaddleX High-Performance Inference Guide.

Comments