Search code examples
deep-learningcomputer-visiontesseractopencvpython

Unrelated output by pytesseract image_to_string function


I'm trying to extract text from an image but pytesseract is giving a totally different output, the image attached below output is "Werle" (complete different word and characters), I tried many different processing methods like image enhancement, rgb2gray, rgb2binary, still didn't work. What confuses me that the text in the image is very clear and straightforward. I also tried to change the notebook from google colab to my local notebook and checked the library version, but same incorrect result.

enter image description here

output >> "Werle"

here's my code:-

ret,frame = cap.read()
crop_img = frame[320:400,430:840]
text = pt.image_to_string(crop_img)

note: this problem happens with other images with the same style but different word


Solution

  • It turned out that Pytesseract is trained on data with a white background and black text, so what I did turn black pixels to white and white pixels to black

        crop_img = frame[320:400,430:840]
    
        lower_black = np.array([0,0,0], dtype = "uint16")
        upper_black = np.array([200,200,200], dtype = "uint16")
        crop_img = cv2.inRange(crop_img, lower_black, upper_black)
    
        text = pt.image_to_string(image=crop_img)
    

    and it worked properly with this preprocessing.