所以我终于找到了解决这种情况的方法。当图像仅包含 1 或 2 个长度的字符串(例如“1”或“25”)时,tesseract-OCR 给出空字符串的情况。
为了在这种情况下获得输出,我在原始图像上多次附加了相同的图像,以使其长度大于 2。例如,如果原始图像仅包含“3”,我附加了“3”图像(相同的图像)4 次以上,从而使其成为包含文本“33333”的图像。然后,我们将此图像提供给 tesseract,它给出输出“33333”(大多数情况下)。然后我们只需将 Tesseract 输出的文本中的空格替换为空格,并将生成的字符串长度除以 5 即可得到索引我们希望从整个文本中输出文本。
请参阅代码以供参考,希望对您有所帮助:
import pytesseract ## pip3 install pytesseract
如果我们从 tesseract 输出中获得空字符串,该方法调用 tesseract 进行 OCR 或调用我们的解决方法代码。
def textFromTesseractOCR(croppedImage):
text = pytesseract.image_to_string(croppedImage)
if text.strip() == '': ### program that handles our problem
if 0 not in croppedImage:
return ""
yDir = 3
xDir = 3
iterations = 4
img = generate_blocks_dilation(croppedImage, yDir, xDir, iterations)
## we dilation to get only the text portion of the image and not the whole image
kernelH = np.ones((1,5),np.uint8)
kernelV = np.ones((5,1),np.uint8)
img = cv2.dilate(img,kernelH,iterations = 1)
img = cv2.dilate(img,kernelV,iterations = 1)
image = cropOutMyImg(img, croppedImage)
concateImg = np.concatenate((image, image), axis = 1)
concateImg = np.concatenate((concateImg, image), axis = 1)
concateImg = np.concatenate((concateImg, image), axis = 1)
concateImg = np.concatenate((concateImg, image), axis = 1)
textA = pytesseract.image_to_string(concateImg)
textA = textA.strip()
textA = textA.replace(" ","")
textA = textA[0:int(len(textA)/5)]
return textA
return text
膨胀方法。该方法仅用于对图像的文本区域进行膨胀
def generate_blocks_dilation(img, yDir, xDir, iterations):
kernel = np.ones((yDir,xDir),np.uint8)
ret,img = cv2.threshold(img, 0, 1, cv2.THRESH_BINARY_INV)
return cv2.dilate(img,kernel,iterations = iterations)
裁剪图片放大部分的方法
def cropOutMyImg(gray, OrigImg):
mask = np.zeros(gray.shape,np.uint8) # mask image the final image without small pieces
_ , contours, hierarchy = cv2.findContours(gray,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
if cv2.contourArea(cnt)!=0:
cv2.drawContours(mask,[cnt],0,255,-1) # the [] around cnt and 3rd argument 0 mean only the particular contour is drawn
# Build a ROI to crop the QR
x,y,w,h = cv2.boundingRect(cnt)
roi=mask[y:y+h,x:x+w]
# crop the original QR based on the ROI
QR_crop = OrigImg[y:y+h,x:x+w]
# use cropped mask image (roi) to get rid of all small pieces
QR_final = QR_crop * (roi/255)
return QR_final