首页 > 编程知识 正文

利用python进行身份证号码(python 提取列表元素)

时间:2023-05-06 04:40:58 阅读:65831 作者:1503

在此,利用pytesseract、opencv3等库,提取身份证上的有用信息(没有照片的方面)。 如果图像分辨率不是很高,则必须首先通过插值图像来提高图像分辨率,以便更好地识别身份证信息。 像脸的某一面,不能根据脸的特征来修改身份证,所以需要修改照片上的身份证的方向。

% matplotlib在线

% configinlinebackend.figure _ format=' retina '

导入类型服务

导入cv2

import matplotlib.pyplot as plt

importmatplotlib.patchesasmpatches

from skimage import io

import numpy as np

导入pandas as PD

导入re

面试时读取图像,然后放大图像:

image=io.imread (身份证背面3.jpg ) )。

打印(image.shape )。

high,width=image.shape[0:2]

#将图像放大两倍,44像素附近的双立方插值

image=cv2.resize(image,(width*4,high*4),interpolation=cv2.INTER_CUBIC ) ) )。

PLT.imshow(image ) )。

PLT.axis (关闭) ) )。

plt.show () )

打印(image.shape ) )。

(403,640,3 )

(1612,2560,3 )

然后,使用pytesseract库的方法直接识别图像中的中文。

可以在pytesseract库中看到检查效果,但结果不好

text=pytesseract.image _ to _ string (image,lang='chi_sim ' ) ) ) ) ) ) ) ) ) )。

是打印(文本)

颁发机关建瓯市公安局

有效期为2012.06.25-2022.O6.25

虽然能识别一些信息,但并不是所有的中文都完全识别。

接下来,对图像进行二值化以识别中文。

#处理图像,转换为灰度图像=二值图像

imagegray=cv2.cvtcolor(image,cv2.COLOR_RGB2GRAY )。

retval,image bin=cv2.threshold (image gray,120,255,cv2.thresh _ ots ucv2. thresh _ binary ) ) )。

PLT.imshow(imagebin,cmap=plt.cm.gray ) )。

PLT.axis (关闭) ) )。

plt.show () )

#再次在pytesseract库中确认了检查效果,但结果不佳

text=pytesseract.image _ to _ string (image bin,lang='chi_sim ' ) ) ) ) ) ) ) ) )。

是打印(文本)

(() (() ) ) ) ) ) ) ) ) ) ) )中华人民共和国

居民身份证

颁发机关建瓯市公安局

有效期为2012-06.25-2022.06.25

可见,所有有用的信息都得到了正确的识别。

提取下一个有用的内容:

#提取识别的内容

textlist=text.split('n ' )。

textdf=PD.dataframe((text ) :文本列表) )。

text df [ ' text len ' ]=text df.text.apply (

#删除长度(=1的行

text df=text df [ text df.text len1].reset _ index (drop=true ) ]

文本文档

文本文本len

0 `“熹”“”“”“……”“_”“……_”“……”“…”“〈_〉中华人民共和国39”

1居民身份证5

2发证机关建瓯市公安局11

3有效期为2012-06.25-2022.06.25 26

进一步处理:

#提取适当的信息

print ('第1行: ',textdf.text[0][-7:] )

print ()=====================' )

print ('第2行: ',textdf.text[1][-5:] )

print ()=====================' )

print ('颁发机构: ',textdf.text[2].split ) ) (-1 ) )

print ()=====================' )

#提取时间信息

timetext=textdf.text[3]

timetext=re.findall('d ',timetext )。

timetext

print ('有效开始时间: ','.'.join ) timetext[0:3] ) )

print ()=====================' )

print ('失效时间: ','.'.join ) timetext[3:] ) )

print ()=====================' )

第一行:中华人民共和国

====================

第二行:居民身份证

====================

发证机关:建瓯市公安局

====================

有效开始时间: 2012.06.25

====================

失效时间: 2022.06.25

====================

这方面的文字大小差异非常大,缺少定位图像倾斜的因素,因此对照片的要求更高。 上述方法需要进一步优化以提高适用性。 身份证上的照片来自网络上的照片。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。