python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > OpenCV OCR图片文本提取

OpenCV OCR实现提取图片文本的完整代码

作者:weixin_30777913

本文主要介绍了使用OpenCV和EasyOCR处理图片并提取文字的流程,首先预处理图片,包括灰度化、去噪、对比度增强等锐化等步骤,此方法适用于处理低质量图片并提取其中的文字,希望对大家有所帮助

完整代码

遍历当前目录及所有子目录下的常见图片,使用 OpenCV 进行多种图像增强,然后调用 EasyOCR 提取文字,并按段落合并同一段中的行,最后把所有结果写入一个带时间戳的文本文件。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import datetime
import cv2
import numpy as np
import easyocr
# ---------- 图像预处理 ----------
def preprocess_image(image_path):
    """
    使用 OpenCV 对图片进行一系列增强,以提高 OCR 识别率。
    返回处理后的灰度图(numpy 数组),可直接送入 EasyOCR。
    """
    # 读取图片(支持中文路径)
    with open(image_path, 'rb') as f:
        data = np.frombuffer(f.read(), dtype=np.uint8)
    img = cv2.imdecode(data, cv2.IMREAD_COLOR)
    if img is None:
        raise ValueError(f"无法读取图片:{image_path}")
    # 1. 转灰度
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 2. 去噪(非局部均值去噪,保留边缘)
    denoised = cv2.fastNlMeansDenoising(gray, None, h=10, templateWindowSize=7, searchWindowSize=21)
    # 3. 对比度增强(CLAHE)
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    enhanced = clahe.apply(denoised)
    # 4. 轻微锐化(可选,改善边缘)
    kernel_sharpen = np.array([[0, -1, 0],
                               [-1, 5, -1],
                               [0, -1, 0]])
    sharpened = cv2.filter2D(enhanced, -1, kernel_sharpen)
    # 5. 尺寸调整:如果图片过小,放大到至少 800 像素宽(保持比例)
    height, width = sharpened.shape[:2]
    if width < 800:
        scale = 800.0 / width
        new_w = int(width * scale)
        new_h = int(height * scale)
        sharpened = cv2.resize(sharpened, (new_w, new_h), interpolation=cv2.INTER_CUBIC)
    return sharpened
# ---------- 段落合并 ----------
def group_into_paragraphs(results, y_gap_ratio=1.5):
    """
    将 EasyOCR 的检测结果(bbox, text, confidence)按垂直距离合并为段落。
    - 首先按 top 坐标排序所有文本行。
    - 如果当前行的 top 与上一段落最后一行的 bottom 的距离小于
      该段落平均行高的 y_gap_ratio 倍,则认为属于同一段落。
    - 每个段落内的行按照从上到下的顺序用空格连接。
    """
    if not results:
        return []
    # 给每个框计算 top, bottom, center_y, center_x
    processed = []
    for (bbox, text, conf) in results:
        # bbox 是四个点 [[x1,y1],[x2,y2],[x3,y3],[x4,y4]]
        pts = np.array(bbox)
        top = np.min(pts[:, 1])
        bottom = np.max(pts[:, 1])
        center_y = (top + bottom) / 2.0
        center_x = (np.min(pts[:, 0]) + np.max(pts[:, 0])) / 2.0
        height = bottom - top
        processed.append({
            'bbox': bbox,
            'text': text,
            'conf': conf,
            'top': top,
            'bottom': bottom,
            'center_y': center_y,
            'center_x': center_x,
            'height': height
        })
    # 按 top 排序(从上到下)
    processed.sort(key=lambda x: x['top'])
    paragraphs = []
    current_para = []
    para_avg_height = 0.0
    for item in processed:
        if not current_para:
            current_para.append(item)
            para_avg_height = item['height']
        else:
            # 上一行的 bottom 与当前行的 top 之间的间隙
            last_bottom = current_para[-1]['bottom']
            gap = item['top'] - last_bottom
            # 使用当前段落平均行高计算阈值
            threshold = para_avg_height * y_gap_ratio if para_avg_height > 0 else 20
            if gap < threshold:
                current_para.append(item)
                # 更新平均行高
                heights = [x['height'] for x in current_para]
                para_avg_height = sum(heights) / len(heights)
            else:
                # 保存当前段落,开始新段落
                paragraphs.append(current_para)
                current_para = [item]
                para_avg_height = item['height']
    if current_para:
        paragraphs.append(current_para)
    # 将每个段落内的文本按顺序(已经是 top 顺序)用空格合并
    merged_paragraphs = []
    for para in para_inner:
        # 同一段落内可能同一行有多个框(按 x 排序)
        # 这里简单按 top 归为一组视为一行,然后行间用空格连接
        # 实际上 EasyOCR 一般返回的是行级框,这里保留后续扩展能力
        lines = []
        while para:
            # 取第一行(最小 top)
            first = para[0]
            line_top = first['top']
            line_bottom = first['bottom']
            # 把所有 top 相近的框归为同一行
            same_line = [x for x in para if abs(x['top'] - line_top) < first['height'] * 0.5]
            # 同一行内按 center_x 排序
            same_line.sort(key=lambda x: x['center_x'])
            line_text = ' '.join([x['text'] for x in same_line])
            lines.append(line_text)
            # 移除已处理的行
            para = [x for x in para if x not in same_line]
        # 段落文本:行之间用空格连接(可改为换行符,看你需要)
        merged_paragraphs.append(' '.join(lines))
    return merged_paragraphs
