[英]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.
我已经根据我的知识尝试但没有成功。任何帮助和指导将不胜感激。
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))
#--- 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))
Use it and let me know if you are able to find the relevant textual information! 如果您能够找到相关的文字信息,请使用它并告诉我们!
In pytesseract, lang = 'chi_sim' tries to interpret the digits also as Chinese characters. 在pytesseract中,lang ='chi_sim'试图将数字解释为中文字符。 Use lang = 'eng' to get the numbers ocr'ed properly
使用lang ='eng'来正确获取数字
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.