答案 0 :(得分:-1)
在简单的预处理可以为您提供所需结果的情况下,您无需培训tesseract。
def remove_small_dots_from_image(im):
_, black_and_white = cv2.threshold(im, 127, 255, cv2.THRESH_BINARY_INV)
n_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(black_and_white, None, None, None, 8, cv2.CV_32S)
sizes = stats[1:, -1] # get CC_STAT_AREA component
img2 = np.zeros(labels.shape, np.uint8)
for i in range(0, n_labels - 1):
if sizes[i] >= 50: # filter small dotted regions
img2[labels == i + 1] = 255
return cv2.bitwise_not(img2)
image = cv2.imread('image.png', cv2.IMREAD_GRAYSCALE)
image = remove_small_dots_from_image(image)
#add white border around the image because tesseract would fail to guess font size otherwise
image = cv2.copyMakeBorder(image, 30, 30, 30, 30, cv2.BORDER_CONSTANT, None, 255)
print(pytesseract.image_to_string(image, config='--psm 7'))
该代码为您提供所需的输出
2