我一直在尝试从该图像中提取粗体白色文本,但无法使其正常工作,似乎 9 被读作 3,而 I 被读作 1。
一直在查看各种网站,这些网站都有代码可以提高图像质量,但无法使其正常工作,任何人都可以帮助我解决这个问题吗?所需的输出应该是“I6M-9U”
def get_text_from_image(image: cv2.Mat) -> str:
pytesseract.pytesseract.tesseract_cmd = r'C:\Tesseract-OCR\tesseract.exe'
# Crop image to only get the piece I am interested in
top, left, height, width = 25, 170, 40, 250
try:
crop_img = image[top:top + height, left:left + width]
# Make it bigger
resize_scaling = 1500
resize_width = int(crop_img.shape[1] * resize_scaling / 100)
resize_height = int(crop_img.shape[0] * resize_scaling / 100)
resized_dimensions = (resize_width, resize_height)
# Resize it
crop_img = cv2.resize(crop_img, resized_dimensions, interpolation=cv2.INTER_CUBIC)
return str(pytesseract.image_to_string(crop_img, config="--psm 6"))
更新代码
ret, thresh1 = cv.threshold(image, 120, 255, cv.THRESH_BINARY +
cv.THRESH_OTSU)
cv.imshow("image", thresh1)
现在已删除所有背景伪影,但现在将第一个字母 I 读取为 1,将 9 读取为 3
对于我来说,I 看起来像是 1。如果您不喜欢 1,请将其从过滤器中删除:
import cv2
import pytesseract
img = cv2.imread('16M.png',cv2.IMREAD_UNCHANGED)
(thresh, blackAndWhiteImage) = cv2.threshold(img, 63, 255, cv2.THRESH_BINARY)
# resize image
scale_percent = 4 # 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'
# Remove 1 from filter, than you will get I instead
custom_config = r'--psm 6 --oem 3 -c tessedit_char_whitelist=-.ABCDEFTGHIJKLMNOPQRSTUVWXYZ0123456789'
tex = pytesseract.image_to_string(resized, config=custom_config)
print(tex)
# Display cropped image
cv2.imshow("Image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
16M-9U-0.0
从过滤器中删除“1”:
I6M-9U-0.0