【问题标题】:How do you convert an image into a number in python using pytesseract如何使用 pytesseract 在 python 中将图像转换为数字
【发布时间】:2020-04-20 05:48:12
【问题描述】:

我一直在尝试使用 pytesseract 将图像转换为字符串/整数。唯一的问题是每次我运行代码时都没有任何反应。我将图像更改为读取“TEXT”的文本图像,pytesseract 检测到它很好。这是我用来将图像转换为字符串的方法。我还包括了我一直在使用的图像。

bal = pytesseract.image_to_string(balIm) print(bal)

我不知道还能尝试什么我能想到的唯一其他方法就是尝试另一个 OCR,任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: python image ocr tesseract python-tesseract


    【解决方案1】:

    尝试将页面分割模式 (PSM) 设置为模式 6,这会将 OCR 设置为检测单个统一的文本块。

    具体来说,这样做:

    bal = pytesseract.image_to_string(balIm, config='--psm 6')
    

    这应该可以满足您的需求。事实上,我尝试在你的图像上运行它,它给了我想要的东西。请注意,我首先下载了您在上面提供的图像,并在我的本地计算机上离线读取了图像:

    In [8]: import pytesseract
    
    In [9]: from PIL import Image
    
    In [10]: balIm = Image.open('wC62s.png')
    
    In [11]: pytesseract.image_to_string(balIm, config='--psm 6')
    Out[11]: '0.03,'
    

    作为最后一点,如果您发现 Tesseract 开箱即用并不适合您,请考虑尝试其中一种页面分割模式来帮助提高准确性:https://tesseract-ocr.github.io/tessdoc/ImproveQuality#page-segmentation-method。为了完整起见,我将在下面提供给您。

      0    Orientation and script detection (OSD) only.
      1    Automatic page segmentation with OSD.
      2    Automatic page segmentation, but no OSD, or OCR.
      3    Fully automatic page segmentation, but no OSD. (Default)
      4    Assume a single column of text of variable sizes.
      5    Assume a single uniform block of vertically aligned text.
      6    Assume a single uniform block of text.
      7    Treat the image as a single text line.
      8    Treat the image as a single word.
      9    Treat the image as a single word in a circle.
     10    Treat the image as a single character.
     11    Sparse text. Find as much text as possible in no particular order.
     12    Sparse text with OSD.
     13    Raw line. Treat the image as a single text line,
           bypassing hacks that are Tesseract-specific.
    

    当您运行image_to_string 时,指定一个输入参数config,它接受您要在其中操作的 PSM。尝试其中的一些,直到您让它适用于您的图像。确保在执行之前在config 参数中使用--psm

    【讨论】:

    • 非常感谢,它第一次尝试有效,甚至在我将其应用于另一个应用程序时也有效。
    • @chessyChad1999 不客气 - 如果您不再需要帮助并且您的问题已得到解答,请考虑接受此答案,让社区知道您已完成此问题。 meta.stackexchange.com/questions/5234/…
    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多