name: pdf-ocr description: 支援雙引擎的PDF OCR識別技能,可從影印版PDF檔案和圖片檔案中提取文字內容 version: 2.1.0 author: PDF OCR Skill Team license: MIT tags: - ocr - pdf - image - text-extraction - chinese - english - siliconflow - deepseek - rapidocr - local-ocr
PDF OCR技能用於從影印版PDF檔案和圖片檔案中提取文字內容。該技能支援兩種OCR引擎: - RapidOCR(本地引擎):無需API金鑰,免費使用,識別速度快 - 矽基流動大模型(雲端引擎):使用AI大模型進行高精度OCR識別
pip install pymupdf pillow requests python-dotenv
安裝RapidOCR以獲得本地識別能力:
pip install rapidocr_onnxruntime
.env.example 檔案並重命名為 .env# OCR引擎選擇
# - "rapid": 使用RapidOCR本地引擎(預設,無需API金鑰)
# - "siliconflow": 使用矽基流動API引擎(需要API金鑰)
OCR_ENGINE=rapid
# 如果使用矽基流動API引擎,需要配置以下選項:
SILICON_FLOW_API_KEY=your_api_key_here
SILICON_FLOW_OCR_MODEL=deepseek-ai/DeepSeek-OCR
# 匯入OCR處理器
from scripts.pdf_ocr_processor import PDFOCRProcessor
# 建立處理器例項(預設使用RapidOCR)
processor = PDFOCRProcessor()
# 執行PDF OCR識別
result = processor.ocr_pdf('path/to/your/scanned.pdf')
# 獲取識別結果
print(f"識別完成,共 {result['page_count']} 頁")
print(f"使用引擎: {result['engine']}")
print(result['text'])
# 匯入OCR處理器
from scripts.pdf_ocr_processor import PDFOCRProcessor
# 建立處理器例項,指定使用矽基流動API
processor = PDFOCRProcessor(engine="siliconflow")
# 執行PDF OCR識別
result = processor.ocr_pdf('path/to/your/scanned.pdf')
# 獲取識別結果
print(f"識別完成,共 {result['page_count']} 頁")
print(result['text'])
# 匯入OCR處理器
from scripts.pdf_ocr_processor import PDFOCRProcessor
# 建立處理器例項
processor = PDFOCRProcessor() # 或 PDFOCRProcessor(engine="siliconflow")
# 執行圖片OCR識別
result = processor.ocr_image_file('path/to/your/image.jpg')
# 獲取識別結果
print(f"識別結果: {result['text']}")
# 使用預設RapidOCR引擎
python pdf_ocr_processor.py your_document.pdf
# 使用矽基流動API引擎
python pdf_ocr_processor.py your_document.pdf siliconflow
import os
from pdf_ocr_processor import PDFOCRProcessor
# 建立處理器例項
processor = PDFOCRProcessor()
# 批次處理目錄中的所有PDF檔案
pdf_dir = "path/to/pdf/files"
output_dir = "path/to/output"
os.makedirs(output_dir, exist_ok=True)
for pdf_file in os.listdir(pdf_dir):
if pdf_file.endswith('.pdf'):
pdf_path = os.path.join(pdf_dir, pdf_file)
output_path = os.path.join(output_dir, f"{os.path.splitext(pdf_file)[0]}.txt")
print(f"處理檔案: {pdf_file}")
try:
result = processor.ocr_pdf(pdf_path)
# 儲存識別結果到文本檔案
with open(output_path, 'w', encoding='utf-8') as f:
f.write(f"=== PDF OCR 識別結果 ===\n")
f.write(f"檔名: {pdf_file}\n")
f.write(f"頁數: {result['page_count']}\n")
f.write(f"使用引擎: {result['engine']}\n\n")
f.write(result['text'])
print(f"處理完成,結果已儲存到: {output_path}")
except Exception as e:
print(f"處理失敗: {e}")
from pdf_ocr_processor import PDFOCRProcessor
def process_with_best_engine(pdf_path):
"""嘗試使用RapidOCR,如果效果不佳則使用矽基流動API"""
# 首先使用RapidOCR本地引擎
rapid_processor = PDFOCRProcessor(engine="rapid")
rapid_result = rapid_processor.ocr_pdf(pdf_path)
# 簡單評估識別效果(例如:檢查識別出的文本長度)
text_length = len(rapid_result['text'])
if text_length < 100: # 如果識別出的文本太短,可能效果不佳
print("RapidOCR識別效果可能不佳,嘗試使用矽基流動API...")
silicon_processor = PDFOCRProcessor(engine="siliconflow")
silicon_result = silicon_processor.ocr_pdf(pdf_path)
return silicon_result
else:
return rapid_result
# 使用示例
result = process_with_best_engine('path/to/your/document.pdf')
print(f"識別完成,使用引擎: {result['engine']}")
print(result['text'])
{
"text": "識別的完整文本內容",
"page_count": 頁數, # 圖片檔案始終為1
"engine": "rapid" | "siliconflow" # 使用的OCR引擎
}
識別速度取決於CPU效能
更多技能請訪問小蔥技能站7w4.net。
矽基流動API引擎:
識別速度取決於檔案頁數、圖片大小和網路狀況
對於複雜的掃描版PDF或圖片,識別準確率可能會有所不同
在與 AI IDE 中的助手互動時,您可以使用以下提示詞來指定使用不同的 OCR 引擎:
示例 1:使用本地引擎
使用者:幫我處理這個掃描版 PDF,用本地 OCR 引擎快速識別
助手:好的,我將使用 RapidOCR 本地引擎為您處理。請提供 PDF 檔案路徑。
示例 2:使用雲端引擎
使用者:這個 PDF 包含手寫體,需要高精度識別,用矽基流動 API
助手:理解,我將使用矽基流動 API 大模型為您處理。請提供 PDF 檔案路徑和您的 API 金鑰(如果尚未配置)。
示例 3:自動選擇
使用者:幫我識別這個 PDF,選擇最合適的引擎
助手:我將預設使用 RapidOCR 本地引擎為您處理。如果識別效果不理想,我們可以嘗試使用矽基流動 API。
當 AI 助手接收到這些提示詞時,會:
通過使用這些提示詞,您可以在與 AI IDE 互動時靈活控制 OCR 引擎的選擇,獲得最佳的識別效果
ModuleNotFoundError: No module named 'rapidocr_onnxruntime'解決方案:安裝RapidOCR依賴:pip install rapidocr_onnxruntime
矽基流動API 401錯誤
Unauthorized: 401 Client Error解決方案:檢查API金鑰是否正確配置在.env檔案中
PDF轉圖片失敗
ImportError: No module named 'fitz'解決方案:安裝PyMuPDF依賴:pip install pymupdf
識別結果為空
MIT License - 詳見 LICENSE.txt
這個PDF OCR技能功能比較實用,能識別影印版PDF和圖片中的文字,支援中文和英文。文件寫得詳細,配置說明也比較清楚。不足之處是安裝說明有些地方容易讓人困惑,比如配置檔案可能需要使用者自己建立;另外版本號在幾個地方顯示得不一樣,可能會造成混淆。整體來說功能完整,適合需要從掃描文件中提取文字的使用者使用。