gpt4 book ai didi

Tesseract OCR having trouble detecting numbers(Tesseract OCR在检测数字时出现问题)

转载 作者:bug小助手 更新时间:2023-10-25 17:16:30 26 4
gpt4 key购买 nike



I am trying to detect some numbers with tesseract in python. Below you will find my starting image and what I can get it down to. Here is the code I used to get it there.

我正在尝试在python中使用tesseract检测一些数字。下面你会发现我的开始图像,以及我可以把它降到什么。这是我用来把它弄到那里的代码。


import pytesseract
import cv2
import numpy as np
pytesseract.pytesseract.tesseract_cmd = "C:\\Users\\choll\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe"

image = cv2.imread(r'64normalwart.png')
lower = np.array([254, 254, 254])
upper = np.array([255, 255, 255])
image = cv2.inRange(image, lower, upper)
image = cv2.bitwise_not(image)
#Uses a language that should work with minecraft text, I have tried with and without, no luck
text = pytesseract.image_to_string(image, lang='mc')
print(text)
cv2.imwrite("Wartthreshnew.jpg", image)
cv2.imshow("Image", image)
cv2.waitKey(0)

I end up with black numbers on a white background which seems pretty good but tesseract can still not detect the numbers. I also noticed the numbers were pretty jagged but I don't know how to fix that. Does anyone have recommendations for how I could make tesseract be able to recognize these numbers?

我最终得到了白色背景上的黑色数字,这看起来很好,但tesseract仍然无法检测到数字。我也注意到这些数字相当参差不齐,但我不知道如何解决这个问题。有没有人建议我如何让tesseract能够识别这些数字?


Starting Image

启动图像


What I end up with

我最终得到的是什么


更多回答

You could try cv2.blur() to smooth the rough edges of the numbers. It will make the image fuzzier overall but tesseract might have an easier time recognizing digits.

您可以尝试使用cv2.blur()来平滑数字的粗略边缘。这将使图像整体上更加模糊,但tesseract可能会更容易识别数字。

Thanks for the suggestion, the image might be too small but it still cant see it.

谢谢你的建议,图像可能太小了,但还是看不见。

Try to add config psm 6 or 7 like this: pytesseract.image_to_string(img, config='--psm 6')

尝试像这样添加配置PSM 6或7:pytesseract.Image_to_string(img,config=‘--PSM 6’)

Good idea. The solution I found was to use --psm 8 and treat it as a word along with limiting it to numbers. stackoverflow.com/questions/44619077/… Was a useful resource for anyone in the future who sees this.

好主意.我找到的解决方案是使用--psm 8并将其视为一个单词,同时将其限制为数字。stackoverflow.com/questions/44619077/.对于将来看到这一点的人来说是一个有用的资源。

优秀答案推荐

Your problem is with the page segmentation mode. Tesseract segments every image in a different way. When you don't choose an appropriate PSM, it goes for mode 3, which is automatic and might not be suitable for your case. I've just tried your image and it works perfectly with PSM 6.

您的问题出在页面分割模式上。Tesseract以不同的方式对每个图像进行分割。如果您没有选择合适的PSM,则会选择模式3,该模式是自动的,可能不适合您的情况。我刚刚试过你的图像,它和PSM 6完美地搭配在一起。


df = pytesseract.image_to_string(np.array(image),lang='eng', config='--psm 6')

These are all PSMs availabe at this moment:

这些都是目前可用的PSM:


  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.


Use the pytesseract.image_to_string(img, config='--psm 8') or try diffrent configs to see if the image will get recognized. Useful link here Pytesseract OCR multiple config options

使用pytesseract.IMAGE_TO_STRING(img,CONFIG=‘--PSM 8’)或尝试不同的配置,以查看是否可以识别映像。此处提供了有用的链接,其中包含多个配置选项



I think tesseract is blacklisted numbers by default, so i tried tessedit_char_whitelist to whitelist the characters i want but it didn't work, so i tried to un-blacklist the numbers using this config tessedit_char_unblacklist='0123456789'

我认为tesseract默认情况下是被列入黑名单的数字,所以我尝试tessedit_char_White elist将我想要的字符列入白名单,但它不起作用,所以我尝试使用此配置将数字取消黑名单。tessedit_char_unBlacklist=‘0123456789’


pytesseract.image_to_string(img, lang='eng', config='--psm 6 --oem 3 -c tessedit_char_unblacklist=0123456789')

更多回答

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

请记住,Stack Overflow不仅仅是为了解决眼前的问题,也是为了帮助未来的读者找到类似问题的解决方案,这需要理解底层代码。这对于我们社区的初学者和不熟悉语法的成员来说尤其重要。考虑到这一点,你能编辑你的答案,包括你正在做的事情的解释以及为什么你认为这是最好的方法吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com