name: "smart-ocr" description: "Perform OCR on image-based documents with multi-strategy Tesseract recognition + DeepSeek AI validation and correction. Invoke when user needs to extract text from scanned books, document screenshots, or image-heavy files."
A three-layer OCR pipeline that combines Tesseract multi-strategy recognition with DeepSeek AI validation and correction for high-accuracy text extraction from images.
.doc / .docx / .pdf file containing mostly images (scanned pages)┌──────────────────────────────────────────────────────────┐
│ Layer 0: Mosaic Detection │
│ → Detect images with dense mosaic/blurred regions │
│ → If valid Chinese char density < 5%, skip OCR entirely │
│ → Mark image as "(含馬賽克區域,已跳過識別)" │
├──────────────────────────────────────────────────────────┤
│ Layer 1: Multi-Strategy Tesseract OCR │
│ (Original / Gray / CLAHE / 2× Upscale / Sharp) │
│ → Auto-select best result by Chinese char count │
│ → Apply common OCR error correction dictionary │
├──────────────────────────────────────────────────────────┤
│ Layer 2: DeepSeek AI Validation │
│ → Judge if OCR text is coherent (score 0-10) │
│ → Route based on confidence level: │
│ ≥ 7/10: Accept as-is, minor typo fix only │
│ 4-6/10: Request DeepSeek to correct garbled chars │
│ < 4/10: Check if mosaic; if yes, discard; if not, raw │
├──────────────────────────────────────────────────────────┤
│ Layer 3: Results Assembly │
│ → Combine per-image results into structured output │
│ → Skip mosaic-only images (output note instead) │
│ → Generate .docx with continuous paragraphs │
└──────────────────────────────────────────────────────────┘
This skill requires a DeepSeek API key for the AI validation layer. Each user must provide their own key.
How to get a key: Register at platform.deepseek.com and create an API key.
How to provide the key: Set it as an environment variable before running the pipeline:
$env:DEEPSEEK_API_KEY = "sk-your-own-api-key-here"
The scripts read the key at runtime from $env:DEEPSEEK_API_KEY. No key is ever hardcoded in the skill files.
https://api.deepseek.com/v1/chat/completionsdeepseek-chat來源於7w4.net。
Note: If no API key is set, the pipeline still runs — it simply falls back to Tesseract-only OCR (Layer 1 + error dictionary), skipping the DeepSeek validation step.
| Tool | Purpose |
|---|---|
| Tesseract v5.x | Base OCR engine with chi_sim+eng |
| OpenCV (cv2) | Image preprocessing (CLAHE, sharpening, etc.) |
| Python 3.x | Script execution |
| Node.js + docx | Generating the final .docx output |
# Pseudo-code — see scripts/ocr_pipeline.py for full implementation
1. Convert .doc → .docx (via LibreOffice: soffice --headless --convert-to docx)
2. Unpack .docx to extract /word/media/* images
3. Sort images by index (image1.png, image2.png, ...)
Before running OCR, detect images that contain mostly mosaic/blurred/redacted regions:
| Criterion | Logic |
|---|---|
| Valid text density | After initial quick OCR pass, if Chinese chars < 5% of total chars, classify as "mosaic" |
| Edge detection | Mosaic regions have abnormally uniform pixel blocks — detect via cv2.Laplacian variance < threshold |
| Small image | Images smaller than 150×150 px are likely icons, stamps or fully mosaic — skip |
When an image is classified as mosaic-containing:
- Skip the full OCR + DeepSeek pipeline for that image
- Output (含馬賽克區域,已跳過識別) in the final document
- The image's entry is still listed with its filename but no text content
### Step 2: Multi-Strategy OCR
For each image, run Tesseract with **5 preprocessing strategies** and auto-select the best:
| Strategy | Code | Best For |
|----------|------|----------|
| **Original** | Raw image → OCR | Clean photos, good lighting |
| **Gray** | `cv2.COLOR_BGR2GRAY` | Standard scanned pages |
| **CLAHE** | `cv2.createCLAHE(clipLimit=2.0)` | Uneven lighting, faded text |
| **2× Upscale** | `cv2.resize(..., INTER_CUBIC)` | Small text, low DPI |
| **Sharp** | `cv2.addWeighted(sharp)` | Slightly blurry edges |
**Selection criterion**: Run each strategy with `--psm 6 --oem 1` and pick the result with the highest Chinese character count.
### Step 3: Apply OCR Error Dictionary + Space Removal
After Tesseraut, apply a correction map for common Tesseract errors found in testing, then remove inter-CJK spaces:
```python
# Common Chinese char substitutions from Tesseract
ocr_corrections = {
"睿": "特", # 睿(ruì) vs 特(tè) — very common
"藹": "勒", # 形近字
# See full list in scripts/ocr_errors.json
}
# Remove spaces between Chinese characters (Tesseract artifact)
# "等 、 博 愛 而 慶 祝" → "等、博愛而慶祝"
# English words and numbers are kept intact
For each image's OCR text, call the DeepSeek API.
Validation Prompt — checks coherence:
你是一位專業的OCR校對專家。請評估以下OCR識別出的文字的通順程度。
評分標準(0-10分):
- 8-10分:意思通順,基本無錯別字
- 5-7分:基本可讀但有個別錯別字或亂碼
- 2-4分:部分可讀但存在大量亂碼
- 0-1分:幾乎完全不可讀
注意:這是書籍/文章內容,是一段連續的論述文字。
請直接輸出分數,不需要解釋。
文字內容:
{ocr_text}
Correction Prompt — for score 4-6:
你是一位專業的OCR文字校對專家。請對以下OCR識別出的文字進行修正。
修正原則:
1. 只修改明顯錯誤的字詞(形近字、音近字)
2. 不要改變原意和句式結構
3. 保持原文的標點和段落
4. 如果遇到無法判斷的亂碼,保持原樣不要猜測
5. 專有名詞(人名、地名、書名)請根據上下文合理修正
只輸出修正後的文字,不要新增任何解釋。
待修正的文字:
{ocr_text}
Generate a .docx file with:
- Each image → labeled section (▎圖片 N (filename.png))
- OCR text as a single continuous paragraph (no internal line breaks)
- Separator between images
The skill includes these scripts in scripts/:
| Script | Function |
|---|---|
ocr_pipeline.py |
Full pipeline: image extraction → multi-strategy OCR → cleaning → results |
ocr_deepseek.py |
DeepSeek API: validation scoring + text correction |
ocr_errors.json |
Common Tesseract error patterns and corrections |
generate_output.js |
Generate the final .docx from OCR results |
doc_to_images.py |
Convert .doc/.docx to individual images |
| Issue | Mitigation |
|---|---|
| DeepSeek API unavailable | Fall back to Tesseract-only result |
| Image cannot be read | Mark as "(無法讀取圖片)" |
| Image detected as mosaic/redacted | Skip OCR, output "(含馬賽克區域,已跳過識別)" |
| All OCR strategies return empty | Mark as "(未識別到文字)" |
| Score < 4 after DeepSeek | Check mosaic flag; if not mosaic, output raw with "(識別質量較低)" note |
| Pattern | Example (OCR → Correct) |
|---|---|
| 形近字替換 | 睿→特, 瞿→髦, 曉→曉, 鍾→欽 |
| 英文半形亂碼 | Gs → The, aert → cent |
| 中英文間距 | 多餘空格需要去除 → 中文間空格全部移除,英文單詞間保留 |
| 漢字間空格 | Tesseract經常在中文字元間插入空格 → remove_intercjk_spaces() 自動清除 |
| 特殊符號替代 | → → -, 中文引號丟失 |
| 行首行尾噪聲 | 裝飾性橫線、頁碼識別為文字 |
這是一個專業級的文字識別工具,能從圖片和掃描文件中準確提取文字,支援多種語言。文件提供了詳細的使用指導和實用示例,對辦公場景(如處理名片、收據等)很有幫助。質量較好,但釋出前的許可證和後設資料資訊需要核對統一。適合需要頻繁處理圖片文字提取的使用者使用。