【问题标题】:Filtering the image and converting to text gives wrong output in python过滤图像并转换为文本会在 python 中产生错误的输出
【发布时间】:2022-01-23 20:30:22
【问题描述】:

我想从图像中提取特定文本,并且我已经在图像中进行了一些过滤,但仍然没有得到确切的文本。还有什么方法可以从图像中单独获取特定文本?

过滤图像并转换为文本的代码

import cv2
import pytesseract

image = cv2.imread('original.png', 0)
thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
img = cv2.adaptiveThreshold(thresh, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,   cv2.THRESH_BINARY, 11, 2)
cv2.imwrite('filtered.png', img)
data = pytesseract.image_to_data(img)
print(data)

cv2.imshow('thresh', img)
cv2.waitKey()

【问题讨论】:

  • 跳过“img = cv2.adaptiveThreshold(thresh, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)”。接下来,OCR 仅对图像的一部分进行文本处理 - IMO 这应该很容易,因为在游戏中文本应该始终位于相同的位置。

标签: python tesseract cv2


【解决方案1】:

您可以尝试 easyOCR 代替 pytesseract

pip install easyocr首次安装

import cv2
import easyocr

image = cv2.imread('original.jpg', 0)
reader = easyocr.Reader(['en'])
result = reader.readtext(image)

#You can use regular expression
interested_string = 'Patrol Rewards'

line = [l[1] for l in result if 'Patrol Rewards' in l[1]]
print(line)

您将获得包含感兴趣字符串的列表,例如

['Patrol Rewards: Courage Horn X 1']

这将提供正确的输出,但与 CPU 上的 pytesseract 相比它有点慢,但如果你配置了 GPU,那么它会更快。但它提供了相当不错的 OCR 结果。

【讨论】:

  • 谢谢。但我尝试在 PyCharm 上安装 easyocr 并在安装时显示错误
  • 你能指定错误吗?
  • 40:399:执行错误:错误:找不到满足要求的版本torchvision> = 0.5(来自easyocr)(来自版本:0.1.6、0.1.7、0.1.8, 0.1.9, 0.2.0, 0.2.1, 0.2.2, 0.2.2.post2, 0.2.2.post3) 错误:找不到匹配的分布 fortorchvision>=0.5(来自 easyocr)警告:您使用的是 pip 版本20.2.3;但是,版本 21.3.1 可用。您应该考虑通过“/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip”命令进行升级。 (1)
  • 你需要先升级你的pip
  • 我已经在 Google colab 中尝试过该代码。它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 2021-02-19
  • 2015-10-31
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多