我在预处理时一直在处理图像,但超立方体无法检测液晶屏幕上的文本。它确实在它周围创建了一个边界框,我猜这意味着它在那里找到了一些东西,但没有给出任何文本作为输出。
这是我的代码:
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() 得到的输出是 。我得到的文本足够准确,但它只是无法从液晶屏幕上读取。我尝试过使用不同类型的二值化/阈值,但 LCD 似乎从未被采用。 LCD 的识别对我的项目非常重要,但我已经陷入这个障碍有一段时间了
我把有趣的部分剪掉了:
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