我使用 Pytesseract 和 openCV 从图像中读取文本。我使用中值模糊、归一化和阈值来去除背景并能够阅读文本。
但是,在标准化过程中,文本的某些部分变得太亮,我希望将它们变暗,以便它们与图像中剩余文本的暗度/强度相匹配。我尝试了形态变换,并尝试了 canny+erosion 来消除噪音,但这些都没有帮助。
我的输入如下所示:
在这里,“代码”,“部门名称”,“15”和“机械”较轻,我无法阅读,而我可以轻松阅读“空气分配”和“基本材料和方法”。
有关如何更改较浅文本颜色的任何帮助都会非常有帮助。
I am trying to run the following code:
import cv2
import pytesseract
img = cv2.imread('/Users/user1/Desktop/folder1/pdf1.pdf')
text = pytesseract.image_to_string(img)
print(text)
Run Code Online (Sandbox Code Playgroud)
which gives me the following error:
Traceback (most recent call last):
File "/Users/user1/PycharmProjects/project1/python_file.py", line 5, in <module>
text = pytesseract.image_to_string(img)
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 346, in image_to_string
return {
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 349, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 249, in run_and_get_output
with save(image) as (temp_name, input_filename):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/contextlib.py", line 113, in __enter__
return next(self.gen)
File "/Users/user1/PycharmProjects/project1/venv/lib/python3.8/site-packages/pytesseract/pytesseract.py", line 172, in …Run Code Online (Sandbox Code Playgroud) 我想用 Pytesseract 自动解决像这样的验证码(所有验证码都是红色背景和白色字母)
我一直在尝试处理图像以使 Pytesseract 能够读取它,但没有成功。很高兴收到您处理此图像的想法。这是我的代码:
import cv2
import pytesseract
tessdata_dir_config = '--tessdata-dir "C:\\Program Files\\Tesseract-OCR\\tessdata"'
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
img = cv2.imread("captcha.png")
img = cv2.resize(img, None, fx=2, fy=2)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
adaptive = cv2.adaptiveThreshold(
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 85, 20)
print((pytesseract.image_to_string(img, config=tessdata_dir_config)).strip())
print((pytesseract.image_to_string(gray, config=tessdata_dir_config)).strip())
print((pytesseract.image_to_string(adaptive, config=tessdata_dir_config)).strip())
cv2.imshow("Captcha", img) # Output: IMQW
cv2.imshow("Gray", gray) # Output: IMOW
cv2.imshow("Adaptive", adaptive) # Output: IMOW,
cv2.waitKey(7000)Run Code Online (Sandbox Code Playgroud)
我是图像处理新手。我需要将图像传递给 pytesseract 来获取图像的内容。在此之前,我需要以图像的所有字符与图像底部对齐的方式预处理图像,而 pytesseract 可以轻松检测到这些字符。
我使用 opencv-python,4.5.5 和 Python 3.8
我正在处理的图像看起来像-
更新: 我已经尝试过使用下面提到的代码:
import cv2
import numpy as np
img = cv2.imread(r"dialated.jpg", cv2.IMREAD_GRAYSCALE)
ret, img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY_INV)
Contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
Contours = sorted(Contours, key=lambda x : cv2.boundingRect(x)[0])
#Contours.sort(key=lambda x : cv2.boundingRect(x)[0]) #throws exception so commented out and used the above line instead.
newImg = np.zeros(img.shape, dtype=np.uint8)
bb = cv2.boundingRect(Contours[0])
newY = (bb[1] + bb[3])
for Contour in Contours:
[x, y, w, h] = cv2.boundingRect(Contour)
newImg[newY-h+1:newY+1, …Run Code Online (Sandbox Code Playgroud)