Skip to content

指定字体文件的位置,方便离线部署 #15432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/version3.x/pipeline_usage/OCR.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -1845,3 +1845,38 @@ from paddleocr import PaddleOCR

pipeline = PaddleOCR(paddlex_config="PaddleOCR.yaml")
```

#### 4.2.3 Specifying the Local Font File Path via Environment Variables
Before initializing the production line object, you can set an environment variable to specify the local font file path. This enables the production line to use the specified font when saving the annotated result image via the `save_to_img` method.
This approach is convenient for offline deployment as it eliminates the need to download font files over the network and simplifies file management. **It is essential to ensure that the specified font file exists!** **

Suppose the font file `simfang.ttf` is stored in the `fonts` folder in the current directory.

Python script:

```python
import os

# Specify the location of the font file before initializing the production line.
os.environ["PADDLE_PDX_LOCAL_FONT_FILE_PATH"] = "./fonts/simfang.ttf"

from paddleocr import PaddleOCR

ocr = PaddleOCR(
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False) # text detection + text recognition
# ocr = PaddleOCR(use_doc_orientation_classify=True, use_doc_unwarping=True) # text image preprocessing + text detection + textline orientation classification + text recognition
# ocr = PaddleOCR(use_doc_orientation_classify=False, use_doc_unwarping=False) # text detection + textline orientation classification + text recognition
# ocr = PaddleOCR(
# text_detection_model_name="PP-OCRv5_server_det",
# text_recognition_model_name="PP-OCRv5_server_rec",
# use_doc_orientation_classify=False,
# use_doc_unwarping=False,
# use_textline_orientation=False) # Switch to PP-OCRv5_server models
result = ocr.predict("./general_ocr_002.png")
for res in result:
res.print()
res.save_to_img("output")
res.save_to_json("output")
```
36 changes: 36 additions & 0 deletions docs/version3.x/pipeline_usage/OCR.md
Original file line number Diff line number Diff line change
Expand Up @@ -1847,3 +1847,39 @@ from paddleocr import PaddleOCR

pipeline = PaddleOCR(paddlex_config="PaddleOCR.yaml")
```

#### 4.2.3 通过环境变量指定使用本地字体文件路径
在初始化产线对象前,可通过设置环境变量,指定本地字体文件路径,使产线在执行 save_to_img 方法保存标注结果图像时使用该字体。
方便离线部署时无需网络下载字体文件,便于文件的管理,**通过该方法需要确保指定的字体文件存在!**

示例如下:
假设字体文件 `simfang.ttf` 存放于当前目录下的 `fonts` 文件夹内

Python 脚本:

```python
import os

# 在初始化产线前指定字体文件的位置
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不是在初始化产线前,必须在import paddleocr之前。
另外,建议直接在命令行中直接设置环境变量,无需修改Python代码。

os.environ["PADDLE_PDX_LOCAL_FONT_FILE_PATH"] = "./fonts/simfang.ttf"

from paddleocr import PaddleOCR

ocr = PaddleOCR(
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False) # 文本检测+文本识别
# ocr = PaddleOCR(use_doc_orientation_classify=True, use_doc_unwarping=True) # 文本图像预处理+文本检测+方向分类+文本识别
# ocr = PaddleOCR(use_doc_orientation_classify=False, use_doc_unwarping=False) # 文本检测+文本行方向分类+文本识别
# ocr = PaddleOCR(
# text_detection_model_name="PP-OCRv5_server_det",
# text_recognition_model_name="PP-OCRv5_server_rec",
# use_doc_orientation_classify=False,
# use_doc_unwarping=False,
# use_textline_orientation=False) # 更换 PP-OCRv5_server 模型
result = ocr.predict("./general_ocr_002.png")
for res in result:
res.print()
res.save_to_img("output")
res.save_to_json("output")
```