Detailed explanation of IOS ID card identification (OCR source code)
Recently, the project used ID card recognition. I searched for a lot of demos on github and found a lot of code on Google. There are those that can identify the ID photos, but they are all packaged into .a. There is no source code. I have worked hard for a few days and have some research results. I am posting it to share with you. If there is a better method, I hope the master will correct it and discuss solutions together. (The following code is available for personal testing and is further exploring intelligent recognition. If you are interested, please join)
Two open source libraries are used here: OpenCV, TesseractOCRiOS, and two language packages chi_sim, and eng. The main processes of ID card identification include: grayscale, threshold binarization, corrosion, contour detection, taking out the ID card number area, and TesseractOCR identification text.
Identity card identification core source code:
UIImage * image = [UIImage imageNamed:@""]; //Convert UIImage to Matcv::Mat resultImage; UIImageToMat(image, resultImage); //Convert to grayscale cvtColor(resultImage, resultImage, 6); //Use threshold binarization cv::threshold(resultImage, resultImage, 100, 255, CV_THRESH_BINARY); //Corrosion, filling (corrosion makes the black point bigger) cv::Mat erodeElement = getStructuringElement(cv::MORPH_RECT, cv::Size(140,140)); cv::erode(resultImage, resultImage, erodeElement); //Tangle detection std::vector> contours; //Define a container to store all detected galleries cv::findContours(resultImage, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); //Retrieve the ID number area std::vectorrects;cv::Rect numberRect = cv::Rect(0,0,0,0); std::vector>::const_iterator itContours = (); for ( ; itContours != (); ++itContours) { cv::Rect rect = cv::boundingRect(*itContours); rects.push_back(rect); NSLog(@"The positions are:x=%d,y=%d,width=%d,height%d",,,,); //Algorithm principle: if the new area range width is greater than the assigned area width and the width is five times the height, the new value will be assigned if ( > && > * 5 && > 200 && < 300) { numberRect = rect; } } //The positioning is successful, the ID number area is captured from the original image, and converted into a grayscale image and binarized processing is performed cv::Mat matImage; UIImageToMat(image, matImage); resultImage = matImage(numberRect); cvtColor(resultImage, resultImage, cv::COLOR_BGR2GRAY); cv::threshold(resultImage, resultImage, 80, 255, CV_THRESH_BINARY); //Convert Mat to UIImage UIImage *numberImage = MatToUIImage(resultImage);
Thank you for reading, I hope it can help you. Thank you for your support for this site!