Python 实现图片提取文字

文章目录

一、效果图

二、库安装

三、使用示例

四、完整代码


一、效果图

使用的图片:

返回文字:

二、库安装

pip install easyocr opencv-python numpy  

三、使用示例

ocr = EasyOCRProcessor()
results = ocr.extract_text(
    "test.png",
    "output.png",
    confidence_threshold=0.6
)

四、完整代码

import easyocr
import cv2
import numpy as np


class EasyOCRProcessor:
    def __init__(self, languages=['ch_sim', 'en']):
        """
        初始化EasyOCR处理器

        参数:
            languages: 需要识别的语言列表
        """
        self.reader = easyocr.Reader(languages)

    def enhance_image(self, image):
        """
        图像增强处理

        参数:
            image: OpenCV图像对象
        返回:
            处理后的图像
        """
        # 亮度和对比度调整
        alpha = 1.2  # 对比度
        beta = 10  # 亮度
        adjusted = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)

        # 锐化
        kernel = np.array([[-1, -1, -1],
                           [-1, 9, -1],
                           [-1, -1, -1]])
        sharpened = cv2.filter2D(adjusted, -1, kernel)

        return sharpened

    def extract_text(self, image_path, output_path=None, confidence_threshold=0.5):
        """
        提取图片中的文字

        参数:
            image_path: 图片路径
            output_path: 可选,输出处理后图片的路径
            confidence_threshold: 置信度阈值
        返回:
            提取的文字内容和位置信息
        """
        try:
            # 读取图片
            image = cv2.imread(image_path)
            if image is None:
                raise ValueError("无法读取图片")

            # 图像增强
            enhanced = self.enhance_image(image)

            # 使用EasyOCR识别文字
            results = self.reader.readtext(enhanced)

            # 处理结果
            text_results = []
            for bbox, text, confidence in results:
                if confidence > confidence_threshold:
                    text_results.append({
                        'text': text,
                        'confidence': confidence,
                        'position': bbox
                    })

                    # 在图片上标记文字区域
                    if output_path:
                        points = np.array(bbox, np.int32)
                        cv2.polylines(image, [points], True, (0, 255, 0), 2)
                        cv2.putText(image, f"{text} ({confidence:.2f})",
                                    (int(bbox[0][0]), int(bbox[0][1]) - 10),
                                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

            # 保存处理后的图片
            if output_path:
                cv2.imwrite(output_path, image)

            return text_results

        except Exception as e:
            print(f"错误: {str(e)}")
            return None


# 使用示例
ocr = EasyOCRProcessor()
results = ocr.extract_text(
    "test.png",
    "output.png",
    confidence_threshold=0.6
)

# 打印结果
if results:
    for result in results:
        print(f"文字: {result['text']}")
        print(f"置信度: {result['confidence']}")
        print(f"位置: {result['position']}")
        print("---")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穿梭的编织者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值