使用python进行图像文字识别(成功)

使用EasyOCR进行图像文字识别

在这篇博文中,我们将介绍如何使用EasyOCR库进行图像文字识别。EasyOCR是一个基于PyTorch的光学字符识别库,支持多种语言和文字。我们将使用Python编程语言和OpenCV库来实现图像文字识别的功能。

首先,我们需要导入所需的库,包括easyocr、matplotlib和cv2。然后,我们创建一个EasyOCR读取器,指定要识别的语言。接下来,我们读取一张包含文字的图像,并使用EasyOCR进行文字识别。最后,我们将打印识别结果,并在图像上绘制识别出的文字和边界框。

# 导入所需的库
import easyocr
import matplotlib.pyplot as plt
import cv2

# 创建 EasyOCR 读取器
reader = easyocr.Reader(['ch_sim', 'en'])

# 读取图像
img = cv2.imread(r'C:\Users\Administrator\Desktop\aa\1.jpg')

# 进行文字识别
result = reader.readtext(r'C:\Users\Administrator\Desktop\aa\1.jpg')

# 打印识别结果
for detection in result:
    top_left = tuple(detection[0][0])
    bottom_right = tuple(detection[0][2])
    text = detection[1]
    confidence = detection[2]
    print(f'Text: {text}, Confidence: {confidence}')

    # 在图像上绘制边界框和识别的文字
    img = cv2.rectangle(img, top_left, bottom_right, (0, 255, 0), 5)
    img = cv2.putText(img, text, top_left, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 2, cv2.LINE_AA)

你可能感兴趣的:(python,开发语言,文字识别)