文章目录
- Windows系统 下载Tesseract安装程序: 访问 GitHub releases页面 下载适合的Windows安装包(如:tesseract-ocr-w64-setup-5.3.3.20231005.exe) 运行安装程序,注意勾选"Additional language data"以安装多语言支持 将Tesseract安装路径(如:C:Program FilesTesseract-OCR)添加到系统PATH环境变量 macOS系统 # 使用Homebrew安装 brew install tesseract # 安装语言包 brew install tesseract-lang Linux系统(Ubuntu/Debian) # 更新包列表 sudo apt update # 安装Tesseract OCR sudo apt install tesseract-ocr # 安装中文语言包 sudo apt install tesseract-ocr-chi-sim tesseract-ocr-chi-tra
- # 安装Pillow用于图像处理 pip install Pillow # 安装pytesseract用于调用Tesseract OCR pip install pytesseract # 安装OpenCV用于高级图像处理(可选) pip install opencv-python # 安装numpy(通常OpenCV会依赖) pip install numpy
- 完成安装后,可以通过以下命令验证Tesseract是否正确安装: tesseract –version
- 让我们从最简单的OCR功能开始,创建一个能够识别图片中文字的基本函数。 import pytesseract from PIL import Image import os def basic_ocr(image_path, language=’eng’): “”” 基础OCR函数:识别图片中的文字 参数: image_path (str): 图片文件路径 language (str): 识别语言,默认为英语(‘eng’) 返回: str: 识别出的文本内容 “”” try: # 检查图片文件是否存在 if not os.path.exists(image_path): raise FileNotFoundError(f”图片文件不存在: {image_path}”) # 使用PIL打开图片 image = Image.open(image_path) # 使用Tesseract进行OCR识别 text = pytesseract.image_to_string(image, lang=language) return text except Exception as e: print(f”OCR识别过程中出现错误: {str(e)}”) return “” # 使用示例 if __name__ == “__main__”: # 替换为你的图片路径 image_path = “sample_text.png” result = basic_ocr(image_path) print(“识别结果:”) print(result)
- Tesseract支持多种语言,可以通过指定语言参数来识别不同语言的文字。 def multi_language_ocr(image_path, languages): “”” 多语言OCR识别 参数: image_path (str): 图片文件路径 languages (list): 语言列表,如[‘eng’, ‘chi_sim’] 返回: dict: 各语言的识别结果 “”” results = {} for lang in languages: try: image = Image.open(image_path) text = pytesseract.image_to_string(image, lang=lang) results[lang] = text except Exception as e: print(f”语言 {lang} 识别失败: {str(e)}”) results[lang] = “” return results # 使用示例 languages = [‘eng’, ‘chi_sim’, ‘fra’] # 英语、简体中文、法语 results = multi_language_ocr(“multilingual_text.png”, languages) for lang, text in results.items(): print(f”{lang} 识别结果:”) print(text) print(“-” * 50)
- 未经处理的图像可能包含以下问题: 噪声和伪影 光照不均 文本倾斜 低对比度 复杂背景 这些因素都会降低OCR的识别准确率。通过适当的预处理,我们可以显著提高识别效果。
- 5.2.1 灰度化与二值化 将彩色 图像转换为灰度图,然后进行二值化处理,可以简化后续处理步骤。 import cv2 import numpy as np from PIL import Image def preprocess_image(image_path, output_path=None): “”” 图像预处理:灰度化、二值化、去噪 参数: image_path (str): 输入图片路径 output_path (str): 预处理后图片保存路径(可选) 返回: numpy.ndarray: 预处理后的图像数组 “”” # 读取图像 image = cv2.imread(image_path) # 转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 使用高斯模糊去噪 blurred = cv2.GaussianBlur(gray, (5, 5), 0) # 使用Otsu’s二值化方法 _, binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 可选:保存预处理后的图像 if output_path: cv2.imwrite(output_path, binary) return binary # 使用预处理后的图像进行OCR def ocr_with_preprocessing(image_path): “”” 使用预处理后的图像进行OCR识别 参数: image_path (str): 图片路径 返回: str: 识别结果 “”” # 图像预处理 processed_image = preprocess_image(image_path) # 将numpy数组转换为PIL图像 pil_image = Image.fromarray(processed_image) # OCR识别 text = pytesseract.image_to_string(pil_image, lang=’eng’) return text 5.2.2 噪声去除 使用形态学操作去除小噪声点。 def remove_noise(image): “”” 使用形态学操作去除噪声 参数: image (numpy.ndarray): 输入图像 返回: numpy.ndarray: 去噪后的图像 “”” # 定义核(结构元素) kernel = np.ones((1, 1), np.uint8) # 开运算:先腐蚀后膨胀,去除小噪声点 image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) # 闭运算:先膨胀后腐蚀,填充小洞 image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) return image 5.2.3 倾斜校正 检测并校正文本的倾斜角度。 def correct_skew(image): “”” 检测并校正图像倾斜 参数: image (numpy.ndarray): 输入图像 返回: numpy.ndarray: 校正后的图像 float: 倾斜角度 “”” # 边缘检测 edges = cv2.Canny(image, 50, 150, apertureSize=3) # 霍夫直线检测 lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=100) if lines is not None: angles = [] for rho, theta in lines[:, 0]: angle = theta * 180 / np.pi – 90 angles.append(angle) # 计算平均角度 median_angle = np.median(angles) # 旋转图像校正倾斜 (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, median_angle, 1.0) corrected = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE) return corrected, median_angle return image, 0 5.2.4 对比度增强 提高图像对比度,使文本更加清晰。 def enhance_contrast(image): “”” 增强图像对比度 参数: image (numpy.ndarray): 输入图像 返回: numpy.ndarray: 对比度增强后的图像 “”” # 使用CLAHE(限制对比度自适应直方图均衡化) clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) enhanced = clahe.apply(image) return enhanced
- def complete_preprocessing(image_path, output_path=None): “”” 完整的图像预处理流程 参数: image_path (str): 输入图片路径 output_path (str): 预处理后图片保存路径(可选) 返回: numpy.ndarray: 预处理后的图像 “”” # 读取图像 image = cv2.imread(image_path) # 转换为灰度图 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 去噪 denoised = cv2.medianBlur(gray, 3) # 对比度增强 enhanced = enhance_contrast(denoised) # 二值化 _, binary = cv2.threshold(enhanced, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # 去除噪声 cleaned = remove_noise(binary) # 倾斜校正 corrected, angle = correct_skew(cleaned) print(f”检测到的倾斜角度: {angle:.2f}度”) # 可选:保存预处理后的图像 if output_path: cv2.imwrite(output_path, corrected) return corrected
- Tesseract提供了多种配置选项,可以通过config参数进行设置。 def advanced_ocr(image_path, config_options=None): “”” 使用高级配置的OCR识别 参数: image_path (str): 图片路径 config_options (str): Tesseract配置参数 返回: dict: 包含文本和详细信息的字典 “”” if config_options is None: config_options = ‘–oem 3 –psm 6’ image = Image.open(image_path) # 获取识别结果和详细信息 data = pytesseract.image_to_data(image, config=config_options, output_type=pytesseract.Output.DICT) # 提取识别文本 text = pytesseract.image_to_string(image, config=config_options) return { ‘text’: text, ‘data’: data } # 常用配置参数说明 “”” –psm N: 页面分割模式 0 = 仅方向和脚本检测 1 = 自动页面分割与文本检测 3 = 全自动页面分割,无文本检测(默认) 6 = 统一文本块 7 = 单行文本 8 = 单个单词 13 = 原始行文本 –oem N: OCR引擎模式 0 = 仅传统引擎 1 = 仅神经网络LSTM引擎 2 = 传统+LSTM引擎 3 = 默认,基于可用内容选择 “””
- 获取每个识别字符、单词或文本行的位置信息。 def get_bounding_boxes(image_path, output_image_path=None): “”” 获取文本边界框并在图像上绘制 参数: image_path (str): 输入图片路径 output_image_path (str): 带边界框的输出图片路径(可选) 返回: list: 边界框信息列表 “”” # 读取图像 image = cv2.imread(image_path) rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) pil_image = Image.fromarray(rgb_image) # 获取详细的OCR数据 data = pytesseract.image_to_data(pil_image, output_type=pytesseract.Output.DICT) boxes = [] # 遍历所有检测到的文本元素 n_boxes = len(data[‘level’]) for i in range(n_boxes): # 只处理置信度较高的结果 if int(data[‘conf’][i]) > 30: (x, y, w, h) = (data[‘left’][i], data[‘top’][i], data[‘width’][i], data[‘height’][i]) text = data[‘text’][i] boxes.append({ ‘text’: text, ‘position’: (x, y, w, h), ‘confidence’: int(data[‘conf’][i]) }) # 在图像上绘制边界框 if output_image_path: cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(image, text, (x, y – 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 保存带边界框的图像 if output_image_path: cv2.imwrite(output_image_path, image) return boxes
- import glob def batch_ocr(image_folder, output_file=”ocr_results.txt”): “”” 批量处理文件夹中的图片 参数: image_folder (str): 图片文件夹路径 output_file (str): 结果输出文件路径 “”” # 支持的图片格式 image_extensions = [‘*.png’, ‘*.jpg’, ‘*.jpeg’, ‘*.bmp’, ‘*.tiff’] image_paths = [] for extension in image_extensions: image_paths.extend(glob.glob(os.path.join(image_folder, extension))) results = [] for image_path in image_paths: print(f”处理图片: {os.path.basename(image_path)}”) try: # 预处理图像 processed_image = complete_preprocessing(image_path) pil_image = Image.fromarray(processed_image) # OCR识别 text = pytesseract.image_to_string(pil_image, lang=’eng+chi_sim’) results.append({ ‘file’: os.path.basename(image_path), ‘text’: text }) except Exception as e: print(f”处理图片 {image_path} 时出错: {str(e)}”) results.append({ ‘file’: os.path.basename(image_path), ‘text’: f”识别失败: {str(e)}” }) # 将结果写入文件 with open(output_file, ‘w’, encoding=’utf-8′) as f: for result in results: f.write(f”文件: {result[‘file’]}n”) f.write(f”识别结果:n{result[‘text’]}n”) f.write(“=” * 50 + “n”) print(f”批量处理完成,结果已保存到: {output_file}”) return results
- 不同的页面布局需要不同的分割模式: def optimize_psm(image_path): “”” 尝试不同的页面分割模式,找到最佳结果 参数: image_path (str): 图片路径 返回: dict: 各PSM模式的识别结果 “”” image = Image.open(image_path) # 定义不同的PSM模式及其描述 psm_modes = { 0: “仅方向和脚本检测”, 1: “自动页面分割与文本检测”, 3: “全自动页面分割,无文本检测(默认)”, 6: “统一文本块”, 7: “单行文本”, 8: “单个单词”, 13: “原始行文本” } results = {} for psm, description in psm_modes.items(): try: config = f’–psm {psm}’ text = pytesseract.image_to_string(image, config=config) results[psm] = { ‘description’: description, ‘text’: text } except Exception as e: results[psm] = { ‘description’: description, ‘text’: f”识别失败: {str(e)}” } return results
- 使用合适的语言模型和词典可以提高识别准确率。 def optimize_language_model(image_path, text_type=”general”): “”” 根据文本类型优化语言模型 参数: image_path (str): 图片路径 text_type (str): 文本类型,如”general”, “document”, “code”等 返回: str: 优化后的识别结果 “”” image = Image.open(image_path) # 根据文本类型选择配置 configs = { “general”: “–oem 3 –psm 6”, “document”: “–oem 3 –psm 1”, “single_line”: “–oem 3 –psm 7”, “single_word”: “–oem 3 –psm 8”, “code”: “–oem 3 –psm 6 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{}[]();:.,<>/*-+=” } config = configs.get(text_type, configs[“general”]) text = pytesseract.image_to_string(image, config=config) return text
- 对于特定领域的OCR应用,可以使用自定义词典来提高专业术语的识别准确率。 def create_custom_dictionary(word_list, dictionary_path=”custom_words.txt”): “”” 创建自定义词典 参数: word_list (list): 自定义单词列表 dictionary_path (str): 词典文件保存路径 “”” with open(dictionary_path, ‘w’, encoding=’utf-8′) as f: for word in word_list: f.write(f”{word}n”) print(f”自定义词典已创建: {dictionary_path}”) def ocr_with_custom_dictionary(image_path, dictionary_path, language=’eng’): “”” 使用自定义词典进行OCR识别 参数: image_path (str): 图片路径 dictionary_path (str): 自定义词典路径 language (str): 基础语言 返回: str: 识别结果 “”” image = Image.open(image_path) # 配置参数,加载自定义词典 config = f’–oem 3 –psm 6 –user-words {dictionary_path}’ text = pytesseract.image_to_string(image, lang=language, config=config) return text
- 将扫描的文档图片转换为可编辑的文本。 class DocumentOCR: “”” 文档OCR处理类 “”” def __init__(self, language=’eng+chi_sim’): self.language = language def process_document(self, image_path, output_text_path=None, output_pdf_path=None): “”” 处理文档图片 参数: image_path (str): 文档图片路径 output_text_path (str): 文本输出路径(可选) output_pdf_path (str): PDF输出路径(可选) 返回: dict: 处理结果 “”” try: # 图像预处理 processed_image = complete_preprocessing(image_path) pil_image = Image.fromarray(processed_image) # 获取文本 text = pytesseract.image_to_string(pil_image, lang=self.language) # 获取详细信息用于生成搜索PDF pdf_data = pytesseract.image_to_pdf_or_hocr(pil_image, extension=’pdf’, lang=self.language) result = { ‘success’: True, ‘text’: text, ‘pdf_data’: pdf_data } # 保存文本结果 if output_text_path: with open(output_text_path, ‘w’, encoding=’utf-8′) as f: f.write(text) print(f”文本结果已保存到: {output_text_path}”) # 保存PDF结果 if output_pdf_path: with open(output_pdf_path, ‘wb’) as f: f.write(pdf_data) print(f”可搜索PDF已保存到: {output_pdf_path}”) return result except Exception as e: print(f”文档处理失败: {str(e)}”) return { ‘success’: False, ‘error’: str(e) } def batch_process_documents(self, input_folder, output_folder): “”” 批量处理文档文件夹 参数: input_folder (str): 输入文件夹路径 output_folder (str): 输出文件夹路径 “”” # 创建输出文件夹 os.makedirs(output_folder, exist_ok=True) # 获取所有图片文件 image_extensions = [‘*.png’, ‘*.jpg’, ‘*.jpeg’, ‘*.bmp’, ‘*.tiff’] image_paths = [] for extension in image_extensions: image_paths.extend(glob.glob(os.path.join(input_folder, extension))) results = [] for image_path in image_paths: filename = os.path.splitext(os.path.basename(image_path))[0] output_text_path = os.path.join(output_folder, f”{filename}.txt”) output_pdf_path = os.path.join(output_folder, f”{filename}.pdf”) print(f”处理文档: {os.path.basename(image_path)}”) result = self.process_document( image_path, output_text_path, output_pdf_path ) results.append({ ‘file’: os.path.basename(image_path), ‘result’: result }) return results # 使用示例 doc_ocr = DocumentOCR(language=’eng+chi_sim’) results = doc_ocr.batch_process_documents(“input_docs”, “output_docs”)
- 从名片图片中提取联系信息。 class BusinessCardOCR: “”” 名片OCR处理类 “”” def __init__(self): self.contact_patterns = { ‘phone’: r'(+?[0-9]{1,3}[-.s]?)?((?[0-9]{1,4})?[-.s]?)?[0-9]{1,4}[-.s]?[0-9]{1,9}’, ’email’: r’b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}b’, ‘website’: r'((https?://)?(www.)?[a-zA-Z0-9-]+.[a-zA-Z]{2,}(/S*)?)’ } def extract_contact_info(self, image_path): “”” 从名片图片中提取联系信息 参数: image_path (str): 名片图片路径 返回: dict: 提取的联系信息 “”” import re # OCR识别 text = basic_ocr(image_path) contact_info = { ‘raw_text’: text, ‘name’: ”, ‘company’: ”, ‘phone’: [], ’email’: [], ‘website’: [] } # 提取电话号码 phone_matches = re.findall(self.contact_patterns[‘phone’], text) contact_info[‘phone’] = [match[0] + match[1] for match in phone_matches if any(match)] # 提取邮箱地址 contact_info[’email’] = re.findall(self.contact_patterns[’email’], text) # 提取网址 website_matches = re.findall(self.contact_patterns[‘website’], text) contact_info[‘website’] = [match[0] for match in website_matches if match[0]] # 简单的姓名和公司提取(实际应用中可能需要更复杂的NLP处理) lines = text.split(‘n’) non_empty_lines = [line.strip() for line in lines if line.strip()] if len(non_empty_lines) >= 2: contact_info[‘name’] = non_empty_lines[0] contact_info[‘company’] = non_empty_lines[1] return contact_info # 使用示例 card_ocr = BusinessCardOCR() contact_info = card_ocr.extract_contact_info(“business_card.jpg”) print(“提取的联系信息:”) for key, value in contact_info.items(): print(f”{key}: {value}”)
- 从图片中的表格提取结构化数据。 def extract_table_data(image_path): “”” 从图片中的表格提取数据 参数: image_path (str): 包含表格的图片路径 返回: list: 表格数据(二维列表) “”” # 预处理图像,特别强调垂直线和水平线 image = cv2.imread(image_path) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 二值化 _, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) # 检测水平线 horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1)) horizontal_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, horizontal_kernel) # 检测垂直线 vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 25)) vertical_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, vertical_kernel) # 合并线条 table_mask = cv2.bitwise_or(horizontal_lines, vertical_lines) # OCR识别 pil_image = Image.fromarray(cv2.bitwise_not(table_mask)) text = pytesseract.image_to_string(pil_image) # 简单的表格解析(实际应用可能需要更复杂的逻辑) table_data = [] lines = text.split(‘n’) for line in lines: if line.strip(): # 假设表格列由多个空格分隔 row = [cell.strip() for cell in line.split(‘ ‘) if cell.strip()] if row: table_data.append(row) return table_data # 使用示例 table_data = extract_table_data(“table_image.png”) print(“提取的表格数据:”) for row in table_data: print(row)
- 环境配置:学会了在不同操作系统上安装和配置Tesseract OCR 基础使用:掌握了基本的OCR识别方法和多语言支持 图像预处理:了解了各种图像预处理技术对识别准确率的影响 高级功能:学会了使用配置参数优化识别结果 实际应用:实现了文档数字化、名片信息提取等实用功能
- 随着人工智能技术的发展,OCR技术也在不断进步: 深度学习应用:基于深度学习的OCR模型在复杂场景下表现更好 端到端识别:直接从图像到结构化数据的端到端识别系统 多模态融合:结合文本、图像、布局等多种信息进行综合理解 实时处理:移动设备和边缘计算设备上的实时OCR应用
- 学习Tesseract的训练方法,创建自定义语言模型 探索其他OCR引擎,如Google Cloud Vision API、Amazon Textract等 研究基于深度学习的OCR模型,如CRNN、Attention-OCR等 了解自然语言处理技术,结合OCR结果进行更深层次的文本理解 通过不断学习和实践,你将能够构建更加智能、高效的OCR应用,解决实际工作中的文字识别需求。 注意:本文提供的代码示例需要根据实际环境进行调整。在使用前,请确保已正确安装所有依赖库,并根据具体需求修改文件路径和参数设置。 以上就是Python使用Tesseract OCR实现识别图片中的文字的详细内容,更多关于Python识别图片文字的资料请关注风君子博客其它相关文章! 您可能感兴趣的文章: 使用Python的pytesseract库开发简易OCR图片文字识别工具 Python基于微信OCR引擎实现高效图片文字识别 Python图片文字识别与提取实战记录 使用python进行图片的文字识别详细代码 Python实现识别图片为文字的示例代码 如何利用Python识别图片中的文字
目录
- 1. 引言
- 2. Tesseract OCR简介
- 2.1 Tesseract OCR的发展历史
- 2.2 Tesseract OCR的特点
- 2.3 Tesseract OCR的工作原理
- 3. 环境配置与安装
- 3.1 安装Tesseract OCR引擎
- 3.2 安装Python相关库
- 3.3 验证安装
- 4. 基础使用:简单的文字识别
- 4.1 基本OCR函数实现
- 4.2 处理不同语言的文字
- 5. 图像预处理技术
- 5.1 图像预处理的重要性
- 5.2 常用的预处理技术
- 5.3 完整的预处理流程
- 6. 高级功能与配置
- 6.1 Tesseract配置参数
- 6.2 获取边界框信息
- 6.3 批量处理多张图片
- 7. 性能优化与准确率提升
- 7.1 选择合适的页面分割模式(PSM)
- 7.2 语言模型优化
- 7.3 自定义词典
- 8. 实际应用案例
- 8.1 文档数字化
- 8.2 名片信息提取
- 8.3 表格数据提取
- 9. 完整代码实现
- 10. 常见问题与解决方案
- 10.1 识别准确率低
- 10.2 处理速度慢
- 10.3 内存占用过高
- 11. 总结与展望
- 11.1 主要收获
- 11.2 未来发展方向
- 11.3 进一步学习建议
光学字符识别(Optical Character Recognition,OCR)是一项将图像中的文字转换为可编辑文本的技术。随着数字化时代的到来,OCR技术在文档数字化、车牌识别、名片管理、自动化数据录入等领域发挥着越来越重要的作用。
在众多OCR工具中,Tesseract OCR因其开源、免费且识别准确率较高而广受欢迎。最初由惠普实验室开发,现在由Google维护,Tesseract支持100多种语言,并且可以通过训练来识别特定字体和字符集。
本文将详细介绍如何使用Python结合Tesseract OCR来实现图片中文字的识别,包括环境配置、基础使用、高级功能以及实际应用案例。
Tesseract OCR最初由惠普实验室在1985年至1994年间开发。2005年,惠普将其开源,并在2006年由Google接手维护。经过多年的发展,Tesseract已经成为最准确的开源OCR引擎之一。
- 多语言支持:支持100多种语言的文字识别
- 开源免费:遵循Apache License 2.0开源协议
- 跨平台:支持Windows、Linux、macOS等操作系统
- 可训练:支持用户自定义训练数据以提高特定场景的识别准确率
- 多种输出格式:支持纯文本、hOCR、PDF等多种输出格式
Tesseract OCR的识别过程主要包括以下几个步骤:

Windows系统
下载Tesseract安装程序:
- 访问 GitHub releases页面
- 下载适合的Windows安装包(如:tesseract-ocr-w64-setup-5.3.3.20231005.exe)
运行安装程序,注意勾选"Additional language data"以安装多语言支持
将Tesseract安装路径(如:C:Program FilesTesseract-OCR)添加到系统PATH环境变量
macOS系统
# 使用Homebrew安装 brew install tesseract # 安装语言包 brew install tesseract-lang
Linux系统(Ubuntu/Debian)
# 更新包列表 sudo apt update # 安装Tesseract OCR sudo apt install tesseract-ocr # 安装中文语言包 sudo apt install tesseract-ocr-chi-sim tesseract-ocr-chi-tra
# 安装Pillow用于图像处理
pip install Pillow
# 安装pytesseract用于调用Tesseract OCR
pip install pytesseract
# 安装OpenCV用于高级图像处理(可选)
pip install opencv-python
# 安装numpy(通常OpenCV会依赖)
pip install numpy
完成安装后,可以通过以下命令验证Tesseract是否正确安装:
tesseract --version
让我们从最简单的OCR功能开始,创建一个能够识别图片中文字的基本函数。
import pytesseract
from PIL import Image
import os
def basic_ocr(image_path, language='eng'):
"""
基础OCR函数:识别图片中的文字
参数:
image_path (str): 图片文件路径
language (str): 识别语言,默认为英语('eng')
返回:
str: 识别出的文本内容
"""
try:
# 检查图片文件是否存在
if not os.path.exists(image_path):
raise FileNotFoundError(f"图片文件不存在: {image_path}")
# 使用PIL打开图片
image = Image.open(image_path)
# 使用Tesseract进行OCR识别
text = pytesseract.image_to_string(image, lang=language)
return text
except Exception as e:
print(f"OCR识别过程中出现错误: {str(e)}")
return ""
# 使用示例
if __name__ == "__main__":
# 替换为你的图片路径
image_path = "sample_text.png"
result = basic_ocr(image_path)
print("识别结果:")
print(result)
Tesseract支持多种语言,可以通过指定语言参数来识别不同语言的文字。
def multi_language_ocr(image_path, languages):
"""
多语言OCR识别
参数:
image_path (str): 图片文件路径
languages (list): 语言列表,如['eng', 'chi_sim']
返回:
dict: 各语言的识别结果
"""
results = {}
for lang in languages:
try:
image = Image.open(image_path)
text = pytesseract.image_to_string(image, lang=lang)
results[lang] = text
except Exception as e:
print(f"语言 {lang} 识别失败: {str(e)}")
results[lang] = ""
return results
# 使用示例
languages = ['eng', 'chi_sim', 'fra'] # 英语、简体中文、法语
results = multi_language_ocr("multilingual_text.png", languages)
for lang, text in results.items():
print(f"{lang} 识别结果:")
print(text)
print("-" * 50)
OCR识别的准确率很大程度上取决于输入图像的质量。本节介绍几种常用的图像预处理技术。
未经处理的图像可能包含以下问题:
- 噪声和伪影
- 光照不均
- 文本倾斜
- 低对比度
- 复杂背景
这些因素都会降低OCR的识别准确率。通过适当的预处理,我们可以显著提高识别效果。
5.2.1 灰度化与二值化
将彩色 图像转换为灰度图,然后进行二值化处理,可以简化后续处理步骤。
import cv2
import numpy as np
from PIL import Image
def preprocess_image(image_path, output_path=None):
"""
图像预处理:灰度化、二值化、去噪
参数:
image_path (str): 输入图片路径
output_path (str): 预处理后图片保存路径(可选)
返回:
numpy.ndarray: 预处理后的图像数组
"""
# 读取图像
image = cv2.imread(image_path)
# 转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 使用高斯模糊去噪
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 使用Otsu's二值化方法
_, binary = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 可选:保存预处理后的图像
if output_path:
cv2.imwrite(output_path, binary)
return binary
# 使用预处理后的图像进行OCR
def ocr_with_preprocessing(image_path):
"""
使用预处理后的图像进行OCR识别
参数:
image_path (str): 图片路径
返回:
str: 识别结果
"""
# 图像预处理
processed_image = preprocess_image(image_path)
# 将numpy数组转换为PIL图像
pil_image = Image.fromarray(processed_image)
# OCR识别
text = pytesseract.image_to_string(pil_image, lang='eng')
return text
5.2.2 噪声去除
使用形态学操作去除小噪声点。
def remove_noise(image):
"""
使用形态学操作去除噪声
参数:
image (numpy.ndarray): 输入图像
返回:
numpy.ndarray: 去噪后的图像
"""
# 定义核(结构元素)
kernel = np.ones((1, 1), np.uint8)
# 开运算:先腐蚀后膨胀,去除小噪声点
image = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)
# 闭运算:先膨胀后腐蚀,填充小洞
image = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)
return image
5.2.3 倾斜校正
检测并校正文本的倾斜角度。
def correct_skew(image):
"""
检测并校正图像倾斜
参数:
image (numpy.ndarray): 输入图像
返回:
numpy.ndarray: 校正后的图像
float: 倾斜角度
"""
# 边缘检测
edges = cv2.Canny(image, 50, 150, apertureSize=3)
# 霍夫直线检测
lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=100)
if lines is not None:
angles = []
for rho, theta in lines[:, 0]:
angle = theta * 180 / np.pi - 90
angles.append(angle)
# 计算平均角度
median_angle = np.median(angles)
# 旋转图像校正倾斜
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, median_angle, 1.0)
corrected = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
return corrected, median_angle
return image, 0
5.2.4 对比度增强
提高图像对比度,使文本更加清晰。
def enhance_contrast(image):
"""
增强图像对比度
参数:
image (numpy.ndarray): 输入图像
返回:
numpy.ndarray: 对比度增强后的图像
"""
# 使用CLAHE(限制对比度自适应直方图均衡化)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
enhanced = clahe.apply(image)
return enhanced
def complete_preprocessing(image_path, output_path=None):
"""
完整的图像预处理流程
参数:
image_path (str): 输入图片路径
output_path (str): 预处理后图片保存路径(可选)
返回:
numpy.ndarray: 预处理后的图像
"""
# 读取图像
image = cv2.imread(image_path)
# 转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 去噪
denoised = cv2.medianBlur(gray, 3)
# 对比度增强
enhanced = enhance_contrast(denoised)
# 二值化
_, binary = cv2.threshold(enhanced, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 去除噪声
cleaned = remove_noise(binary)
# 倾斜校正
corrected, angle = correct_skew(cleaned)
print(f"检测到的倾斜角度: {angle:.2f}度")
# 可选:保存预处理后的图像
if output_path:
cv2.imwrite(output_path, corrected)
return corrected
Tesseract提供了多种配置选项,可以通过config参数进行设置。
def advanced_ocr(image_path, config_options=None):
"""
使用高级配置的OCR识别
参数:
image_path (str): 图片路径
config_options (str): Tesseract配置参数
返回:
dict: 包含文本和详细信息的字典
"""
if config_options is None:
config_options = '--oem 3 --psm 6'
image = Image.open(image_path)
# 获取识别结果和详细信息
data = pytesseract.image_to_data(image, config=config_options, output_type=pytesseract.Output.DICT)
# 提取识别文本
text = pytesseract.image_to_string(image, config=config_options)
return {
'text': text,
'data': data
}
# 常用配置参数说明
"""
--psm N: 页面分割模式
0 = 仅方向和脚本检测
1 = 自动页面分割与文本检测
3 = 全自动页面分割,无文本检测(默认)
6 = 统一文本块
7 = 单行文本
8 = 单个单词
13 = 原始行文本
--oem N: OCR引擎模式
0 = 仅传统引擎
1 = 仅神经网络LSTM引擎
2 = 传统+LSTM引擎
3 = 默认,基于可用内容选择
"""
获取每个识别字符、单词或文本行的位置信息。
def get_bounding_boxes(image_path, output_image_path=None):
"""
获取文本边界框并在图像上绘制
参数:
image_path (str): 输入图片路径
output_image_path (str): 带边界框的输出图片路径(可选)
返回:
list: 边界框信息列表
"""
# 读取图像
image = cv2.imread(image_path)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_image)
# 获取详细的OCR数据
data = pytesseract.image_to_data(pil_image, output_type=pytesseract.Output.DICT)
boxes = []
# 遍历所有检测到的文本元素
n_boxes = len(data['level'])
for i in range(n_boxes):
# 只处理置信度较高的结果
if int(data['conf'][i]) > 30:
(x, y, w, h) = (data['left'][i], data['top'][i], data['width'][i], data['height'][i])
text = data['text'][i]
boxes.append({
'text': text,
'position': (x, y, w, h),
'confidence': int(data['conf'][i])
})
# 在图像上绘制边界框
if output_image_path:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
# 保存带边界框的图像
if output_image_path:
cv2.imwrite(output_image_path, image)
return boxes
import glob
def batch_ocr(image_folder, output_file="ocr_results.txt"):
"""
批量处理文件夹中的图片
参数:
image_folder (str): 图片文件夹路径
output_file (str): 结果输出文件路径
"""
# 支持的图片格式
image_extensions = ['*.png', '*.jpg', '*.jpeg', '*.bmp', '*.tiff']
image_paths = []
for extension in image_extensions:
image_paths.extend(glob.glob(os.path.join(image_folder, extension)))
results = []
for image_path in image_paths:
print(f"处理图片: {os.path.basename(image_path)}")
try:
# 预处理图像
processed_image = complete_preprocessing(image_path)
pil_image = Image.fromarray(processed_image)
# OCR识别
text = pytesseract.image_to_string(pil_image, lang='eng+chi_sim')
results.append({
'file': os.path.basename(image_path),
'text': text
})
except Exception as e:
print(f"处理图片 {image_path} 时出错: {str(e)}")
results.append({
'file': os.path.basename(image_path),
'text': f"识别失败: {str(e)}"
})
# 将结果写入文件
with open(output_file, 'w', encoding='utf-8') as f:
for result in results:
f.write(f"文件: {result['file']}n")
f.write(f"识别结果:n{result['text']}n")
f.write("=" * 50 + "n")
print(f"批量处理完成,结果已保存到: {output_file}")
return results
不同的页面布局需要不同的分割模式:
def optimize_psm(image_path):
"""
尝试不同的页面分割模式,找到最佳结果
参数:
image_path (str): 图片路径
返回:
dict: 各PSM模式的识别结果
"""
image = Image.open(image_path)
# 定义不同的PSM模式及其描述
psm_modes = {
0: "仅方向和脚本检测",
1: "自动页面分割与文本检测",
3: "全自动页面分割,无文本检测(默认)",
6: "统一文本块",
7: "单行文本",
8: "单个单词",
13: "原始行文本"
}
results = {}
for psm, description in psm_modes.items():
try:
config = f'--psm {psm}'
text = pytesseract.image_to_string(image, config=config)
results[psm] = {
'description': description,
'text': text
}
except Exception as e:
results[psm] = {
'description': description,
'text': f"识别失败: {str(e)}"
}
return results
使用合适的语言模型和词典可以提高识别准确率。
def optimize_language_model(image_path, text_type="general"):
"""
根据文本类型优化语言模型
参数:
image_path (str): 图片路径
text_type (str): 文本类型,如"general", "document", "code"等
返回:
str: 优化后的识别结果
"""
image = Image.open(image_path)
# 根据文本类型选择配置
configs = {
"general": "--oem 3 --psm 6",
"document": "--oem 3 --psm 1",
"single_line": "--oem 3 --psm 7",
"single_word": "--oem 3 --psm 8",
"code": "--oem 3 --psm 6 -c tessedit_char_whitelist=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz{}[]();:.,<>/*-+="
}
config = configs.get(text_type, configs["general"])
text = pytesseract.image_to_string(image, config=config)
return text
对于特定领域的OCR应用,可以使用自定义词典来提高专业术语的识别准确率。
def create_custom_dictionary(word_list, dictionary_path="custom_words.txt"):
"""
创建自定义词典
参数:
word_list (list): 自定义单词列表
dictionary_path (str): 词典文件保存路径
"""
with open(dictionary_path, 'w', encoding='utf-8') as f:
for word in word_list:
f.write(f"{word}n")
print(f"自定义词典已创建: {dictionary_path}")
def ocr_with_custom_dictionary(image_path, dictionary_path, language='eng'):
"""
使用自定义词典进行OCR识别
参数:
image_path (str): 图片路径
dictionary_path (str): 自定义词典路径
language (str): 基础语言
返回:
str: 识别结果
"""
image = Image.open(image_path)
# 配置参数,加载自定义词典
config = f'--oem 3 --psm 6 --user-words {dictionary_path}'
text = pytesseract.image_to_string(image, lang=language, config=config)
return text
将扫描的文档图片转换为可编辑的文本。
class DocumentOCR:
"""
文档OCR处理类
"""
def __init__(self, language='eng+chi_sim'):
self.language = language
def process_document(self, image_path, output_text_path=None, output_pdf_path=None):
"""
处理文档图片
参数:
image_path (str): 文档图片路径
output_text_path (str): 文本输出路径(可选)
output_pdf_path (str): PDF输出路径(可选)
返回:
dict: 处理结果
"""
try:
# 图像预处理
processed_image = complete_preprocessing(image_path)
pil_image = Image.fromarray(processed_image)
# 获取文本
text = pytesseract.image_to_string(pil_image, lang=self.language)
# 获取详细信息用于生成搜索PDF
pdf_data = pytesseract.image_to_pdf_or_hocr(pil_image, extension='pdf', lang=self.language)
result = {
'success': True,
'text': text,
'pdf_data': pdf_data
}
# 保存文本结果
if output_text_path:
with open(output_text_path, 'w', encoding='utf-8') as f:
f.write(text)
print(f"文本结果已保存到: {output_text_path}")
# 保存PDF结果
if output_pdf_path:
with open(output_pdf_path, 'wb') as f:
f.write(pdf_data)
print(f"可搜索PDF已保存到: {output_pdf_path}")
return result
except Exception as e:
print(f"文档处理失败: {str(e)}")
return {
'success': False,
'error': str(e)
}
def batch_process_documents(self, input_folder, output_folder):
"""
批量处理文档文件夹
参数:
input_folder (str): 输入文件夹路径
output_folder (str): 输出文件夹路径
"""
# 创建输出文件夹
os.makedirs(output_folder, exist_ok=True)
# 获取所有图片文件
image_extensions = ['*.png', '*.jpg', '*.jpeg', '*.bmp', '*.tiff']
image_paths = []
for extension in image_extensions:
image_paths.extend(glob.glob(os.path.join(input_folder, extension)))
results = []
for image_path in image_paths:
filename = os.path.splitext(os.path.basename(image_path))[0]
output_text_path = os.path.join(output_folder, f"{filename}.txt")
output_pdf_path = os.path.join(output_folder, f"{filename}.pdf")
print(f"处理文档: {os.path.basename(image_path)}")
result = self.process_document(
image_path,
output_text_path,
output_pdf_path
)
results.append({
'file': os.path.basename(image_path),
'result': result
})
return results
# 使用示例
doc_ocr = DocumentOCR(language='eng+chi_sim')
results = doc_ocr.batch_process_documents("input_docs", "output_docs")
从名片图片中提取联系信息。
class BusinessCardOCR:
"""
名片OCR处理类
"""
def __init__(self):
self.contact_patterns = {
'phone': r'(+?[0-9]{1,3}[-.s]?)?((?[0-9]{1,4})?[-.s]?)?[0-9]{1,4}[-.s]?[0-9]{1,9}',
'email': r'b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}b',
'website': r'((https?://)?(www.)?[a-zA-Z0-9-]+.[a-zA-Z]{2,}(/S*)?)'
}
def extract_contact_info(self, image_path):
"""
从名片图片中提取联系信息
参数:
image_path (str): 名片图片路径
返回:
dict: 提取的联系信息
"""
import re
# OCR识别
text = basic_ocr(image_path)
contact_info = {
'raw_text': text,
'name': '',
'company': '',
'phone': [],
'email': [],
'website': []
}
# 提取电话号码
phone_matches = re.findall(self.contact_patterns['phone'], text)
contact_info['phone'] = [match[0] + match[1] for match in phone_matches if any(match)]
# 提取邮箱地址
contact_info['email'] = re.findall(self.contact_patterns['email'], text)
# 提取网址
website_matches = re.findall(self.contact_patterns['website'], text)
contact_info['website'] = [match[0] for match in website_matches if match[0]]
# 简单的姓名和公司提取(实际应用中可能需要更复杂的NLP处理)
lines = text.split('n')
non_empty_lines = [line.strip() for line in lines if line.strip()]
if len(non_empty_lines) >= 2:
contact_info['name'] = non_empty_lines[0]
contact_info['company'] = non_empty_lines[1]
return contact_info
# 使用示例
card_ocr = BusinessCardOCR()
contact_info = card_ocr.extract_contact_info("business_card.jpg")
print("提取的联系信息:")
for key, value in contact_info.items():
print(f"{key}: {value}")
从图片中的表格提取结构化数据。
def extract_table_data(image_path):
"""
从图片中的表格提取数据
参数:
image_path (str): 包含表格的图片路径
返回:
list: 表格数据(二维列表)
"""
# 预处理图像,特别强调垂直线和水平线
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
# 检测水平线
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1))
horizontal_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, horizontal_kernel)
# 检测垂直线
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 25))
vertical_lines = cv2.morphologyEx(binary, cv2.MORPH_OPEN, vertical_kernel)
# 合并线条
table_mask = cv2.bitwise_or(horizontal_lines, vertical_lines)
# OCR识别
pil_image = Image.fromarray(cv2.bitwise_not(table_mask))
text = pytesseract.image_to_string(pil_image)
# 简单的表格解析(实际应用可能需要更复杂的逻辑)
table_data = []
lines = text.split('n')
for line in lines:
if line.strip():
# 假设表格列由多个空格分隔
row = [cell.strip() for cell in line.split(' ') if cell.strip()]
if row:
table_data.append(row)
return table_data
# 使用示例
table_data = extract_table_data("table_image.png")
print("提取的表格数据:")
for row in table_data:
print(row)
以下是一个完整的OCR工具类,整合了前面介绍的各种功能:
import pytesseract
import cv2
import numpy as np
from PIL import Image
import os
import glob
import re
class AdvancedOCR:
"""
高级OCR工具类
"""
def __init__(self, default_language='eng'):
self.default_language = default_language
def preprocess_image(self, image_path, output_path=None):
"""
图像预处理
参数:
image_path (str): 输入图片路径
output_path (str): 预处理后图片保存路径(可选)
返回:
numpy.ndarray: 预处理后的图像
"""
# 读取图像
image = cv2.imread(image_path)
if image is None:
raise ValueError(f"无法读取图像: {image_path}")
# 转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 去噪
denoised = cv2.medianBlur(gray, 3)
# 对比度增强
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
enhanced = clahe.apply(denoised)
# 二值化
_, binary = cv2.threshold(enhanced, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 形态学操作去噪
kernel = np.ones((1, 1), np.uint8)
cleaned = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_CLOSE, kernel)
# 可选:保存预处理后的图像
if output_path:
cv2.imwrite(output_path, cleaned)
return cleaned
def correct_skew(self, image):
"""
校正图像倾斜
参数:
image (numpy.ndarray): 输入图像
返回:
numpy.ndarray: 校正后的图像
float: 倾斜角度
"""
# 边缘检测
edges = cv2.Canny(image, 50, 150, apertureSize=3)
# 霍夫直线检测
lines = cv2.HoughLines(edges, 1, np.pi/180, threshold=100)
if lines is not None:
angles = []
for rho, theta in lines[:, 0]:
angle = theta * 180 / np.pi - 90
angles.append(angle)
# 计算中位数角度
median_angle = np.median(angles)
# 旋转图像校正倾斜
(h, w) = image.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, median_angle, 1.0)
corrected = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC,
borderMode=cv2.BORDER_REPLICATE)
return corrected, median_angle
return image, 0
def extract_text(self, image_path, language=None, psm=6, preprocess=True):
"""
提取图像中的文本
参数:
image_path (str): 图片路径
language (str): 识别语言
psm (int): 页面分割模式
preprocess (bool): 是否进行预处理
返回:
str: 识别出的文本
"""
if language is None:
language = self.default_language
try:
if preprocess:
# 预处理图像
processed_image = self.preprocess_image(image_path)
pil_image = Image.fromarray(processed_image)
else:
# 直接使用原图
pil_image = Image.open(image_path)
# 配置参数
config = f'--oem 3 --psm {psm}'
# OCR识别
text = pytesseract.image_to_string(pil_image, lang=language, config=config)
return text.strip()
except Exception as e:
print(f"文本提取失败: {str(e)}")
return ""
def extract_text_with_boxes(self, image_path, language=None, output_image_path=None, confidence_threshold=30):
"""
提取文本及边界框信息
参数:
image_path (str): 图片路径
language (str): 识别语言
output_image_path (str): 带边界框的输出图片路径(可选)
confidence_threshold (int): 置信度阈值
返回:
dict: 包含文本和边界框信息的字典
"""
if language is None:
language = self.default_language
# 读取图像
image = cv2.imread(image_path)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
pil_image = Image.fromarray(rgb_image)
# 获取详细的OCR数据
data = pytesseract.image_to_data(pil_image, lang=language, output_type=pytesseract.Output.DICT)
# 提取文本和边界框
text_boxes = []
n_boxes = len(data['level'])
for i in range(n_boxes):
if int(data['conf'][i]) > confidence_threshold:
(x, y, w, h) = (data['left'][i], data['top'][i], data['width'][i], data['height'][i])
text = data['text'][i].strip()
if text: # 只保留非空文本
text_boxes.append({
'text': text,
'position': (x, y, w, h),
'confidence': int(data['conf'][i])
})
# 在图像上绘制边界框
if output_image_path:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 0), 2)
# 保存带边界框的图像
if output_image_path:
cv2.imwrite(output_image_path, image)
# 提取完整文本
full_text = pytesseract.image_to_string(pil_image, lang=language)
return {
'full_text': full_text.strip(),
'text_boxes': text_boxes,
'raw_data': data
}
def batch_process(self, input_folder, output_folder, language=None):
"""
批量处理文件夹中的图片
参数:
input_folder (str): 输入文件夹路径
output_folder (str): 输出文件夹路径
language (str): 识别语言
返回:
list: 处理结果列表
"""
if language is None:
language = self.default_language
# 创建输出文件夹
os.makedirs(output_folder, exist_ok=True)
# 获取所有图片文件
image_extensions = ['*.png', '*.jpg', '*.jpeg', '*.bmp', '*.tiff']
image_paths = []
for extension in image_extensions:
image_paths.extend(glob.glob(os.path.join(input_folder, extension)))
results = []
for image_path in image_paths:
filename = os.path.splitext(os.path.basename(image_path))[0]
print(f"处理图片: {os.path.basename(image_path)}")
try:
# 提取文本
text = self.extract_text(image_path, language=language)
# 保存文本结果
output_text_path = os.path.join(output_folder, f"{filename}.txt")
with open(output_text_path, 'w', encoding='utf-8') as f:
f.write(text)
# 保存带边界框的图像
output_image_path = os.path.join(output_folder, f"{filename}_boxes.png")
box_data = self.extract_text_with_boxes(
image_path,
language=language,
output_image_path=output_image_path
)
results.append({
'file': os.path.basename(image_path),
'text': text,
'boxes': box_data['text_boxes'],
'success': True
})
except Exception as e:
print(f"处理图片 {image_path} 时出错: {str(e)}")
results.append({
'file': os.path.basename(image_path),
'error': str(e),
'success': False
})
# 生成处理报告
report_path = os.path.join(output_folder, "processing_report.txt")
with open(report_path, 'w', encoding='utf-8') as f:
f.write("OCR处理报告n")
f.write("=" * 50 + "n")
successful = sum(1 for r in results if r['success'])
f.write(f"成功处理: {successful}/{len(results)} 个文件nn")
for result in results:
f.write(f"文件: {result['file']}n")
if result['success']:
f.write(f"状态: 成功n")
f.write(f"提取字符数: {len(result['text'])}n")
else:
f.write(f"状态: 失败 - {result['error']}n")
f.write("-" * 30 + "n")
print(f"批量处理完成,报告已保存到: {report_path}")
return results
# 使用示例
if __name__ == "__main__":
# 创建OCR实例
ocr = AdvancedOCR(default_language='eng+chi_sim')
# 单张图片处理
result = ocr.extract_text("sample.png")
print("识别结果:")
print(result)
# 批量处理
# results = ocr.batch_process("input_images", "output_results")
问题原因:
- 图像质量差
- 文本字体特殊
- 背景复杂
- 语言模型不匹配
解决方案:
- 优化图像预处理流程
- 尝试不同的PSM模式
- 使用合适的语言包
- 训练自定义语言模型
问题原因:
- 图像分辨率过高
- 使用了复杂的预处理
- 同时处理多语言
解决方案:
- 适当降低图像分辨率
- 根据需求简化预处理步骤
- 使用GPU加速(如果支持)
- 只加载需要的语言包
问题原因:
- 同时处理大量高分辨率图像
- 内存泄漏
解决方案:
- 分批处理大文件
- 及时释放不再使用的资源
- 使用流式处理
本文详细介绍了如何使用Python和Tesseract OCR实现图片中文字的识别。我们从基础的环境配置开始,逐步深入到图像预处理、高级功能配置、性能优化以及实际应用案例。
- 环境配置:学会了在不同操作系统上安装和配置Tesseract OCR
- 基础使用:掌握了基本的OCR识别方法和多语言支持
- 图像预处理:了解了各种图像预处理技术对识别准确率的影响
- 高级功能:学会了使用配置参数优化识别结果
- 实际应用:实现了文档数字化、名片信息提取等实用功能
随着人工智能技术的发展,OCR技术也在不断进步:
- 深度学习应用:基于深度学习的OCR模型在复杂场景下表现更好
- 端到端识别:直接从图像到结构化数据的端到端识别系统
- 多模态融合:结合文本、图像、布局等多种信息进行综合理解
- 实时处理:移动设备和边缘计算设备上的实时OCR应用
- 学习Tesseract的训练方法,创建自定义语言模型
- 探索其他OCR引擎,如Google Cloud Vision API、Amazon Textract等
- 研究基于深度学习的OCR模型,如CRNN、Attention-OCR等
- 了解自然语言处理技术,结合OCR结果进行更深层次的文本理解
通过不断学习和实践,你将能够构建更加智能、高效的OCR应用,解决实际工作中的文字识别需求。
注意:本文提供的代码示例需要根据实际环境进行调整。在使用前,请确保已正确安装所有依赖库,并根据具体需求修改文件路径和参数设置。
以上就是Python使用Tesseract OCR实现识别图片中的文字的详细内容,更多关于Python识别图片文字的资料请关注风君子博客其它相关文章!
您可能感兴趣的文章:
- 使用Python的pytesseract库开发简易OCR图片文字识别工具
- Python基于微信OCR引擎实现高效图片文字识别
- Python图片文字识别与提取实战记录
- 使用python进行图片的文字识别详细代码
- Python实现识别图片为文字的示例代码
- 如何利用Python识别图片中的文字