PyTesseract 无法识别绿色背景上的文本

PyTesseract unable to recognize text on a green background

提问人:skullx 提问时间:11/17/2023 最后编辑:Ynjxsjmhskullx 更新时间:11/19/2023 访问量:83

问:

我在预处理时一直在玩弄图像,但 tesseract 无法检测到 LCD 屏幕上的文本。它确实在它周围创建了一个边界框,我猜这意味着它在那里找到了一些东西,但没有给出任何文本作为输出。

我正在处理的图像:

1

这是我的代码:

import cv2
import pytesseract
import numpy as np

img = cv2.imread("test-python2.jpg")

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

ret, thresh1 = cv2.threshold(gray, 50, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)

rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))
kernel = np.ones((5, 5), np.uint8)
#closin = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel)

dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)

contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
im2 = img.copy()
for cnt in contours:
    x, y, w, h = cv2.boundingRect(cnt)
    im2 = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
    
    cropped = img[y:y + h, x:x + w]
    text = pytesseract.image_to_string(cropped)
    im2 = cv2.putText(im2, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 255, 0), 3)
    text2 = text.encode('latin-1', 'replace').decode('latin-1')
    print (text2)


cv2.imshow("", im2)
cv2.waitKey(0)

我从中得到的输出是这样的:cv2.imshow()

2

我得到的文本足够准确,但它只是没有从 LCD 屏幕上读取。我尝试过使用不同类型的二值化/阈值,但 LCD 似乎从未被拾取。LCD的识别对我的项目非常重要,但我已经在这个障碍上停留了一段时间。

python opencv ocr python-tesseract

评论

0赞 Paul H 11/17/2023
如果将图像裁剪为仅包含 LCD 屏幕会发生什么?
0赞 fmw42 11/17/2023
请出示您的门槛图片?通常这就是问题所在。也许在 A 或 B 通道上转换为 LAB 和阈值。
0赞 skullx 11/18/2023
@PaulH我尝试只通过屏幕,但它没有显示输出。
0赞 skullx 11/18/2023
@fmw42我尝试进一步更改图像。这是转到 pytesseract 链接的图像。我唯一的输出是一行空行。

答:

1赞 Hermann12 11/17/2023 #1

我删掉了中间部分:

import subprocess
import cv2
import pytesseract

# Image manipulation
# Commands https://imagemagick.org/script/convert.php
mag_img = r'D:\Programme\ImageMagic\magick.exe'
con_bw = r"D:\Programme\ImageMagic\convert.exe"

in_file = r'green_red.jpg'
out_file = r'green_bw.jpg'

# Play with black and white and contrast for better results
process = subprocess.run([con_bw, in_file, "-resize", "100%","-threshold","28%", out_file])

# Text ptocessing
pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files\Tesseract-OCR\tesseract.exe'
img = cv2.imread(out_file)

# Parameters see tesseract doc 
custom_config = r'--psm 6 --oem 3 -c tessedit_char_whitelist=ABCDEFTGHIJKLMNOPQRSTUVWXYZkh' 

tex = pytesseract.image_to_string(img, config=custom_config)
print(tex)

with open("number.txt", 'w') as f:
    f.writelines(tex)

cv2.imshow('image',img)
cv2.waitKey(12000)
cv2.destroyAllWindows()

输出:

CTEP

图像:enter image description here enter image description here

选项 2 使用 CV2 而不是 imagemagick。我裁剪了交互区域并将其设置为 blach&white,然后对文本进行 ocr 处理:

import cv2
import numpy as np
import pytesseract
 
img = cv2.imread('green.jpg',cv2.IMREAD_UNCHANGED)
print(img.shape) # Print image shape
cv2.imshow("Original", img)
 
# Cropping an image and set color to B&W
cropped_image = img[370:600, 603:1360] # img[y:y+h, x:x+w]
grayImage = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2GRAY)
(thresh, blackAndWhiteImage) = cv2.threshold(grayImage, 65, 255, cv2.THRESH_BINARY) # | cv2.THRESH_OTSU => 90
print(thresh)

# resize image
scale_percent = 43 # 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=ABCDEFTGHIJKLMNOPQRSTUVWXYZkh' 
tex = pytesseract.image_to_string(resized, config=custom_config)
print(tex)

# Display cropped image
cv2.imshow("cropped", resized)
 
# Save the cropped image
cv2.imwrite("Cropped Image.jpg", resized)
 
cv2.waitKey(0)
cv2.destroyAllWindows()

输出:

(1080, 1920, 3)  # image size
65.0             # thresh value
CTEP             # -> OCR

评论

0赞 skullx 11/18/2023
感谢您的回复,我没有安装ImageMagick并尝试使用ImageMagick来更改图像的亮度和对比度。我得到的输出图像看起来像这样。你能展示一下out_file是什么样子的吗?cv2.convertScaleAbs(image, alpha=2.8, beta=5)
0赞 Hermann12 11/19/2023
@skulix,更新了我的答案并添加了来自 Image Magic 的结果图像。你现在也找到了一个 cv2 解决方案,在我上面的回答中。