个人技术分享

如下有三中方法:

方法1. 使用Tesseract OCR(pytesseract)

安装依赖

首先,确保你已经安装了Tesseract OCR引擎(例如,通过你的操作系统的包管理器)。然后,你可以通过pip安装pytesseractPillow(PIL的分支)。

pip install pytesseract pillow

你可能还需要设置Tesseract OCR引擎的路径,例如:

import pytesseract
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'  # 例如:r'C:\Program Files\Tesseract-OCR\tesseract.exe'

提取文字

from PIL import Image
import pytesseract

def ocr_core(filename):
    text = pytesseract.image_to_string(Image.open(filename))
    return text

print(ocr_core('example.png'))  # 替换 'example.png' 为你的图片文件

方法2. 使用Google Cloud Vision API

设置Google Cloud项目

  • 前往Google Cloud Console并创建一个项目。
  • 启用Vision API。
  • 创建一个服务账户并下载其JSON密钥文件。

安装依赖

你需要安装Google Cloud Vision的Python客户端库:

pip install google-cloud-vision

提取文字

from google.cloud import vision_v1 as vision
from google.cloud.vision_v1 import types

def ocr_with_google_cloud(path):
    client = vision.ImageAnnotatorClient()

    with open(path, 'rb') as image_file:
        content = image_file.read()

    image = types.Image(content=content)

    response = client.document_text_detection(image=image)
    texts = response.text_annotations
    print('Texts:')

    for text in texts:
        print('\n"{}"'.format(text.description))

        vertices = (["({},{})".format(vertex.x, vertex.y)
                    for vertex in text.bounding_poly.vertices])
        print("bounds: {}".format(",".join(vertices)))

ocr_with_google_cloud('example.png')  # 替换 'example.png' 为你的图片文件

方法3. 使用EasyOCR库

EasyOCR 是一个简单而强大的 OCR 引擎,它支持超过 70 种语言和多种脚本,包括拉丁字母、中文、阿拉伯文、日文、韩文等。以下是如何在 Python 中使用 EasyOCR 库来从图片中提取文字的具体方法:

安装 EasyOCR

首先,你需要安装 EasyOCR。你可以通过 pip 来安装:

pip install easyocr

使用 EasyOCR 提取图片中的文字

以下是一个简单的示例,展示了如何使用 EasyOCR 从图片中提取文字:

import easyocr

def ocr_with_easyocr(image_path, lang_list=['ch_sim', 'en']):
    """
    使用 EasyOCR 从图片中提取文字

    参数:
    image_path (str): 图片路径
    lang_list (list): 要识别的语言列表,默认为简体中文 ('ch_sim') 和英文 ('en')

    返回:
    list: 包含一个或多个 dict 的列表,每个 dict 代表一行文字及其位置信息
    """
    reader = easyocr.Reader(lang_list=lang_list)  # 创建一个 Reader 对象,指定要识别的语言
    result = reader.readtext(image_path)  # 从图片中提取文字
    return result

# 示例使用
results = ocr_with_easyocr('example.png')  # 替换 'example.png' 为你的图片文件
for result in results:
    print(f"文本: {result['text']}, 位置: {result['bbox']}")

在上面的示例中,ocr_with_easyocr 函数接受一个图片路径和一个语言列表作为参数。它使用 easyocr.Reader 创建一个 Reader 对象,并指定要识别的语言。然后,它调用 readtext 方法从图片中提取文字,并返回一个包含识别结果的列表。每个结果都是一个字典,包含识别到的文本和其在图片中的位置信息(以边界框的形式给出)。