Files
workspace/notes/后端开发/paddlepaddle/paddleocr.md
T
2025-07-24 17:22:24 +08:00

226 lines
7.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
注:这里以 **识别集装箱号** 为案例进行演示
# 相关网站
- paddlepaddle:https://www.paddlepaddle.org.cn/
- github:https://github.com/PaddlePaddle/PaddleOCR/tree/main
# 1 环境配置
当前使用的是虚拟环境:
- python:3.11
- paddlepaddle:3.1
- paddleocr:3.1.0
## 1.1 安装paddlepaddle
网站:https://www.paddlepaddle.org.cn/install/quick?docurl=/documentation/docs/zh/develop/install/pip/linux-pip.html
需要注意自己的 CUDA 版本,不要过高即可,查看命令:
```bash
nvidia-smi
```
输出如下:
```
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.105.17 Driver Version: 525.105.17 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 Tesla T4 On | 00000000:00:09.0 Off | 0 |
| N/A 27C P8 8W / 70W | 4MiB / 15360MiB | 0% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| No running processes found |
+-----------------------------------------------------------------------------+
```
当前电脑的 CUDA 版本为 12.0,所以下载 小于等于 12.0版本的paddlepaddle 即可
```bash
python -m pip install paddlepaddle-gpu==3.1.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/
```
检查是否安装成功:
```bash
python -c "import paddle; paddle.utils.run_check()"
```
输出以下内容即代表成功:
```
(ocr) root@VM-0-80-ubuntu:/workspace# python -c "import paddle; paddle.utils.run_check()"
/root/miniforge3/envs/ocr/lib/python3.11/site-packages/paddle/utils/cpp_extension/extension_utils.py:715: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md
warnings.warn(warning_message)
Running verify PaddlePaddle program ...
I0710 06:19:32.810492 2651 pir_interpreter.cc:1524] New Executor is Running ...
W0710 06:19:32.813128 2651 gpu_resources.cc:114] Please NOTE: device: 0, GPU Compute Capability: 7.5, Driver API Version: 12.0, Runtime API Version: 11.7
I0710 06:19:35.382279 2651 pir_interpreter.cc:1547] pir interpreter is running by multi-thread mode ...
PaddlePaddle works well on 1 GPU.
PaddlePaddle is installed successfully! Let's start deep learning with PaddlePaddle now.
```
## 1.2 下载paddleocr
下载网站:https://github.com/PaddlePaddle/PaddleOCR/releases
当前使用的版本是 3.1.0
下载后解压
```bash
unzip PaddleOCR-3.1.0.zip
```
安装依赖
```bash
cd PaddleOCR-3.1.0
pip install -r requirements.txt --user
```
测试是否能正常运行:
```bash
python tools/infer/predict_system.py --image_dir="/workspace/img/" --use_angle_cls=True --use_space_char=True
```
输出以下内容即成功:
```
ppocr INFO: not find det model file path None
```
# 2 准备数据集
## 2.1 标注工具
### 2.1.1 PPOcrLabel
> 存在问题:EXE 安装包运行会报错。但是该工具在本地运行标注的话还是比较推荐的。
下载地址:
- 专门为 PPOCR 制作的标注工具
### 2.1.2 X-AnyLabeling
下载地址:
- 支持多种导出格式,其中就支持 PPOCR 的格式
## 2.2 数据集结构
示例:
```
conno/1b4e2845-e9ba-4233-9268-bf07801ace0e.jpg [{"transcription": "RKSU5020243", "points": [[233, 317], [299, 317], [258, 833], [181, 839]], "difficult": false}]
conno/6ffd244b-1d27-4084-998b-e4d7ac076c5f.jpg [{"transcription": "ZGXU6173701", "points": [[717, 448], [781, 419], [835, 980], [751, 996]], "difficult": false}]
conno/0ba730a8-052b-4662-88fa-bfd3c7dbd118.jpg [{"transcription": "WHLU5706937", "points": [[565, 567], [915, 485], [915, 551], [578, 635]], "difficult": false}]
conno/8f50bf55-f825-43ac-87a5-f744aa3c2099.jpg [{"transcription": "EISU9420010", "points": [[295, 744], [624, 747], [618, 813], [299, 806]], "difficult": false}]
conno/33e22c52-64b6-4715-87d9-d3a7823b97f7.jpg [{"transcription": "SSKU1302201", "points": [[660, 676], [960, 671], [966, 724], [662, 737]], "difficult": false}]
conno/34a24249-7670-4be2-981f-19c502ee7da2.jpg [{"transcription": "TKRU4507940", "points": [[660, 395], [724, 395], [717, 882], [619, 902]], "difficult": false}]
conno/67ead2d0-89c0-455c-aef0-49a76a3492aa.jpg [{"transcription": "待识别", "points": [[585, 415], [589, 415], [593, 501], [917, 341], [893, 263]], "difficult": false}]
conno/78c58cfa-ff0f-4890-9fd9-07a0b639b9ca.jpg [{"transcription": "LYGU3568255", "points": [[553, 393], [587, 491], [972, 334], [983, 245]], "difficult": false}]
conno/481aee74-f9ce-4145-a8c7-f4c1f45fcc75.jpg [{"transcription": "RKSU4005584", "points": [[56, 298], [19, 935], [101, 937], [115, 280]], "difficult": false}]
conno/630d6e72-f99d-4922-8886-ea14f9e1a9bd.jpg [{"transcription": "FCIU2682384", "points": [[562, 737], [572, 803], [867, 712], [858, 655]], "difficult": false}]
conno/9751b358-0b48-4006-8a73-89e816d37b67.jpg [{"transcription": "CNIU2452605", "points": [[608, 801], [606, 867], [908, 837], [906, 773]], "difficult": false}]
```
# 3 准备预训练模型
> 如果已经有预训练模型的话,可以忽略这一步。
下载地址:https://github.com/PaddlePaddle/PaddleOCR/blob/release/3.1/docs/version3.x/model_list.md
如果报 404 的话,根据 版本 + 路径 进行查找即可。
## 3.1 文本检测训练模型
PP-OCRv5_mobile_det
![image-20250710162314463](./assets/paddleocr/image-20250710162314463.png)
## 3.2 文本识别训练模型
PP-OCRv5_mobile_rec
![image-20250710162351772](./assets/paddleocr/image-20250710162351772.png)
# 4 数据整理
## 4.1 检测模型所需数据准备
# 报错处理
## ImportError: libGL.so.1: cannot open shared object file: No such file or directory
### 问题原因
- 缺少 `libGL.so.1` 库
### 解决方案
```bash
sudo apt update
sudo apt install libgl1-mesa-glx
```
## all input arrays must have the same shap
### 问题原因
- 图像尺寸不一致
### 解决方案
将 batch_size_per_card 设置为 1
```yaml
Eval:
loader:
batch_size_per_card: 1
```
## OSError: cannot open resource
### 问题原因
- 字体路径无效
### 解决方案
将paddleocr里面的字体放到系统字体路径下,例如:
```bash
cp /workspace/PaddleOCR-3.1.0/doc/fonts/simfang.ttf /usr/share/fonts/
```