references/paddleocr.md
5.8 KB查看排版效果| 1 | # PaddleOCR Chinese Document OCR — Reference |
| 2 | |
| 3 | > Scanned Chinese document OCR with PP-OCRv4. ~98% simplified Chinese accuracy. |
| 4 | > Supports traditional Chinese, vertical text, and handwriting. |
| 5 | |
| 6 | ## Environment |
| 7 | |
| 8 | - **Venv**: `~/.venvs/paddleocr/` |
| 9 | - **Python**: `~/.venvs/paddleocr/bin/python` |
| 10 | - **Dependencies**: paddlepaddle==2.6.2 + paddleocr==2.7.3 + numpy==1.26.4 (numpy<2 avoids opencv ABI conflict) |
| 11 | |
| 12 | ### Installation |
| 13 | |
| 14 | ```bash |
| 15 | python3 -m venv ~/.venvs/paddleocr |
| 16 | ~/.venvs/paddleocr/bin/pip install paddlepaddle==2.6.2 |
| 17 | ~/.venvs/paddleocr/bin/pip install "numpy<2" paddleocr==2.7.3 |
| 18 | ``` |
| 19 | |
| 20 | ## Python 3.13 Compatibility (Important!) |
| 21 | |
| 22 | > ⚠️ **PaddleOCR has multiple compatibility issues with Python 3.13**. You MUST fix them manually. |
| 23 | |
| 24 | ### Issue 1: imghdr Module Removed |
| 25 | |
| 26 | **Symptom:** |
| 27 | ``` |
| 28 | ModuleNotFoundError: No module named 'imghdr' |
| 29 | ``` |
| 30 | |
| 31 | **Cause:** Python 3.13 removed the `imghdr` module (originally used for image type detection). |
| 32 | |
| 33 | **Solution:** Create a shim module: |
| 34 | |
| 35 | ```python |
| 36 | # Save to site-packages/imghdr.py |
| 37 | """imghdr module shim for Python 3.13+ compatibility""" |
| 38 | def whatfile(f): |
| 39 | return None |
| 40 | |
| 41 | def what(buf, h=None): |
| 42 | return None |
| 43 | ``` |
| 44 | |
| 45 | ### Issue 2: np.sctypes Removed |
| 46 | |
| 47 | **Symptom:** |
| 48 | ``` |
| 49 | AttributeError: module 'numpy' has no attribute 'sctypes' |
| 50 | ``` |
| 51 | |
| 52 | **Cause:** NumPy 2.0 removed `np.sctypes` attribute (used by `imgaug` package). |
| 53 | |
| 54 | **Solution:** Edit `site-packages/imgaug/imgaug.py` lines 44-46: |
| 55 | |
| 56 | ```python |
| 57 | # Original code (BROKEN): |
| 58 | # NP_FLOAT_TYPES = set(np.sctypes["float"]) |
| 59 | # NP_INT_TYPES = set(np.sctypes["int"]) |
| 60 | # NP_UINT_TYPES = set(np.sctypes["uint"]) |
| 61 | |
| 62 | # Fixed code: |
| 63 | NP_FLOAT_TYPES = {np.float16, np.float32, np.float64} |
| 64 | NP_INT_TYPES = {np.int8, np.int16, np.int32, np.int64} |
| 65 | NP_UINT_TYPES = {np.uint8, np.uint16, np.uint32, np.uint64} |
| 66 | ``` |
| 67 | |
| 68 | ### Issue 3: OpenCV Incompatible with NumPy 2.x |
| 69 | |
| 70 | **Symptom:** |
| 71 | ``` |
| 72 | ImportError: numpy.core.multiarray failed to import |
| 73 | ``` |
| 74 | |
| 75 | **Cause:** `opencv-python` 4.6 was compiled against NumPy 1.x, incompatible with NumPy 2.x ABI. |
| 76 | |
| 77 | **Solution:** Use `opencv-python-headless` 4.13.0.92+: |
| 78 | |
| 79 | ```bash |
| 80 | ~/.venvs/paddleocr/bin/pip uninstall opencv-python -y |
| 81 | ~/.venvs/paddleocr/bin/pip install opencv-python-headless --no-cache-dir |
| 82 | ``` |
| 83 | |
| 84 | ### Issue 4: scikit-image DLL Load Failed (Windows) |
| 85 | |
| 86 | **Symptom:** |
| 87 | ``` |
| 88 | ImportError: DLL load failed while importing _cython_blas |
| 89 | ``` |
| 90 | |
| 91 | **Cause:** Windows security policy blocks unsigned C extension DLLs. |
| 92 | |
| 93 | **Solutions:** |
| 94 | - Option A: Use pre-compiled wheel (Recommended) |
| 95 | - Option B: Temporarily disable Windows Defender Real-time Protection |
| 96 | - Option C: Use WSL2 or Linux environment |
| 97 | |
| 98 | ### Recommended Python 3.13 Installation |
| 99 | |
| 100 | ```bash |
| 101 | # Environment |
| 102 | python3 -m venv ~/.venvs/paddleocr |
| 103 | source ~/.venvs/paddleocr/bin/activate |
| 104 | |
| 105 | # Install (use PaddlePaddle 3.0.0 which supports NumPy 2.x) |
| 106 | pip install paddlepaddle==3.0.0 |
| 107 | pip install "numpy>=2" paddleocr==2.7.3 |
| 108 | pip uninstall opencv-python -y |
| 109 | pip install opencv-python-headless --no-cache-dir |
| 110 | |
| 111 | # Create imghdr shim |
| 112 | cat > ~/.venvs/paddleocr/lib/python3.13/site-packages/imghdr.py << 'EOF' |
| 113 | def whatfile(f): |
| 114 | return None |
| 115 | def what(buf, h=None): |
| 116 | return None |
| 117 | EOF |
| 118 | |
| 119 | # Patch np.sctypes issue |
| 120 | # Manually edit ~/.venvs/paddleocr/lib/python3.13/site-packages/imgaug/imgaug.py |
| 121 | ``` |
| 122 | |
| 123 | --- |
| 124 | |
| 125 | ## CLI: OCR a Single Image |
| 126 | |
| 127 | ```bash |
| 128 | ~/.venvs/paddleocr/bin/python -c " |
| 129 | from paddleocr import PaddleOCR |
| 130 | ocr = PaddleOCR(lang='ch') # auto-downloads models on first run |
| 131 | result = ocr.ocr('image.png') |
| 132 | for line_info in result[0]: |
| 133 | text = line_info[1][0] |
| 134 | conf = line_info[1][1] |
| 135 | print(f'{conf:.2f} {text}') |
| 136 | " |
| 137 | ``` |
| 138 | |
| 139 | ## Workflow: Scanned PDF → Structured Markdown |
| 140 | |
| 141 | ### Step 1: Split pages |
| 142 | |
| 143 | Preferred (if OpenDataLoader installed): |
| 144 | ```bash |
| 145 | opendataloader-pdf input_scan.pdf -f text -o ./pages |
| 146 | ``` |
| 147 | |
| 148 | Alternative (pdf2image): |
| 149 | ```bash |
| 150 | pip install pdf2image |
| 151 | python -c " |
| 152 | from pdf2image import convert_from_path |
| 153 | images = convert_from_path('input.pdf', dpi=300) |
| 154 | for i, img in enumerate(images): |
| 155 | img.save(f'page_{i+1:03d}.png') |
| 156 | " |
| 157 | ``` |
| 158 | |
| 159 | ### Step 2: OCR each page |
| 160 | |
| 161 | ```python |
| 162 | from paddleocr import PaddleOCR |
| 163 | import os |
| 164 | |
| 165 | ocr = PaddleOCR(lang='ch', use_angle_cls=True, use_gpu=False) |
| 166 | output_pages = {} |
| 167 | |
| 168 | png_files = sorted([f for f in os.listdir('./pages') if f.endswith('.png')], |
| 169 | key=lambda x: int(''.join(filter(str.isdigit, x)) or 0)) |
| 170 | |
| 171 | for png in png_files: |
| 172 | result = ocr.ocr(f'./pages/{png}') |
| 173 | page_num = ''.join(filter(str.isdigit, png)) |
| 174 | lines = [] |
| 175 | for line_info in result[0]: |
| 176 | text = line_info[1][0] |
| 177 | conf = line_info[1][1] |
| 178 | if conf > 0.6: |
| 179 | lines.append(text) |
| 180 | output_pages[page_num] = lines |
| 181 | ``` |
| 182 | |
| 183 | ### Step 3: Assemble Markdown |
| 184 | |
| 185 | ```markdown |
| 186 | # OCR Result — filename |
| 187 | > Engine: PaddleOCR PP-OCRv4 (Simplified Chinese) |
| 188 | > Pages: 17 | Date: YYYY-MM-DD |
| 189 | |
| 190 | --- |
| 191 | |
| 192 | ## Page 1 |
| 193 | |
| 194 | Body text... |
| 195 | |
| 196 | --- |
| 197 | |
| 198 | ## Page 2 |
| 199 | |
| 200 | Body text... |
| 201 | ``` |
| 202 | |
| 203 | ## Accuracy |
| 204 | |
| 205 | | Engine | Simplified Chinese | Traditional Chinese | Seals/Signatures | Vertical Text | |
| 206 | |--------|-------------------|-------------------|------------------|---------------| |
| 207 | | Tesseract chi_sim | ~80% | ~60% | ❌ garbled | ❌ not supported | |
| 208 | | **PaddleOCR PP-OCRv4** | **~98%** | **~95%** | **✅ mostly correct** | **✅ supported** | |
| 209 | | **PaddleOCR + 预处理** | **~98%+** | **~96%** | **✅** | **✅** | |
| 210 | |
| 211 | > 预处理(倾斜校正/文档展平)对手机拍照、弯曲文档提升 1~5%。 |
| 212 | > 详见 SKILL.md D 节 `scripts/pdf_preprocess.py`。 |
| 213 | |
| 214 | ## Troubleshooting |
| 215 | |
| 216 | ### Poor character accuracy? |
| 217 | - Increase DPI: minimum 300, recommended 400-600 |
| 218 | - Lower confidence threshold: `if conf > 0.3` to retain more candidates |
| 219 | - Verify critical pages (signatures, amounts) with `vision_analyze` |
| 220 | |
| 221 | ### Poor table recognition? |
| 222 | PaddleOCR's table recognition (`lang='ch'` + `use_angle_cls=True`) is moderate. For dense tables, combine with the main `pdf-ocr-md` docling+PaddleOCR pipeline. |
| 223 | |
| 224 | ### GPU acceleration |
| 225 | ```bash |
| 226 | ~/.venvs/paddleocr/bin/pip install paddlepaddle-gpu==2.6.2 |
| 227 | ``` |
| 228 | Set `use_gpu=True` when creating PaddleOCR. |
文件内容是平台检查时读出的文本,只供查看。平台不会运行包里的任何脚本或程序。

