Files
Knowledge/1 使用/ocr/试用 PaddleOCR-VL.md

74 lines
2.6 KiB
Markdown
Raw Permalink 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.
在 **AI Studio** 中试用 **PaddleOCR-VL** 可以通过以下步骤实现,结合平台提供的免费算力和开源模型快速体验文档解析能力:
---
### **1. 进入AI Studio平台**
- 访问 [百度AI Studio](https://aistudio.baidu.com/) 并登录账号(支持手机号/百度账号)。
- 新用户可领取免费GPU算力资源(如V100或A100),用于模型推理或训练。
---
### **2. 创建项目并配置环境**
- **新建项目**:在AI Studio控制台点击“**新建项目**”,选择“**Notebook**”类型。
- **选择环境**:推荐使用“**PaddlePaddle 2.x**”镜像(已预装PaddleOCR依赖)。
- **启动项目**:进入Notebook后,终端会自动加载PaddlePaddle环境。
---
### **3. 安装PaddleOCR-VL**
在Notebook的代码单元格中执行以下命令,安装最新版PaddleOCR并加载VL模型:
```python
# 安装PaddleOCR(若环境未预装)
!pip install paddlepaddle paddleocr -i https://mirror.baidu.com/pypi/simple
# 克隆PaddleOCR仓库(获取VL模型配置)
!git clone https://github.com/PaddlePaddle/PaddleOCR.git
%cd PaddleOCR
```
---
### **4. 下载PaddleOCR-VL预训练模型**
PaddleOCR-VL的模型权重需手动下载,可通过以下方式获取:
- **官方GitHub**:从 [PaddleOCR-VL Release页](https://github.com/PaddlePaddle/PaddleOCR/releases) 下载模型文件(如`ch_PP-OCRv4_VL_det`、`ch_PP-OCRv4_VL_rec`)。
- **AI Studio数据集**:搜索“PaddleOCR-VL”公共数据集,直接挂载到项目。
**示例代码(挂载数据集)**:
```python
from aistudio.datasets import load_dataset
# 挂载PaddleOCR-VL模型(假设数据集ID为12345)
dataset = load_dataset("12345")
model_path = dataset.data_path + "/models/"
```
---
### **5. 运行PaddleOCR-VL推理**
加载模型并测试文档解析功能(支持PDF、图片等格式):
```python
from paddleocr import PaddleOCR
# 初始化VL模型(需指定模型路径)
ocr = PaddleOCR(
det_model_dir=model_path + "ch_PP-OCRv4_VL_det/", # 检测模型
rec_model_dir=model_path + "ch_PP-OCRv4_VL_rec/", # 识别模型
use_angle_cls=True,
lang="ch", # 中文文档
use_gpu=True # 启用GPU加速
)
# 测试单张图片
img_path = "test_doc.jpg"
result = ocr.ocr(img_path, cls=True)
# 打印结果
for line in result:
print(line)
```
---
### **6. 高级功能(可选)**
- **批量处理**:通过循环调用`ocr.ocr()`解析多张图片或PDF。
- **可视化**:使用`PaddleOCR`内置的`draw_ocr`函数生成标注结果图。
- **自定义训练**:参考[PaddleOCR训练教程](https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/doc/doc_ch/training