# ---------- 主程序 ----------
def main():
    # 图片扩展名(常见格式,可自行添加)
    IMAGE_EXTENSIONS = ('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.tif', '.webp')
    # 初始化 EasyOCR(中英文混合,开启 GPU 加速)
    print("正在初始化 EasyOCR,请稍候...")
    try:
        reader = easyocr.Reader(['ch_sim', 'en'], gpu=True)
    except Exception:
        print("GPU 初始化失败,切换为 CPU 模式。")
        reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)
    # 获取当前工作目录
    root_dir = os.getcwd()
    print(f"开始遍历目录:{root_dir}")
    # 存储结果:{文件路径: [段落1, 段落2, ...]}
    ocr_results = {}
    for dirpath, _, filenames in os.walk(root_dir):
        for fname in filenames:
            if not fname.lower().endswith(IMAGE_EXTENSIONS):
                continue
            full_path = os.path.join(dirpath, fname)
            rel_path = os.path.relpath(full_path, root_dir)  # 相对路径便于阅读
            print(f"正在处理:{rel_path}")
            try:
                # 1. 图像增强
                processed_img = preprocess_image(full_path)
                # 2. OCR 识别(返回行级结果,方便后续段落合并)
                results = reader.readtext(processed_img, paragraph=False)
                if not results:
                    print(f"  -> 未检测到文本")
                    ocr_results[rel_path] = []
                    continue
                # 3. 段落合并
                paragraphs = group_into_paragraphs(results, y_gap_ratio=1.5)
                ocr_results[rel_path] = paragraphs
                print(f"  -> 检测到 {len(results)} 个文本行,合并为 {len(paragraphs)} 个段落")
            except Exception as e:
                print(f"  -> 处理出错:{e}")
                ocr_results[rel_path] = []
    # ---------- 写入结果文件 ----------
    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    output_filename = f"ocr_results_{timestamp}.txt"
    with open(output_filename, 'w', encoding='utf-8') as f:
        for img_path, paragraphs in ocr_results.items():
            f.write(f"===== {img_path} =====\n")
            if paragraphs:
                for i, para in enumerate(paragraphs, 1):
                    f.write(f"[段落 {i}]\n{para}\n\n")
            else:
                f.write("(未识别到文本)\n\n")
            f.write("\n")  # 不同图片之间再空一行
    print(f"\n所有处理完成!结果已保存至:{output_filename}")
if __name__ == "__main__":
    main()

代码说明

1.图像预处理

2.段落合并

3.EasyOCR 调用

4.输出文件

运行环境准备

pip install easyocr opencv-python numpy

将脚本放在需要处理的目录下,直接运行即可。

到此这篇关于OpenCV OCR实现提取图片文本的完整代码的文章就介绍到这了,更多相关OpenCV OCR图片文本提取内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文