【问题标题】:Converting image identified by PyTesseract to an array将 PyTesseract 识别的图像转换为数组
【发布时间】:2020-07-09 18:26:18
【问题描述】:

我有一张带有数字列表的图像,我使用 PyTesseract 扫描了这些数字以构造一个字符串。具体来说,代码如下:

from PIL import Image
import pytesseract
from scipy import stats
import numpy as np

pytesseract.pytesseract.tesseract_cmd = r'C:\\\Program Files\\\Tesseract-OCR\\\tesseract.exe'

str1=pytesseract.image_to_string(Image.open('D:/Image.png'))

这是我正在扫描的图像:

问题在于 PyTesseract 将图像扫描为单个字符而不是整数。

我想了解为什么会发生这种情况以及我可以做些什么来获得预期的结果。

简而言之,PyTesseract 不会扫描数字列表中的整数,而是将它们作为单个字符进行扫描。我如何告诉它扫描整数并将它们放入数组中?

【问题讨论】:

  • 你的意思是它会返回一个字符串而不是一个数字列表?
  • 它返回一个字符串。我需要一串数字。
  • 什么?所以您想将其转换为list?I need a string of numbers. 您可以编辑您的帖子并将您的期望输出添加到您的帖子中吗?
  • 一个数字列表就可以了。好的,让我编辑我想要的内容。

标签: arrays python-3.x image-processing python-tesseract


【解决方案1】:

好吧,如果你只想得到一个列表,使用re.splitstrip可以解决。(因为tesseract的结果有一些错误)。

你可以试试这个:

import pytesseract
import re

data = pytesseract.image_to_string('OCR.png')
dataList = re.split(r',|\.| ',data) # split the string
resultList = [int(i.strip()) for i in dataList if i != ''] # remove the '' str and convert str to int.
print(resultList)

# result: [71, 194, 38, 1701, 89, 76, 11, 83, 1629, 48, 94, 63, 132, 16, 111, 95, 84, 341, 975, 14, 40, 64, .......

【讨论】:

  • 漂亮,效果很好!那个拆分命令让我逃脱了......
猜你喜欢
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多