IOS ID card identification (OCR source code) detailed explanation and example code

  • 2021-12-09 10:18:27
  • OfStack

Detailed explanation of IOS ID card identification (OCR source code)

Recently, the project used ID card identification. I searched a pile of demo on github and found a pile of codes on Google. Some of them can identify ID photos, but they are all static libraries packaged into. a. There is no source code. I worked hard to eat books for a few days and have 1 research result. Now I post it to share with you. If there is a better method, I hope God will correct me and discuss solutions together. (The following code is available for personal testing, and we are exploring intelligent identification in the next step. If you are interested, please join)

Two open source libraries are used here: OpenCV, TesseractOCRiOS, and two language packs chi_sim, eng. The process of ID card recognition mainly includes: gray scale, threshold 2 value, corrosion, contour detection, taking out ID card number area, TesseractOCR recognition of characters.

ID card identification core source code:


UIImage * image = [UIImage imageNamed:@"abc.png"];

// Will UIImage Convert to Matcv::Mat resultImage;

UIImageToMat(image, resultImage);

// Turn to gray image 

cvtColor(resultImage, resultImage, 6);

// Using Threshold 2 Valuation 

cv::threshold(resultImage, resultImage, 100, 255, CV_THRESH_BINARY);

// Corrosion, filling (corrosion is to make black spots bigger) 

cv::Mat erodeElement = getStructuringElement(cv::MORPH_RECT, cv::Size(140,140)); 

cv::erode(resultImage, resultImage, erodeElement);

// Corridor detection std::vector> contours;

// Definition 1 Containers to store all detected corridors 

cv::findContours(resultImage, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

// Take out the ID number area 

std::vectorrects;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);

NSLog(@" The positions are respectively :x=%d,y=%d,width=%d,height%d",rect.x,rect.y,rect.width,rect.height);

// Algorithm principle: If the new area range width is larger than the assigned area width and the width is high 5 Times gives a new value 

    if (rect.width > numberRect.width && rect.width > rect.height * 5 && rect.height > 200 && rect.height < 300) {

    numberRect = rect;

    }

}

// Positioning is successful, go to the original image to intercept the ID number area, and convert it into a gray image, and carry out 2 Valuation processing 

cv::Mat matImage;

UIImageToMat(image, matImage);

resultImage = matImage(numberRect);

cvtColor(resultImage, resultImage, cv::COLOR_BGR2GRAY);

cv::threshold(resultImage, resultImage, 80, 255, CV_THRESH_BINARY);

// Will Mat Convert to UIImage

UIImage *numberImage = MatToUIImage(resultImage);

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: