身份证识别

安装环境

tar leptonica-1.77.0
./configure
make && make install
  1. centos yum install epel-release yum install tesseract-devel leptonica-devel
  2. debian apt-get install libtesseract-dev libtesseract3 tesseract-ocr

pip install opencv-python pytesseract tesseract tesseract-ocr

代码

import re
import cv2
import pytesseract
from PIL import Image# 身份证号
r = r'^([1-9]\d{5}[12]\d{3}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])\d{3}[0-9xX])$'# 根据比例和偏移算出号码位置
# 一般面部比例
def CalcIdRectByFaceRect_normal(x, y, w, h):scale = float(w) / 95x1 = int(x + ((0 - 159)) * scale)y1 = int(y + (0 + (149)) * scale)x2 = int(x + (0 - 159 + (275)) * scale)y2 = int(y + (0 + (149) + (45)) * scale)return (x1, y1, x2, y2)# 较大面部比例
def CalcIdRectByFaceRect_big(x, y, w, h):scale = float(w) / 95x1 = int(x + ((0 - 159) + 10) * scale)y1 = int(y + (0 + (149 - 3)) * scale)x2 = int(x + (0 - 159 + (275 - 10)) * scale)y2 = int(y + (0 + (149 - 3) + (45 - 10)) * scale)return (x1, y1, x2, y2)# 较小面部比例
def CalcIdRectByFaceRect_small(x, y, w, h):scale = float(w) / 95x1 = int(x + ((0 - 159) - 10) * scale)y1 = int(y + (0 + (149 + 3)) * scale)x2 = int(x + (0 - 159 + (275 + 10)) * scale)y2 = int(y + (0 + (149 + 5) + (45 + 10)) * scale)return (x1, y1, x2, y2)# 二值化算法
def binarizing(img, threshold):pixdata = img.load()w, h = img.sizefor y in range(h):for x in range(w):if pixdata[x, y] < threshold:pixdata[x, y] = 0else:pixdata[x, y] = 255return img# 去除干扰线算法
def depoint(img):  # input: gray imagepixdata = img.load()w, h = img.sizefor y in range(1, h - 1):for x in range(1, w - 1):count = 0if pixdata[x, y - 1] > 245:count = count + 1if pixdata[x, y + 1] > 245:count = count + 1if pixdata[x - 1, y] > 245:count = count + 1if pixdata[x + 1, y] > 245:count = count + 1if count > 2:pixdata[x, y] = 255return img#  通过头像的位置 身份证号码识别
def identity_OCR_byFaceRect(oImg, faceRect):(x, y, w, h) = faceRectiw, ih = oImg.size# 将身份证放大3倍largeImg = oImg.resize((iw * 3, ih * 3), Image.ANTIALIAS)# largeImg.save('1_large.png')(x1, y1, x2, y2) = CalcIdRectByFaceRect_normal(x, y, w, h)region = (x1 * 3, y1 * 3, x2 * 3, y2 * 3)code = GetRegionString(largeImg, region)if not re.match(r, code):(x1, y1, x2, y2) = CalcIdRectByFaceRect_small(x, y, w, h)region = (x1 * 3, y1 * 3, x2 * 3, y2 * 3)code = GetRegionString(largeImg, region)if not re.match(r, code):(x1, y1, x2, y2) = CalcIdRectByFaceRect_big(x, y, w, h)region = (x1 * 3, y1 * 3, x2 * 3, y2 * 3)code = GetRegionString(largeImg, region)if not re.match(r, code):code = 'NONE'return code, (x1, y1, x2, y2)def GetRegionString(img, region):# 裁切身份证号码图片cropImg = img.crop(region)cropImg.save('/tmp/2_crop.png')# 转化为灰度图grayImg = cropImg.convert('L')# grayImg.save('3_grey.png')# 把图片变成二值图像。bImg = binarizing(grayImg, 100)# bImg.save('4_bin.png')dImg = depoint(bImg)# dImg.save('5_depoint.png')code = pytesseract.image_to_string(dImg)code = PostProc(code)return code#  号码后处理
def PostProc(s):res = sres = res.replace(" ", "")res = res.replace("O", "0")res = res.replace("U", "0")res = res.replace("D", "0")res = res.replace("Z", "2")res = res.replace("S", "5")res = res.replace("s", "5")res = res.replace("o", "6")res = res.replace("f", "7")res = res.replace("H", "11")return res#  检测身份证
def DetectFacesAndIDs(pic_path):frame = cv2.imread(pic_path)oImg = Image.open(pic_path)ih, iw = frame.shape[:2]# 人脸识别分类器classfier = cv2.CascadeClassifier("/usr/local/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_alt.xml")# 识别出人脸后要画的边框的颜色,RGB格式color = (0, 255, 0)color2 = (255, 0, 0)# 将当前帧转换成灰度图像gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)# 人脸检测,1.2和2分别为图片缩放比例和需要检测的有效点数faceRects = classfier.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))if len(faceRects) > 0:  # 大于0则检测到人脸for faceRect in faceRects:  # 单独框出每一张人脸x, y, w, h = faceRectcv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)code, (x1, y1, x2, y2) = identity_OCR_byFaceRect(oImg, faceRect)cv2.rectangle(frame, (x1, y1), (x2, y2), color2, 2)#cv2.imwrite("%s.iddet.png" % pic_path, frame)return codereturn 'NONE'if __name__ == '__main__':
DetectFacesAndIDs('1.jpg')

