我正在将pytesseract(0.3.2)与openCV(4.1.2)配合使用以标识图像中的数字。当image_to_string工作时,image_to_data和image_to_boxes无效。我需要能够在图像上绘制边界框,这使我很困惑。我尝试了不同的图像,旧版本的pytesseract等。我正在使用Windows和Jupyter笔记本。
import cv2
import pytesseract
#erosion
def erode(image):
kernel = np.ones((5,5),np.uint8)
return cv2.erode(image, kernel, iterations = 1)
#grayscale
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#thresholding
def thresholding(image):
#return cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
return cv2.threshold(image, 200, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
img = cv2.imread('my_image.jpg')
pytesseract.pytesseract.tesseract_cmd = r'C:\mypath\tesseract.exe'
gray = get_grayscale(img)
thresh = thresholding(gray)
erode = remove_noise(thresh)
custom_config = r'-c tessedit_char_whitelist=0123456789 --psm 6'
print(pytesseract.image_to_string(erode, config=custom_config))
cv2.imwrite("test.jpg", erode)
#these return nothing
print(pytesseract.image_to_boxes(Image.open('test.jpg')))
print(pytesseract.image_to_data(Image.open('test.jpg')))
代替使用image_to_boxes
,另一种方法是简单地使用cv2.findContours
查找轮廓,使用cv2.findContours
获得边界矩形坐标,并使用cv2.boundingRect
绘制边界框>
使用此示例输入图像
cv2.boundingRect
抽屉框
OCR的结果
cv2.rectangle
代码