|
|
发表于 2016-9-8 02:03:54
|未经授权,严禁转载,违者必究...
|
|阅读模式
身份证号码 识别 iOS 身份证号码识别 身份证号码识别男女 身份证号码识别系统 图像识别 身份证号码 身份证号码识别软件 如何识别身份证号码 身份证号码识别性别 身份证号码真假识别 身份证号码识别年龄
一、媒介
??身份证识别,又称OCR手艺。OCR手艺是光学字符识此外缩写,是经由过程扫描等光学输入体例将各类单据、报刊、册本、文稿及其它印刷品的文字转化为图像信息,再操纵文字识别手艺将图像信息转化为可以利用的计较机输入手艺。
搜了良多资料,发现要进行身份证号码的识别,需要用到以下几种手艺:
经由过程识别图像,将图像信息转化为可以利用的计较机输入手艺。好比下面这张包含一串数字的图片,经由过程ocr识别手艺可以将图片中包含的数字信息以字符串的体例输出。
??OpenCV是一个开源的跨平台计较机视觉和机械进修库,通俗点的说,就是他给计较机供给了一双眼睛,一双可以从图片中获守信息的眼镜,从而完成人脸识别、身份证识别、去红眼、追踪移动物体等等的图像相关的功能。
1248713-32c649f05acf5d64.png
podfile文件.png
导入完成之后运行项目,会发现报如下错误
1248713-296ca01578ad6c92.png
Bitode报错.png
因为导入的库不撑持Bitcode机制,需要关失落,在工程->TARGETS->Build Setting-> Enable Bitcode设置为NO就ok。
#import
@class UIImage;
typedef void (^CompleateBlock)(NSString *text);
@interface RecogizeCardManager : NSObject
/**
* 初始化一个单例
*
* @return 返回一个RecogizeCardManager的实例对象
*/
+ (instancetype)recognizeCardManager;
/**
* 按照身份证照片获得身份证号码
*
* @param cardImage 传入的身份证照片
* @param compleate 识别完成后的回调
*/
- (void)recognizeCardWithImage:(UIImage *)cardImage compleate:(CompleateBlock)compleate;
@end
.m文件
#import "RecogizeCardManager.h"
#import
#import
#import
#import
@implementation RecogizeCardManager
+ (instancetype)recognizeCardManager {
static RecogizeCardManager *recognizeCardManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
recognizeCardManager = [[RecogizeCardManager alloc] init];
});
return recognizeCardManager;
}
- (void)recognizeCardWithImage:(UIImage *)cardImage compleate:(CompleateBlock)compleate {
//扫描身份证图片,并进行预处置,定位号码区域图片并返回
UIImage *numberImage = [self opencvScanCard:cardImage];
if (numberImage == nil) {
compleate(nil);
}
//操纵TesseractOCR识别文字
[self tesseractRecognizeImage:numberImage compleate:^(NSString *numbaerText) {
compleate(numbaerText);
}];
}
//扫描身份证图片,并进行预处置,定位号码区域图片并返回
- (UIImage *)opencvScanCard:(UIImage *)image {
//将UIImage转换成Mat
cv::Mat resultImage;
UIImageToMat(image, resultImage);
//转为灰度图
cvtColor(resultImage, resultImage, cv::COLOR_BGR2GRAY);
//操纵阈值二值化
cv::threshold(resultImage, resultImage, 100, 255, CV_THRESH_BINARY);
//侵蚀,填充(侵蚀是让黑色点变年夜)
cv::Mat erodeElement = getStructuringElement(cv::MORPH_RECT, cv::Size(26,26));
cv::erode(resultImage, resultImage, erodeElement);
//轮廊检测
std::vector> contours;//界说一个容器来存储所有检测到的轮廊
cv::findContours(resultImage, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
//掏出身份证号码区域
std::vector rects;
cv::Rect numberRect = cv::Rect(0,0,0,0);
std::vector>::const_iterator itContours = contours.begin();
for ( ; itContours != contours.end(); ++itContours) {
cv::Rect rect = cv::boundingRect(*itContours);
rects.push_back(rect);
//算法道理
if (rect.width > numberRect.width && rect.width > rect.height * 5) {
numberRect = rect;
}
}
//身份证号码定位掉败
if (numberRect.width == 0 || numberRect.height == 0) {
return nil;
}
//定位成功成功,去原图截取身份证号码区域,并转换成灰度图、进行二值化处置
cv::Mat matImage;
UIImageToMat(image, matImage);
resultImage = matImage(numberRect);
cvtColor(resultImage, resultImage, cv::COLOR_BGR2GRAY);
cv::threshold(resultImage, resultImage, 80, 255, CV_THRESH_BINARY);
//将Mat转换成UIImage
UIImage *numberImage = MatToUIImage(resultImage);
return numberImage;
}
//操纵TesseractOCR识别文字
- (void)tesseractRecognizeImage:(UIImage *)image compleate:(CompleateBlock)compleate {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
tesseract.image = [image g8_blackAndWhite];
tesseract.image = image;
// Start the recognition
[tesseract recognize];
//执行回调
compleate(tesseract.recognizedText);
});
}
RecognizeCardViewController代码
故事版结构界面
1248713-986b4b9b54b36436.png
故事版结构界面.png
.m文件
#import "RecognizeCardViewController.h"
#import "RecogizeCardManager.h"
@interface RecognizeCardViewController ()[U]{
UIImagePickerController *imgagePickController;
}
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
- (IBAction)cameraAction:(id)sender;
- (IBAction)photoAction:(id)sender;
@end
@implementation RecognizeCardViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imgView.contentMode = UIViewContentModeScaleAspectFit;
imgagePickController = [[UIImagePickerController alloc] init];
imgagePickController.delegate = self;
imgagePickController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
imgagePickController.allowsEditing = YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//摄影
- (IBAction)cameraAction:(id)sender {
//判定是否可以打开拍照机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imgagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置摄像头模式(摄影,录制视频)为摄影
imgagePickController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
[self presentViewController:imgagePickController animated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"设备不克不及打开相机" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
[alert show];
}
}
//相册
- (IBAction)photoAction:(id)sender {
imgagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imgagePickController animated:YES completion:nil];
}
#pragma mark - UIImagePickerControllerDelegate
//合用获取所有媒体资本,只需判定资本类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
UIImage *srcImage = nil;
//判定资本类型
if ([mediaType isEqualToString:@"public.image"]){
srcImage = info[UIImagePickerControllerEditedImage];
self.imgView.image = srcImage;
//识别身份证
self.textLabel.text = @"图片插入成功,正在识别中...";
[[RecogizeCardManager recognizeCardManager] recognizeCardWithImage:srcImage compleate:^(NSString *text) {
if (text != nil) {
self.textLabel.text = [NSString stringWithFormat:@"识别成果:%@",text];
}else {
self.textLabel.text = @"请选择照片";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒" message:@"照片识别掉败,请选择清楚、没有复杂布景的身份证照片重试!" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil];
[alert show];
}
}];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
//进入拍摄页面点击打消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil];
}
@end
总结
??经由过程上面的尝试,该法式对身份证识此外准确率几乎可以达到90%,剩下的10%首要取决于图像的预处置,预处置法式是整个识别系统的要害地点。该系统的道理同样也合用于获取身份证上其他的信息,也可以应用于银行卡、车商标等的识别。最后针对实现的结果进行一步总结。
这里贴上几个关于OpenCV的进修网站
OpenCV官方进修文档
OpenCV入门指南
OPEN CV for iOS
该项目已经开源在github RecognizeCard 上了,假如喜好可以点个赞。有什么问题可以留言,我也是第一次接触,一路前进,大师加油。
|
|