Python OpenCV实现身份证号码识别相关推荐

  1. python基础教程:python+OpenCV实现车牌号码识别

    这篇文章主要介绍了python+OpenCV实现车牌号码识别,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 基于python+OpenCV的车牌号码识别,供大家参考,具 ...

  2. 基于python+OpenCV的车牌号码识别

    基于python+OpenCV的车牌号码识别 车牌识别行业已具备一定的市场规模,在电子警察.公路卡口.停车场.商业管理.汽修服务等领域已取得了部分应用.一个典型的车辆牌照识别系统一般包括以下4个部分: ...

  3. 基于深度学习的身份证号码识别(OCR,Opencv,Pytorch)

    文章目录 前言 模型结构 数据集 模型训练 项目结构&源码下载 前言 最近做了一个身份证号码识别项目,在此分享一下.视频效果如下所示,共有两种识别方法,其一就是直接上传身份证号码的截图进行识别 ...

  4. Android Studio+OpenCV 识别身份证号码---识别身份证号码

    上一章使用CLion+OpenCV对身份证号码进行了检测,经过检测我们能拿到身份证号码区域的照片,现在我们对上一章拿到的图片中的数字进行识别 上一章链接:CLion+OpenCV 识别身份证号码--- ...

  5. 利用python进行身份证号码大全_用 Java 撸一个身份证号码识别系统,准确率高达 90%...

    项目介绍 本项目是通过学习https://gitee.com/nbsl/idCardCv 后整合tess4j,不需要经过训练直接使用的,当然,你也可以进行训练后进行使用.该项目修改原有的需要安装ope ...

  6. 牛逼了!github上一个 身份证号码识别系统

    前言 最近发现一个有趣的项目. 这个项目是通过学习https://gitee.com/nbsl/idCardCv 后整合 tess4j,不需要经过训练直接使用的,当然,你也可以进行训练后进行使用. 该 ...

  7. iOS身份证号码识别

    最近不少简友说git上下载下来的代码报各种问题,因为包含的库都比较大,所以大家在pod的时候耐心等待,另外我已经将代码适配到了iOS10. 一.前言   身份证识别,又称OCR技术.OCR技术是光学字 ...

  8. Opencv获取身份证号码区域

    记得应该是16年的时候,从一个公开课看到了关于OCR方面的内容,里面讲到了通过OpenCV对身份证号码区域的剪裁以及使用Tess-Two进行文字识别,实现了对身份证号码的识别功能. 断断续续看了点关于 ...

  9. Java 身份证号码识别系统

    最近发现一个有趣的项目. 这个项目是通过学习https://gitee.com/nbsl/idCardCv 后整合 tess4j,不需要经过训练直接使用的,当然,你也可以进行训练后进行使用. 该项目修 ...

  10. Python+OpenCv实现AI人脸识别身份认证系统(2)——人脸数据采集、存储

    原 Python+OpenCv实现AI人脸识别身份认证系统(2)--人脸数据采集.存储 2019年07月02日 08:47:52 不脱发的程序猿 阅读数 602更多 所属专栏: 人脸识别身份认证系统设 ...

最新文章

  1. Docker入门六部曲——Stack
  2. make 操作技巧指南--gcc版本设置
  3. 提高SQL的查询效率
  4. 一篇文章讲懂Vmware网卡配置,解决常见问题
  5. 前端面试题整理【转】
  6. SAP CRM系统UI checkbox的设计与实现
  7. python 列表函数
  8. Wpf解决TextBox文件拖入问题、拖放问题
  9. cocos2D中实现滑动菜单CCScrollView+CCMenu效果,(注意不是cocos2D-x)!!
  10. oracle字符数量,Oracle中统计表中每行字段符合条件的数量
  11. 华三H3C链路聚合配置实例
  12. html语言添加点击事件,vue 中拼接html时添加点击事件
  13. 乱码 问号 java_java 中文 乱码 问号
  14. echart 图谱_echart——关系图graph详解
  15. MATLAB中subs函数
  16. SOLIDWORKS出工程图时,小数点前的“0”不显示怎么办?
  17. 解决Pycharm出现的Debug无法正常运行(Frames are not available)的问题
  18. Bessie‘s Dream
  19. 各种OJ网站,刷题必备
  20. win7计算机本地用户和组,Win7系统找不到本地用户和组的两大解决方案

热门文章

  1. 【常用办公软件有那些】万彩办公大师教程丨屏幕放大镜的使用
  2. ib网卡无法启动,需要修改为以太网模式
  3. 【Java】ResourceBundle 使用
  4. 2015-5-10分享pdf
  5. firebug下载地址
  6. php 判断是否为中文,php判断是否为中文正则表达式大全
  7. 淘宝双11促销背后高并发处理之淘宝网采用什么技术架构来实现网站高负载
  8. 【UWB 定位】室内定位 三边定位算法
  9. 红帽子linux拨号上网,centos6.5宽带拨号上网的方法
  10. python访问纯真IP数据库