---
comments: true
---
# Text Detection Module Usage Guide
## 1. Overview
The text detection module is a critical component of OCR (Optical Character Recognition) systems, responsible for locating and marking text-containing regions in images. The performance of this module directly impacts the accuracy and efficiency of the entire OCR system. The text detection module typically outputs bounding boxes for text regions, which are then passed to the text recognition module for further processing.
## 2. Supported Models List
> The inference time only includes the model inference time and does not include the time for pre- or post-processing. The "Standard Mode" values correspond to the local paddle_static inference engine.
| Model | Model Download Link | Detection Hmean (%) | GPU Inference Time (ms) [Standard Mode / High-Performance Mode] |
CPU Inference Time (ms) [Standard Mode / High-Performance Mode] |
Model Size (MB) | Description |
|---|---|---|---|---|---|---|
| PP-OCRv6_medium_det | Inference Model/Training Model | 86.2* | - / - | - / - | 59.4 | PP-OCRv6 medium-scale text detection model based on PPLCNetV4 + RepLKFPN, highest accuracy, suitable for server deployment |
| PP-OCRv6_small_det | Inference Model/Training Model | 84.1* | - / - | - / - | 9.6 | PP-OCRv6 small text detection model, balancing accuracy and efficiency, suitable for mobile deployment |
| PP-OCRv6_tiny_det | Inference Model/Training Model | 80.6* | - / - | - / - | 1.9 | PP-OCRv6 ultra-lightweight text detection model (0.43M params), suitable for edge/IoT scenarios |
| PP-OCRv5_server_det | Inference Model/Training Model | 83.8 | 89.55 / 70.19 | 383.15 / 383.15 | 84.3 | PP-OCRv5 server-side text detection model with higher accuracy, suitable for deployment on high-performance servers |
| PP-OCRv5_mobile_det | Inference Model/Training Model | 79.0 | 10.67 / 6.36 | 57.77 / 28.15 | 4.7 | PP-OCRv5 mobile-side text detection model with higher efficiency, suitable for deployment on edge devices |
| PP-OCRv4_server_det | Inference Model/Training Model | 69.2 | 127.82 / 98.87 | 585.95 / 489.77 | 109 | PP-OCRv4 server-side text detection model with higher accuracy, suitable for deployment on high-performance servers |
| PP-OCRv4_mobile_det | Inference Model/Training Model | 63.8 | 9.87 / 4.17 | 56.60 / 20.79 | 4.7 | PP-OCRv4 mobile-side text detection model with higher efficiency, suitable for deployment on edge devices |
| Mode | GPU Configuration | CPU Configuration | Acceleration Techniques |
|---|---|---|---|
| Standard Mode | FP32 precision / No TRT acceleration | FP32 precision / 8 threads | PaddleInference |
| High-Performance Mode | Optimal combination of precision types and acceleration strategies | FP32 precision / 8 threads | Optimal backend selection (Paddle/OpenVINO/TRT, etc.) |
paddle_static inference engine by default. To run it, first install PaddlePaddle by following [PaddlePaddle Framework Installation](../paddlepaddle_installation.en.md).
If you choose `transformers` as the inference engine, make sure the Transformers environment is configured, and then run the following command:
```bash
# Use the transformers engine for inference
paddleocr text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png \
--engine transformers
```
If you choose `onnxruntime` as the inference engine, make sure the ONNX Runtime environment is configured, and then run the following command:
```bash
# Use the onnxruntime engine for inference
paddleocr text_detection -i https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png \
--engine onnxruntime
```
In most scenarios, the default `paddle_static` inference engine delivers better inference performance and is the recommended first choice.
Note: The official models would be download from HuggingFace by default. If can't access to HuggingFace, please set the environment variable `PADDLE_PDX_MODEL_SOURCE="BOS"` to change the model source to BOS. In the future, more model sources will be supported.
You can also integrate the model inference into your project. Before running the following code, download the [example image](https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png) locally.
```python
from paddleocr import TextDetection
model = TextDetection()
output = model.predict("general_ocr_001.png", batch_size=1)
for res in output:
res.print()
res.save_to_img(save_path="./output/")
res.save_to_json(save_path="./output/res.json")
```
The example above uses the paddle_static inference engine by default. To run it, first install PaddlePaddle by following [PaddlePaddle Framework Installation](../paddlepaddle_installation.en.md).
If you choose `transformers` as the inference engine, make sure the Transformers environment is configured, and then run the following code:
```python
from paddleocr import TextDetection
model = TextDetection(engine="transformers")
output = model.predict("general_ocr_001.png", batch_size=1)
for res in output:
res.print()
res.save_to_img(save_path="./output/")
res.save_to_json(save_path="./output/res.json")
```
If you choose `onnxruntime` as the inference engine, make sure the ONNX Runtime environment is configured, and then run the following code:
```python
from paddleocr import TextDetection
model = TextDetection(engine="onnxruntime")
output = model.predict("general_ocr_001.png", batch_size=1)
for res in output:
res.print()
res.save_to_img(save_path="./output/")
res.save_to_json(save_path="./output/res.json")
```
In most scenarios, the default `paddle_static` inference engine delivers better inference performance and is the recommended first choice.
If you want to use the trained model with the `paddle_dynamic` or `transformers` engine, refer to the [Weight Conversion](#52-weight-conversion) section in the [Inference Engine](#5-inference-engine) section below to convert the model from the `pdparams` format to the `safetensors` format using PaddleX.
The output will be:
```bash
{'res': {'input_path': 'general_ocr_001.png', 'page_index': None, 'dt_polys': array([[[ 75, 549],
...,
[ 77, 586]],
...,
[[ 31, 406],
...,
[ 34, 455]]], dtype=int16), 'dt_scores': [0.873949039891189, 0.8948166013613552, 0.8842595305917041, 0.876953790920377]}}
```
Output parameter meanings:
input_path:Path of the input image.page_index:If the input is a PDF, this indicates the current page number; otherwise, it is Nonedt_polys:Predicted text detection boxes, where each box contains four vertices (x, y coordinates).dt_scores:Confidence scores of the predicted text detection boxes.
Method and parameter descriptions:
* Instantiate the text detection model with TextDetection:
| Parameter | Description | Type | Default |
|---|---|---|---|
model_name |
Meaning:Model name. Description: If set to None, PP-OCRv6_medium_det will be used. |
str|None |
None |
model_dir |
Meaning:Model storage path. | str|None |
None |
device |
Meaning:Device for inference. Description: For example: "cpu", "gpu", "npu", "gpu:0", "gpu:0,1".If multiple devices are specified, parallel inference will be performed. By default, GPU 0 is used if available; otherwise, CPU is used. |
str|None |
None |
engine |
Meaning: Inference engine. Description: Supports None (the default), paddle, paddle_static, paddle_dynamic, transformers, and onnxruntime. When left as None, local inference uses the paddle_static engine by default. For detailed descriptions, supported values, compatibility rules, and examples, see Inference Engine and Configuration. |
str|None |
None |
engine_config |
Meaning: Inference-engine configuration. Description: Recommended together with engine. For supported fields, compatibility rules, and examples, see Inference Engine and Configuration. |
dict|None |
None |
enable_hpi |
Meaning:Whether to enable high-performance inference. | bool |
False |
use_tensorrt |
Meaning:Whether to use the Paddle Inference TensorRT subgraph engine. Description: If the model does not support acceleration through TensorRT, setting this flag will not enable acceleration. For Paddle with CUDA version 11.8, the compatible TensorRT version is 8.x (x>=6), and it is recommended to install TensorRT 8.6.1.6. |
bool |
False |
precision |
Meaning:Computation precision when using the Paddle Inference TensorRT subgraph engine. Description: Options: "fp32", "fp16". |
str |
"fp32" |
enable_mkldnn |
Meaning:Whether to enable MKL-DNN acceleration for inference. Description: If MKL-DNN is unavailable or the model does not support it, acceleration will not be used even if this flag is set. |
bool |
True |
mkldnn_cache_capacity |
Meaning:MKL-DNN cache capacity. | int |
10 |
cpu_threads |
Meaning:Number of threads to use for inference on CPUs. | int |
10 |
limit_side_len |
Meaning:Limit on the side length of the input image for detection.
Description:
int specifies the value. If set to None, the model's default configuration will be used. |
int|None |
None |
limit_type |
Meaning:Type of image side length limitation. Description: "min" ensures the shortest side of the image is no less than det_limit_side_len; "max" ensures the longest side is no greater than limit_side_len. If set to None, the model's default configuration will be used. |
str|None |
None |
max_side_limit |
Meaning:Limit on the max length of the input image for detection. Description: int limits the longest side of the image for input detection model. If set to None, the model's default configuration will be used. |
int|None |
None |
thresh |
Meaning:Pixel score threshold. Pixels in the output probability map with scores greater than this threshold are considered text pixels. Description: If set to None, the model's default configuration will be used. |
float|None |
None |
box_thresh |
Meaning:If the average score of all pixels inside the bounding box is greater than this threshold, the result is considered a text region. Description: If set to None, the model's default configuration will be used. |
float|None |
None |
unclip_ratio |
Meaning:Expansion ratio for the Vatti clipping algorithm, used to expand the text region.
Description:
If set to None, the model's default configuration will be used. |
float|None |
None |
input_shape |
Meaning:Input image size for the model in the format (C, H, W). |
tuple|None |
None |
predict() method parameters:
| Parameter | Description | Type | Default |
|---|---|---|---|
input |
Meaning:Input data to be predicted. Required. Description: Supports multiple input types:
|
Python Var|str|list |
|
batch_size |
Meaning:Batch size Description:Positive integer. |
int |
1 |
limit_side_len |
Meaning:Same meaning as the instantiation parameters. Description: If set to None, the instantiation value is used; otherwise, this parameter takes precedence. |
int|None |
None |
limit_type |
Meaning:Same meaning as the instantiation parameters. Description: If set to None, the instantiation value is used; otherwise, this parameter takes precedence. |
str|None |
None |
thresh |
Meaning:Same meaning as the instantiation parameters. Description: If set to None, the instantiation value is used; otherwise, this parameter takes precedence. |
float|None |
None |
box_thresh |
Meaning:Same meaning as the instantiation parameters. Description: If set to None, the instantiation value is used; otherwise, this parameter takes precedence. |
float|None |
None |
unclip_ratio |
Meaning:Same meaning as the instantiation parameters. Description: If set to None, the instantiation value is used; otherwise, this parameter takes precedence. |
float|None |
None |
| Method | Description | Parameters | Type | Description | Default |
|---|---|---|---|---|---|
print() |
Print results to terminal | format_json |
bool |
Format output as JSON | True |
indent |
int |
JSON indentation level | 4 | ||
ensure_ascii |
bool |
Escape non-ASCII characters | False |
||
save_to_json() |
Save results as JSON file | save_path |
str |
Output file path | Required |
indent |
int |
JSON indentation level | 4 | ||
ensure_ascii |
bool |
Escape non-ASCII characters | False |
||
save_to_img() |
Save results as image | save_path |
str |
Output file path | Required |
| Attribute | Description |
|---|---|
json |
Get prediction results in JSON format |
img |
Get visualization image as a dictionary |
| model | engine | Preprocessing (ms) | Inference (ms) | PostProcessing (ms) | End-to-End (ms) |
|---|---|---|---|---|---|
| PP-OCRv5_mobile_det | paddle_static | 11.43 | 13.80 | 2.15 | 27.58 |
| paddle_dynamic | 11.70 | 48.36 | 2.47 | 62.71 | |
| transformers | 14.05 | 18.45 | 3.98 | 37.54 | |
| onnxruntime | 9.98 | 5.70 | 2.04 | 17.90 | |
| PP-OCRv5_server_det | paddle_static | 13.24 | 26.91 | 2.63 | 43.05 |
| paddle_dynamic | 11.82 | 45.56 | 2.52 | 60.10 | |
| transformers | 14.56 | 13.76 | 7.44 | 36.76 | |
| onnxruntime | 10.01 | 13.76 | 1.92 | 25.86 | |
| PP-OCRv6_medium_det | paddle_static | 13.89 | 16.02 | 2.49 | 33.14 |
| paddle_dynamic | 11.42 | 26.23 | 2.30 | 40.10 | |
| transformers | 11.40 | 8.57 | 8.35 | 29.57 | |
| onnxruntime | 10.80 | 13.06 | 2.19 | 26.18 | |
| PP-OCRv6_small_det | paddle_static | 10.91 | 10.97 | 2.41 | 24.45 |
| paddle_dynamic | 11.56 | 22.17 | 2.66 | 36.55 | |
| transformers | 11.70 | 7.34 | 3.87 | 23.89 | |
| onnxruntime | 11.32 | 7.46 | 2.54 | 21.49 | |
| PP-OCRv6_tiny_det | paddle_static | 11.14 | 10.71 | 2.84 | 24.85 |
| paddle_dynamic | 11.52 | 21.70 | 2.94 | 36.31 | |
| transformers | 10.90 | 6.99 | 4.13 | 23.00 | |
| onnxruntime | 11.19 | 6.35 | 2.79 | 20.49 |