0

I am still learning to code in python and I am struggling to code object-oriented way. I have written a code using pytessarct library and with the extracted words I have tried to make a simple detector by using keywords as a filter. I want to redo it in class and object format strictly as a learning exercise. Will be highly grateful if anybody can extend any help. Thank you

import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' 
import os
import cv2
from PIL import Image
import matplotlib.pyplot as plt
from PIL import ImageFilter
import re 
img_path = 'C:/Users/RAJ/realtest/'

for i in os.listdir(img_path):
  images=Image.open(img_path+'//'+i)
  plt.imshow(images)
  plt.show()
  images_new=images.convert('LA')
  im_SHARPEN2 = images_new.filter(filter=ImageFilter.SHARPEN)
  extract = pytesseract.image_to_string(im_SHARPEN2, lang = 'eng')
  extract2 = pytesseract.image_to_string(images_new,lang = 'eng')
  final= extract+extract2  
  x = re.search(r"INCOME|TAX|Account|GOVT.", final,re.M|re.I)
  y = re.search(r"GOVERNMENT|DOB|Male|Female.", final,re.M|re.I)
if x == None and y== None:
    print('Not a pan card or adhaar card')
elif type(x)== re.Match:
    print('This is a pan card')
else:
    print('adhaar card detected')
3
  • This question might be a better fit for: codereview.stackexchange.com Commented Mar 6, 2021 at 18:04
  • If you want the code indentation to stay clean, copy-paste your code, select it and press ctrl+K. Commented Mar 6, 2021 at 18:05
  • As you said this was just an exercise for you, but I highly recommend to read this. A class with just the constructor and one method should be just a function. Commented Apr 25, 2022 at 21:38

1 Answer 1

1
import pytesseract
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' 
import os
import cv2
from PIL import Image
import matplotlib.pyplot as plt
from PIL import ImageFilter
import re 

class is_pan_card():
    def __init__(self,path):
        self.img_path = path
        
        for i in os.listdir(self.img_path):
            images=Image.open(self.img_path+'//'+i)
            plt.imshow(images)
            plt.show()
            images_new=images.convert('LA')
            im_SHARPEN2 = images_new.filter(filter=ImageFilter.SHARPEN)
            extract = pytesseract.image_to_string(im_SHARPEN2, lang = 'eng')
            extract2 = pytesseract.image_to_string(images_new,lang = 'eng')
            final= extract+extract2  
            x = re.search(r"INCOME|TAX|Account|GOVT.", final,re.M|re.I)
            y = re.search(r"GOVERNMENT|DOB|Male|Female.", final,re.M|re.I)
        if x == None and y== None:
            print('Not a pan card or adhaar card')
        elif type(x)== re.Match:
            print('This is a pan card')
        else:
            print('adhaar card detected')

is_pan =is_pan_card('C:/Users/RAJ/realtest/')
Sign up to request clarification or add additional context in comments.

1 Comment

This is working fine thank you so much for this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.