Search code examples
pythonpython-3.xocrtesseractpython-tesseract

Tesseract ocr output with single characters in between the detected text


I am trying to use Tesseract to extract from the below image,

enter image description here

text = pytesseract.image_to_string(image, config='-c preserve_interword_spaces=1 --psm 1 --oem 1')

Here is the result from tesseract 4 ocr,

print(text)

Wrote Datastream application 
e Used Kafka to get the accounts

If you see the bullet point in the image is converted to e, I found several such points in document converted into single characters in ascii

If anyone is familiar with such issue and have a solution please let me know.


Solution

  • I have a suggestion, maybe its better to remove the bullet-points.

    • One solution for removing the bullet-point is applying adaptive-threshold

    • If we apply adaptive-threshold to the current image:

      • enter image description here
    • Now if we read it:

      • Wrote Datastream application |
        Used Kafka to get the accounts
        
        

    Code:


    import cv2
    import pytesseract
    
    img = cv2.imread("4XMue.png")
    gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                                cv2.THRESH_BINARY, 11, 131)
    txt = pytesseract.image_to_string(thr)
    print(txt)
    

    Please let me tell you that my example code, may not work for every image. Since an image may have different artifacts or require additional processing. You may need to change the block-size and C parameters of the adaptive-threshold. Therefore, please start with reading the adaptive-threshold