title: how to extract data from ID card using python
body: <p>I have a below image and I am extracting the text from the image.The below image is a template image not a genuine Id.</p>
  <p><a href="https://i.stack.imgur.com/SfuFl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SfuFl.jpg" alt="enter image description here" /></a></p>
  <p>I am using the below code to extract the text from the image:</p>
  <pre><code>def data_extraction_with_cleaning(path,file_name,threshold,preprocess_resize,filtering):
      &quot;&quot;&quot;This function will do data extraction along with image preprocessing&quot;&quot;&quot;
      image = cv2.imread(path+file_name)
      gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
      
      # check to see if we should apply thresholding to preprocess the
      # image
      if threshold == &quot;thresh&quot;:
          gray = cv2.threshold(gray, 0, 255,
                               cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
      
      elif threshold == &quot;adaptive&quot;:
          gray = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
     
      if preprocess_resize == &quot;linear&quot;:
          gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
      
      elif preprocess_resize == &quot;cubic&quot;:
          gray = cv2.resize(gray, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)
      
      # make a check to see if blurring should be done to remove noise, first is default median blurring
      
      
      if filtering == &quot;blur&quot;:
          gray = cv2.medianBlur(gray, 3)
      
      elif filtering == &quot;bilateral&quot;:
          gray = cv2.bilateralFilter(gray, 9, 75, 75)
      
      elif filtering == &quot;gauss&quot;:
          gray = cv2.GaussianBlur(gray, (5,5), 0)
      else:
          pass
      
      # write the grayscale image to disk as a temporary file so we can
      # apply OCR to it
      filename = &quot;{}.png&quot;.format(os.getpid())
      cv2.imwrite(filename, gray)
      
  
      # load the image as a PIL/Pillow image, apply OCR, and then delete
      # the temporary file
      pytesseract.pytesseract.tesseract_cmd = r'C:\Users\user\AppData\Local\Tesseract-OCR\tesseract.exe'
      text = pytesseract.image_to_string(Image.open(filename), lang = 'eng')
      text = re.sub(r'[^\da-zA-Z0-9_() \n]+', '', text)
      # text = text.replace('\n', ' ')
      
      # add +hin after eng within the same argument to extract hindi specific text - change encoding to utf-8 while writing
      os.remove(filename)
      # print(text)
      
      # show the output images
      # cv2.imshow(&quot;Image&quot;, image)
      # cv2.imshow(&quot;Output&quot;, gray)
      # cv2.waitKey(0)
      
      # writing extracted data into a text file
      text_output = open('outputbase1.txt', 'w', encoding='utf-8')
      text_output.write(text)
      text_output.close()
      
      file = open('outputbase1.txt', 'r', encoding='utf-8')
      text = file.read()
      # print(text)
      
      # Cleaning all the gibberish text
      text = ftfy.fix_text(text)
      text = ftfy.fix_encoding(text)
      return text
  
  text_passport_can = data_extraction_with_cleaning(path,file_name,'thresh','linear','blur')
  </code></pre>
  <p>The output text is coming as junk.Below is my Output file.</p>
  <pre><code>Qo Authority Awdurdod Ugndarras Instanuey f Aulotay
  Myxieghed Udaras Exbou00 Apxt Autondades Ait
  Vieanomaineny Myndighel Pus vydall Valpanigal tedgvegagtace Tadaves
  Awtorta Organ Whadry Organ Urad Kisaits hatgag 
   dwte of expiry Dydidiad dod i ben Bileachdach gu De gekigheldichar
  van dt paspuor eindigt op Date dexpiration Gidtig bis Data di
  Kadenza Cykiigt indi As feldhrry Muepounvic Migs Vilido ad Este
  pasaporie explra el Varmeinen vounmsssolopiva Gaugt La tn Plytnost
  Joy Kehtiy kuni Deriquma teeming Gahop iki Data ta Ightetug Date
  Wygaineeda Waznoicy Yel do Odtum platnostif Lefirau 35
  
    
  
   PEGBRALEXRGORDIEV 6   6 6 CK CK KKK KK KEK KEE
  
  x
  
    
  
  1071926378GBR8412036M1 80628702
  </code></pre>
  <p>How to deal with such case.I know about one package <code>PassportEye</code> which deals with Passport data extraction.But I am looking for data extraction with <code>OpenCv</code> or <code>Pytesseract</code> as i have other ID's to extract text from.</p>