智能图片转文字

👤 Longer 📦 v1.0.0 ⭐ 4.3 ⬇️ 506 下载
📄 办公效率 免费 🔑 需 API Key

📖 技能介绍


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."


Smart OCR: AI-Assisted Image-to-Text Recognition

A three-layer OCR pipeline that combines Tesseract multi-strategy recognition with DeepSeek AI validation and correction for high-accuracy text extraction from images.

小葱技能7w4.net有更新,你可以访问看下。

When to Invoke

  • User uploads a .doc / .docx / .pdf file containing mostly images (scanned pages)
  • User asks to "extract text from images," "OCR these pictures," or "convert image to text"
  • User needs accurate Chinese+English bilingual OCR from document images
  • User complains about garbled OCR results and needs better quality

Architecture

┌──────────────────────────────────────────────────────────┐
│  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              │
└──────────────────────────────────────────────────────────┘

Prerequisites

1. API Key Setup

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.

  • API Endpoint: https://api.deepseek.com/v1/chat/completions
  • Model: deepseek-chat

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.

2. Dependencies (Pre-installed)

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

Workflow (Step-by-Step)

Step 1: Prepare Images

# 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, ...)

Step 0 (Pre-filter): Mosaic Detection

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

Step 4: DeepSeek Validation & Correction

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}

Step 5: Assemble Output

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

Scripts Reference

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

Error Handling

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

Known Error Patterns (from testing)

Pattern Example (OCR → Correct)
形近字替换 睿→特, 瞿→髦, 晓→晓, 钟→钦
英文半角乱码 GsThe, aertcent
中英文间距 多余空格需要去除 → 中文间空格全部移除,英文单词间保留
汉字间空格 Tesseract经常在中文字符间插入空格 → remove_intercjk_spaces() 自动清除
特殊符号替代 -, 中文引号丢失
行首行尾噪声 装饰性横线、页码识别为文字

🤖 AI 评测

这是一个专业级的文字识别工具,能从图片和扫描文档中准确提取文字,支持多种语言。文档提供了详细的使用指导和实用示例,对办公场景(如处理名片、收据等)很有帮助。质量较好,但发布前的许可证和元数据信息需要核对统一。适合需要频繁处理图片文字提取的用户使用。

📊 多维度评分

适应性4.3
规范性4.3
有效性4.3
可靠性3.9
可信度4.8

📁 包含文件 (6 个)

📄 SKILL.md 9.1 KB
📄 scripts/generate_output.js 6.7 KB
📄 scripts/ocr_deepseek.py 9.1 KB
📄 scripts/ocr_errors.json 2.1 KB
📄 scripts/ocr_pipeline.py 12.2 KB
📄 scripts/run_pipeline.py 7.1 KB