技能备份 - 2026-04-15 (40个技能)
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
---
|
||||
name: ocr
|
||||
description: Optical Character Recognition (OCR) tool, supports Chinese and English text extraction from PDFs and images. Use cases: (1) extract text from scanned PDFs, (2) recognize text from images, (3) extract text content from invoices, contracts, and other documents
|
||||
---
|
||||
|
||||
# OCR Text Recognition
|
||||
|
||||
This skill uses PaddleOCR for text recognition, supporting both Chinese and English.
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Basic Usage
|
||||
|
||||
Perform OCR recognition directly on image or PDF files:
|
||||
|
||||
```python
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
ocr = PaddleOCR(lang='ch')
|
||||
result = ocr.predict("file_path.jpg")
|
||||
```
|
||||
|
||||
## Dependency Installation
|
||||
|
||||
Install dependencies before first use:
|
||||
|
||||
```bash
|
||||
pip3 install paddlepaddle paddleocr
|
||||
```
|
||||
|
||||
## Output Format
|
||||
|
||||
Recognition results return JSON containing:
|
||||
- `rec_texts`: List of recognized text
|
||||
- `rec_scores`: Confidence score for each text
|
||||
|
||||
## Typical Use Cases
|
||||
|
||||
1. **PDF Scans**: Use PyMuPDF to extract images first, then OCR
|
||||
2. **Image Text Recognition**: Perform OCR directly on images
|
||||
3. **Multi-page PDFs**: Process page by page
|
||||
|
||||
## Scripts
|
||||
|
||||
Common scripts are located in the `scripts/` directory.
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
name: ocr
|
||||
description: 光学字符识别 (OCR) 工具,支持从 PDF 和图片中提取中英文文本。适用场景:(1) 从扫描版 PDF 提取文字,(2) 识别图片中的文字,(3) 提取发票、合同等文档的文字内容
|
||||
---
|
||||
|
||||
# OCR 文字识别
|
||||
|
||||
本技能使用 PaddleOCR 进行文字识别,支持中文和英文。
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 基础用法
|
||||
|
||||
直接对图片或 PDF 文件进行 OCR 识别:
|
||||
|
||||
```python
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
ocr = PaddleOCR(lang='ch')
|
||||
result = ocr.predict("file_path.jpg")
|
||||
```
|
||||
|
||||
## 依赖安装
|
||||
|
||||
首次使用前请安装依赖:
|
||||
|
||||
```bash
|
||||
pip3 install paddlepaddle paddleocr
|
||||
```
|
||||
|
||||
## 输出格式
|
||||
|
||||
识别结果返回 JSON 格式,包含:
|
||||
- `rec_texts`: 识别出的文字列表
|
||||
- `rec_scores`: 每段文字的置信度分数
|
||||
|
||||
## 典型使用场景
|
||||
|
||||
1. **PDF 扫描件**:先用 PyMuPDF 提取图片,再进行 OCR
|
||||
2. **图片文字识别**:直接对图片进行 OCR
|
||||
3. **多页 PDF**:逐页处理
|
||||
|
||||
## 脚本
|
||||
|
||||
常用脚本位于 `scripts/` 目录下。
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"ownerId": "kn7cpqbnatvf5664znnnamfg6s81jzv4",
|
||||
"slug": "ocr-python",
|
||||
"version": "1.0.0",
|
||||
"publishedAt": 1771704541442
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
OCR Text Recognition Script
|
||||
Usage: python3 ocr.py <file_path> [--output <output_file>]
|
||||
Supported: .pdf, .jpg, .jpeg, .png
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
|
||||
def extract_images_from_pdf(pdf_path):
|
||||
"""Extract images from PDF"""
|
||||
import fitz
|
||||
|
||||
images = []
|
||||
doc = fitz.open(pdf_path)
|
||||
|
||||
for page_num in range(len(doc)):
|
||||
page = doc[page_num]
|
||||
page_images = page.get_images()
|
||||
|
||||
for img_index, img in enumerate(page_images):
|
||||
xref = img[0]
|
||||
base_image = doc.extract_image(xref)
|
||||
image_bytes = base_image["image"]
|
||||
image_ext = base_image["ext"]
|
||||
|
||||
output_path = f"/tmp/pdf_page{page_num+1}_img{img_index}.{image_ext}"
|
||||
with open(output_path, "wb") as f:
|
||||
f.write(image_bytes)
|
||||
images.append(output_path)
|
||||
|
||||
doc.close()
|
||||
return images
|
||||
|
||||
|
||||
def ocr_file(file_path, output_path=None):
|
||||
"""Perform OCR recognition on file"""
|
||||
from paddleocr import PaddleOCR
|
||||
|
||||
# Initialize OCR
|
||||
ocr = PaddleOCR(lang='ch', use_angle_cls=True)
|
||||
|
||||
ext = os.path.splitext(file_path)[1].lower()
|
||||
|
||||
if ext == '.pdf':
|
||||
# PDF: extract images first, then recognize
|
||||
images = extract_images_from_pdf(file_path)
|
||||
all_texts = []
|
||||
|
||||
for img_path in images:
|
||||
print(f"Recognizing: {img_path}")
|
||||
result = ocr.predict(img_path)
|
||||
if result:
|
||||
texts = result[0].get('rec_texts', [])
|
||||
all_texts.extend(texts)
|
||||
|
||||
# Clean up temporary images
|
||||
for img_path in images:
|
||||
try:
|
||||
os.remove(img_path)
|
||||
except:
|
||||
pass
|
||||
|
||||
final_texts = all_texts
|
||||
else:
|
||||
# Image: recognize directly
|
||||
print(f"Recognizing: {file_path}")
|
||||
result = ocr.predict(file_path)
|
||||
|
||||
if result and len(result) > 0:
|
||||
final_texts = result[0].get('rec_texts', [])
|
||||
else:
|
||||
final_texts = []
|
||||
|
||||
# Output results
|
||||
if output_path:
|
||||
with open(output_path, 'w', encoding='utf-8') as f:
|
||||
for text in final_texts:
|
||||
f.write(text + '\n')
|
||||
print(f"Results saved to: {output_path}")
|
||||
else:
|
||||
print("\n=== OCR Recognition Results ===\n")
|
||||
for text in final_texts:
|
||||
print(text)
|
||||
|
||||
return final_texts
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='OCR Text Recognition Tool')
|
||||
parser.add_argument('file', help='File to recognize (PDF or image)')
|
||||
parser.add_argument('--output', '-o', help='Output file path (optional)')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if not os.path.exists(args.file):
|
||||
print(f"Error: File not found: {args.file}")
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
ocr_file(args.file, args.output)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user