简体   繁体   English

使用python中的OCR从图像中提取文本

[英]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中,lan​​g ='chi_sim'试图将数字解释为中文字符。 Use lang = 'eng' to get the numbers ocr'ed properly 使用lang ='eng'来正确获取数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 python OCR pytesseract 从图像中提取文本 - Can't extract text from an image with python OCR pytesseract 如何使用 Tesseract OCR 从具有水平线的图像中提取文本? - How to extract at text from an image with horizontal line using Tesseract OCR? 如何从图像中提取文本所需的部分,而不是使用 OCR 提取图像中的所有文本? - How to extract the required parts of the text from the image instead of extracting all the text in an image using OCR? 使用 python 从此图像中提取文本 - Extract text from this image using python 使用 OCR 从图像中读取文本,使用 python 读取具有两列或三列数据的图像 - Read text from image using OCR for the image which have two columns or three columns of data using python 使用 OCR 从多个图像中提取文本到 CSV - Extract text from multiple images to CSV using OCR 如何使用OCR从指定位置提取文本信息? - How to extract text information from specified places using OCR? Python 无法从图像中读取文本 [Python OCR with Tesseract] - Python cannot read text from an image [Python OCR with Tesseract] 如何在python中使用OCR获取从Image识别的文本坐标 - How to get the co-ordinates of the text recogonized from Image using OCR in python 在使用 OCR 从图像中提取文本期间,python 中的子进程库出现问题 - Having issue with subprocess library in python during text extraction from image using OCR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM