Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
#!/usr/bin/env python
#Raspberry pi 3B+, kernel 5.14.82, Debian Strecth, OpenCV 4.0-pre
#Date 24th November, 2018

from PIL import Image
import pytesseract
import cv2
import numpy as np

def main():
    img = cv2.imread('id.jpg' )
    #img = cv2.resize(img, (640, 480)) 

    canny = cv2.Canny(img, 400, 600)
    gray = cv2.GaussianBlur(canny,(5,5), 0)
    inverted = cv2.bitwise_not(gray)

    test1 = Image.fromarray(gray)
    test2 = Image.fromarray(inverted)

    result = pytesseract.image_to_string(test1, lang="eng", config="-c tessedit_char_whitelist=0123456789X")
    print( result)
    print( "-------")
    result = pytesseract.image_to_string(test2, lang="eng")
    print( result)

    cv2.imwrite('inverted.jpg', inverted)
    cv2.imwrite('gray.jpg', gray)
    cv2.imshow('Gray', gray)
    cv2.imshow('Inverted', inverted)

    k = cv2.waitKey(0)

if __name__ == "__main__":
    main()

image description image description