smart_ocr — 智能OCR
v1.0.0使用 PaddleOCR 从图像和扫描文档中提取文本 - 支持 100+ 种语言
运行时依赖
安装命令
点击复制技能文档
智能OCR技能概述 本技能使用PaddleOCR(一款支持100+语言的领先OCR引擎)实现从图像和扫描文档中智能提取文本。可以从照片、截图、扫描的PDF文件和手写文档中以高精度提取文本。 如何使用 提供图像或扫描文档 可选指定语言 我将提取文本并提供位置和置信度数据 示例提示: “从此截图中提取所有文本” “OCR此扫描的PDF文档” “从此名片照片中读取文本” “从此图像中提取中文和英文文本” 领域知识 PaddleOCR基础 from paddleocr import PaddleOCR # 初始化OCR引擎 ocr = PaddleOCR(use_angle_cls=True, lang='en') # 在图像上运行OCR result = ocr.ocr('image.png', cls=True) # 结果结构:[[box, (text, confidence)], ...] for line in result[0]: box = line[0] # [[x1,y1], [x2,y2], [x3,y3], [x4,y4]] text = line[1][0] # 提取的文本 conf = line[1][1] # 置信度评分 print(f"{text} ({conf:.2f})") 支持的语言 # 常见语言代码 languages = { 'en': 'English', 'ch': 'Chinese (Simplified)', 'cht': 'Chinese (Traditional)', 'japan': 'Japanese', 'korean': 'Korean', 'french': 'French', 'german': 'German', 'spanish': 'Spanish', 'russian': 'Russian', 'arabic': 'Arabic', 'hindi': 'Hindi', 'vi': 'Vietnamese', 'th': 'Thai', # ... 100+语言支持 } # 使用特定语言 ocr = PaddleOCR(lang='ch') # 中文 ocr = PaddleOCR(lang='japan') # 日语 ocr = PaddleOCR(lang='multilingual') # 自动检测 配置选项 from paddleocr import PaddleOCR ocr = PaddleOCR( # 检测设置 det_model_dir=None, # 自定义检测模型 det_limit_side_len=960, # 最大边长用于检测 det_db_thresh=0.3, # 二值化阈值 det_db_box_thresh=0.5, # 框评分阈值 # 识别设置 rec_model_dir=None, # 自定义识别模型 rec_char_dict_path=None, # 自定义字符字典 # 角度分类 use_angle_cls=True, # 启用角度分类 cls_model_dir=None, # 自定义分类模型 # 语言 lang='en', # 语言代码 # 性能 use_gpu=True, # 使用GPU(如果可用) gpu_mem=500, # GPU内存限制(MB) enable_mkldnn=True, # CPU优化 # 输出 show_log=False, # 抑制日志 ) 处理不同来源 图像文件 # 单个图像 result = ocr.ocr('image.png') # 多个图像 images = ['img1.png', 'img2.png', 'img3.png'] for img in images: result = ocr.ocr(img) process_result(result) PDF文件(扫描) from pdf2image import convert_from_path def ocr_pdf(pdf_path): """OCR扫描的PDF文件.""" # 将PDF页面转换为图像 images = convert_from_path(pdf_path) all_text = [] for i, img in enumerate(images): # 保存临时图像 temp_path = f'temp_page_{i}.png' img.save(temp_path) # OCR图像 result = ocr.ocr(temp_path) # 提取文本 page_text = '\n'.join([line[1][0] for line in result[0]]) all_text.append(f"--- Page {i+1} ---\n{page_text}") os.remove(temp_path) return '\n\n'.join(all_text) URL和字节 import requests from io import BytesIO # 从URL response = requests.get('https://example.com/image.png') result = ocr.ocr(BytesIO(response.content)) # 从字节 with open('image.png', 'rb') as f: img_bytes = f.read() result = ocr.ocr(BytesIO(img_bytes)) 结果处理 def process_ocr_result(result): """处理OCR结果为结构化数据.""" lines = [] for line in result[0]: box = line[0] text = line[1][0] confidence = line[1][1] # 计算边界框 x_coords = [p[0] for p in box] y_coords = [p[1] for p in box] lines.append({ 'text': text, 'confidence': confidence, 'bbox': { 'left': min(x_coords), 'top': min(y_coords), 'right': max(x_coords), 'bottom': max(y_coords), }, 'raw_box': box }) return lines # 按位置排序(从上到下,从左到右) def sort_by_position(lines): return sorted(lines, key=lambda x: (x['bbox']['top'], x['bbox']['left'])) 文本布局重建 def reconstruct_layout(result, line_threshold=10): """从OCR结果重建文本布局.""" lines = process_ocr_result(result) lines = sort_by_position(lines) # 将文本分组为逻辑行 text_lines = [] current_line = [] current_y = None for line in lines: y = line['bbox']['top'] if current_y is None or abs(y - current_y) < line_threshold: current_line.append(line) else: text_lines.append(current_line) current_line = [line] current_y = y if current_line: text_lines.append(current_line) return text_lines