First, preface
?? Identity card identification, also known as OCR technology. OCR technology is an abbreviation for optical character recognition, which transforms the text of various bills, newspapers, books, manuscripts and other printed materials into image information by means of scanning and other optical input methods, and then transforms the image information into the computer input technology that can be used by using the word recognition technology.
?? Because the project needs, so these days to check the relevant information, want to see on the internet there is no big God package of ready-made demo can be used. But no fruit, on-line on the OCR this piece of information is very few, more reliable is to charge, and the price is not cheap. But in the Celestial kingdom, the charge feeling in the heart is uncomfortable, so decided oneself research.
?? Previous final implementation effect (if the Mac is not a retain screen, the resolution will have an impact and need to be debugged on the real machine)
The effect of the final implementation. GIF Ii. technology needed
Search a lot of information, found that to identify the identification number, need to use the following techniques:
- Image processing Technology
including grayscale processing, binary, corrosion, wheel corridor detection and so on.
灰度化处理
: Image grayscale processing is to specify the image of each pixel of the RGB three components through a certain algorithm to calculate the gray value of the pixel point, so that the image contains only brightness and no color information.
Grayscale image. png
二值化
: Two the value of the processing is to convert the grayscale processing of the picture to only contain black and white two colors of the image, there is no other gray changes between them. In a binary graph, 255 is white, and 0 is black.
Binary graphs. png
腐蚀
: The corrosion of the image is to enlarge the black block in the resulting two value graph. The element that joins the neighboring black pixels in the picture. Through corrosion, the identity card number on the ID card can be connected together to form a rectangular area.
Corrosion diagram. png
轮廊检测
: The image after the corrosion operation of the adjacent points will be connected together to form a large area, this time through the wheel corridor detection can be found in each large area, so that you can locate the ID number on the area.
Wheel gallery. png
- Word Recognition technology
By identifying images, the image information is transformed into a computer input technology that can be used. For example, the following picture, which contains a series of numbers, can be used to output the digital information contained in a picture as a string by means of OCR recognition technology.
A picture that contains numbers. PNG III, open source framework OpenCV and Tesseractocrios
- OpenCV (complete image processing technology)
?? OpenCV is an open source cross-platform computer vision and machine Learning Library, popular point is that he gave the computer a pair of eyes, a pair of glasses that can get information from the picture, to complete face recognition, identity card recognition, red-eye, tracking moving objects and other image-related functions. OPENCV official website
- Tesseractocrios (Completion of text recognition technology)
?? Tesseract is the most accurate open source OCR engine available today, and can read images in various formats and convert them into text in various languages. The Tesseractocrios is the Tesseract engine library for the iOS platform package.
Iv. Demonstration of actual combat
- Create an iOS project
- Using Cocopods to import the above two libraries because the OpenCV library file is larger, so the time will be a little longer, patient waiting is.
Podfile file. png
- Run the project after the import is complete, you will find the following error
Bitode error. png
Because the imported library does not support the Bitcode mechanism, it needs to be switched off,->targets->build setting-> Enable Bitcode is set to No on the project.
Turn off Bitcode.png.
- Import the language pack required by Tesseractocrios?? Tesseractocrios Library does not have its own language pack, we need to manually import, we are here directly to the TESSERACT-OCR website, Tessdata is the language pack we need to use. The downloaded language pack has more than 400 megabytes. Here we only need to use the English language pack, so just import eng.traineddata on OK, the others are deleted.
There are several points to note when importing language packs:
- The language pack needs to be placed in the Tessdata directory. The Find language pack in Tesseractocrios is located in the Tessdata directory, so we cannot import eng.traineddata into the project separately, but we need to put it in the Tessdata directory to import the project.
- To import Tessdata into the Xcode project, you need to tick Create folder refrences. As mentioned above, the language pack needs to be placed in the Tessdata directory, so when importing files to Xcode you need to create the form of a folder instead of creating a group. Such as:
How to import the Tessdata folder. png
- Create a recogizecardmanager to manage ID-related code. Since the OpenCV and Tesseractocrios libraries are written in C + +, you need to change the. m of the recogizecardmanager.m suffix to. mm
Recogizecardmanager.png
- Code in the Recogizecardmanager
. h file
#import <Foundation/Foundation.h>@class UIImage; typedef VOID (^compleateblock) (nsstring *text); @interface Recogizecardmanager: nsobject/*** Initializes a single case * * @return Returns an instance object of Recogizecardmanager */+ ( Instancetype) Recognizecardmanager; /*** Get ID number according to photo ID * * @param cardimage incoming ID photo * @param compleate recognition completed callback */-(void) Recognizecardwithimage: ( UIImage *) cardimage compleate: (compleateblock) compleate; @end
. m file
#import"RecogizeCardManager.h"#import <opencv2/opencv.hpp>#import <opencv2/imgproc/types_c.h>#import <opencv2/imgcodecs/ios.h>#import <TesseractOCR/TesseractOCR.h> @implementation recogizecardmanager+ (instancetype) Recognizecardmanager {static Recogizecardmanager *recognizecardmanager = nil;Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{recognizecardmanager = [[Recogizecardmanager alloc] init];});return Recognizecardmanager;} - (void) Recognizecardwithimage: (UIImage *) cardimage compleate: (compleateblock) Compleate {Scan ID image and preprocess, locate the image of the number area and return to UIImage *numberimage = [self opencvscancard:cardimage];if (numberimage = = nil) {compleate (nil);}Use TESSERACTOCR to recognize text [self tesseractrecognizeimage:numberimage compleate:^ (nsstring *numbaertext) {compleate ( Numbaertext); }];}Scan ID image and preprocess, locate the number area picture and return-(UIImage *) Opencvscancard: (UIImage *) Image {Convert UIImage to Mat Cv::mat resultimage; Uiimagetomat (image, Resultimage);Converted to grayscale Cvtcolor (Resultimage, Resultimage, Cv::color_bgr2gray);Using a threshold value of two cv::threshold (Resultimage, Resultimage,100,255, cv_thresh_binary);corrosion, filling (corrosion is to make the black point larger) cv::mat erodeelement = Getstructuringelement (Cv::morph_rect, Cv::size (26,26)); Cv::erode (Resultimage, Resultimage, erodeelement);Wheel Gallery InspectionSTD::vector<STD::VECTOR<CV::P oint>> contours;Define a container to store all detected galleries cv::findcontours (resultimage, contours, Cv_retr_tree, Cv_chain_approx_simple, Cvpoint (0,0));Remove the ID number areaSTD::Vector<cv::rect> rects; Cv::rect Numberrect = Cv::rect (0,0,0,0);STD::vector<STD::VECTOR<CV::P oint>>::const_iterator itcontours = Contours.begin ();for (; Itcontours! = Contours.end (); ++itcontours) {Cv::rect Rect = Cv::boundingrect (*itcontours); Rects.push_back (rect) ;Algorithm principleif (Rect.width > Numberrect.width && rect.width > Rect.height *5) {numberrect = rect;}}ID Number location failedif (Numberrect.width = =0 | | Numberrect.height = =0) {return nil; }Successful positioning, go to the original image to intercept the identification number area, and converted to grayscale, binary processing Cv::mat matimage; Uiimagetomat (image, Matimage); Resultimage = Matimage (Numberrect); Cvtcolor (Resultimage, Resultimage, Cv::color_bgr2gray); Cv::threshold (Resultimage, Resultimage,255, cv_thresh_binary); //Convert mat to UIImage UIImage *numberimage = Mattouiimage (resultimage); return numberimage;} //using TESSERACTOCR to recognize text-(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]; //Execute callback Compleate (Tesseract.recognizedtext);});
- Recognizecardviewcontroller Code
Story Version Layout interface
Story version layout interface. png.m File
#import"RecognizeCardViewController.h"#import"RecogizeCardManager.h"@interfaceRecognizecardviewcontroller () <Uinavigationcontrollerdelegate,uiimagepickercontrollerdelegate>{Uiimagepickercontroller *imgagepickcontroller;}@property (Weaknonatomic)IboutletUiimageview *imgview;@property (Weaknonatomic)IboutletUILabel *textlabel;-(ibaction) Cameraaction: (ID) sender;-(ibaction) Photoaction: (ID) sender;@end@implementationrecognizecardviewcontroller-(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 the can be recreated.}Photo-(ibaction) Cameraaction: (ID) Sender {Determine if the camera can be openedif ([Uiimagepickercontroller issourcetypeavailable:Uiimagepickercontrollersourcetypecamera]) {Imgagepickcontroller. sourcetype =Uiimagepickercontrollersourcetypecamera;Set camera mode (take photo, record video) for photo Imgagepickcontroller. Cameracapturemode =Uiimagepickercontrollercameracapturemodephoto; [Self Presentviewcontroller:imgagepickcontroller Animated:YES Completion:NIL]; }else {Uialertview *alert = [[Uialertview Alloc] Initwithtitle:@ "hint" message:@ "Device cannot open camera" delegate:Self Cancelbuttontitle:@ "Know" Otherbuttontitles:NIL]; [Alert show]; }}Albums-(ibaction) Photoaction: (ID) Sender {imgagepickcontroller. sourcetype =Uiimagepickercontrollersourcetypephotolibrary; [Self Presentviewcontroller:imgagepickcontroller Animated:YES Completion:NIL];}#pragma mark-uiimagepickercontrollerdelegateFor all media resources, simply determine the resource type-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary<NSString *,id> *) info{NSString *mediatype=[info Objectforkey:Uiimagepickercontrollermediatype];UIImage *srcimage =NilJudging resource typesif ([mediatype isequaltostring:@ "Public.image"]) {srcimage = info[Uiimagepickercontrollereditedimage];Self. Imgview. image = Srcimage;ID card IdentificationSelf. Textlabel. Text =@ "Picture inserted successfully, in recognition ..."; [[Recogizecardmanager Recognizecardmanager] Recognizecardwithimage:srcimage compleate:^ (NSString *text) {if (Text! =Nil) {Self. Textlabel. Text = [NSString stringWithFormat:@ "Recognition Result:%@", text]; }else {Self. Textlabel. Text =@ "Please select photos";Uialertview *alert = [[Uialertview alloc] Initwithtitle:@ "Prompt" message:@ "Photo recognition failed, please select clear, no complex background ID photo Retry! "Delegate: Selfcancelbuttontitle:@" Know "Otherbuttontitles: Nil]; [Alert show]; } }]; } [Selfdismissviewcontrolleranimated:YES Completion:nil];} //Enter shooting page click Cancel button-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) Picker {[self dismissviewcontrolleranimated:YES Completion:nil];} @end
Summarize
?? Through the above experiment, the program to the identity card recognition of the correct rate can reach almost 90%, the remaining 10% depends mainly on the image preprocessing, preprocessing is the key to the entire identification system. The principle of the system also applies to obtain other information on the ID card, can also be applied to the identification of bank card, license plate number and so on. Finally, a summary of the implementation results is made.
- The correct rate of recognition depends mainly on the key points of the corrosion and the algorithm of removing the area of the ID number (the wheel corridor extraction).
腐蚀:
Corrosion parameters are important, for some introduction to corrosion, can refer to this article corrosion and swelling (eroding and dilating)
取出身份证号码区域的算法(轮廊提取):
all of the processing is to locate the area of the ID number in the picture, and the corridor extraction is one such operation. The algorithm to filter the corridor graph is very important but also a difficult point. I found the idea in this blog about iOS ID number recognition. To extract the gallery of the ID number area, the algorithm principle is that the width of the corridor is the widest of all, and the length of the width must be greater than 5 times times the height.
However, there are still many problems with this algorithm. Sometimes it is possible that a more complex picture background will affect the detection of the wheel gallery, based on this problem:
- On the one hand, the image preprocessing can be optimized to reduce interference to detect the area of ID card number.
- The second aspect is the optimization algorithm.
- Recognition speed using Tesseractocrios to identify the clearer text is faster, I tried to use an unprocessed picture of the number to handle, the recognition speed is less than 5s. But after the two-value graph processing recognition speed is reduced, I think the two value of the image can be processed further processing, such as two-value graph to refine the skeleton, and then do a uniform expansion of the skeleton, so that the identification number may be much clearer.
Here are a few learning sites on OPENCV
OPENCV Official Learning Document
OpenCV Getting Started Guide
OPEN CV for IOS
iOS ID number identification