Extract text from image using OCR in python

I want to extract text from a specific area of the image like the name and ID number from identity card. The ID card from which I want to extract text is in the Chinese language(Chinese ID card). I have tried this code but it just extracts the address and date of birth which I don't need. I just need the name and ID number.

import cv2
from PIL import Image
import pytesseract
import argparse
import os

image = cv2.imread("E:/face.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename,gray)

text = pytesseract.image_to_string(Image.open(filename), lang='chi_sim')
print(text)
os.remove(filename)

I have also attached the image from which I am trying to extract text. I have tried according to my knowledge but not succeeded.any help and guidance would be appreciated.enter image description here

This is the binary image

1 answer

  • answered 2018-07-11 07:47 Jeru Luke

    I can suggest a pre-processing step prior to finding textual information. The code is simple to comprehend.

    Code:

    image = cv2.imread(r'C:\Users\Jackson\Desktop\face.jpg')
    
    #--- dilation on the green channel ---
    dilated_img = cv2.dilate(image[:,:,1], np.ones((7, 7), np.uint8))
    bg_img = cv2.medianBlur(dilated_img, 21)
    
    #--- finding absolute difference to preserve edges ---
    diff_img = 255 - cv2.absdiff(image[:,:,1], bg_img)
    
    #--- normalizing between 0 to 255 ---
    norm_img = cv2.normalize(diff_img, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
    cv2.imshow('norm_img', cv2.resize(norm_img, (0, 0), fx = 0.5, fy = 0.5))
    

    enter image description here

    #--- Otsu threshold ---
    th = cv2.threshold(norm_img, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
    cv2.imshow('th', cv2.resize(th, (0, 0), fx = 0.5, fy = 0.5))
    

    enter image description here

    Use it and let me know if you are able to find the relevant textual information!