tesseract 提取无意义单词时不准确

问题描述 投票:0回答:1

我无法从图像中准确地提取文本

import cv2
import pytesseract

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

image_path = 'crop.png'
img = cv2.imread(image_path)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply more aggressive thresholding and additional morphology operations
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

custom_config = r'--oem 1 --psm 6 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ'

text = pytesseract.image_to_string(thresh, config=custom_config)

print(text)

这是我的代码,我得到可靠的输出 在这里查看

https://colab.research.google.com/drive/11utvWD3s6DqqGZQEnk5cKIAj46ZLsF5y?usp=sharing

python opencv ocr tesseract python-tesseract
1个回答
0
投票

如果我使用黑白图像,大小调整为 43%,我会得到正确的输出:

import cv2
import pytesseract
 
img = cv2.imread('crop.png',cv2.IMREAD_UNCHANGED)
print(img.shape) # Print image shape
# cv2.imshow("Original", img)

grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
# (thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 150, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 65, 255, cv2.THRESH_BINARY) 
print(thresh)

# resize image
scale_percent = 30 # percent of original size
width = int(blackAndWhiteImage.shape[1] * scale_percent / 100)
height = int(blackAndWhiteImage.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(blackAndWhiteImage, dim, interpolation = cv2.INTER_AREA)

# OCR resized Black & White image
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
custom_config = r'--psm 6 --oem 3 -c tessedit_char_whitelist=ABCDEFTGHIJKLMNOPQRSTUVWXYZ' 
tex = pytesseract.image_to_string(resized, config=custom_config)
print(tex)

### Display resized image
cv2.imshow("resized", resized)
 
# Save the resized image
cv2.imwrite("resized_crop.png", resized)

cv2.waitKey(0)
cv2.destroyAllWindows()
###

输出:

TLYTILAER
NSNOPMARC
AFSENNACT
DHOMEDZEW
EMSINODEH
PKELIZAWS
CCJIXCZTN
AINTENDKZ
DETROBAPB
© www.soinside.com 2019 - 2024. All rights reserved.