身份证识别(一)——身份证正反面与头像检测

前言

1、这是一个手持的身份识别项目,属于图像识别的范围,不需要借助手持身份扫描器,只是检测当前视频或者摄像头中是否有身份证,然后做相关的处理。
2.这个项目是我之前做的一个大项目的一部分,项目用到的库有OpenCV,Boost, 还Caffe这个深度学习框架,用的一些相关的技术有图像处理的人脸检测,人脸识别,人脸验证,数字识别,汉字识别,以后有时间,可能会加上语音识别,或者对应声纹识别的相关操作。
3.我的项目环境是Ubuntu,Qt Creator 5.9.OpenCV3.3,使用Caffe训练VGG,VGG_Face,OCR等。

身份证检测

1.资源准备
(1)我写了个爬虫从网上下载了一大堆有身证的图像,这些身份都是公开的或者演示用的随机生成的身份证,所以应该不会某个人的个人隐私权,如果有侵犯,请私信我,下面是我下载的身份证的相关图像:
在这里插入图片描述
(2)使用LabelImage标注数据
在这里插入图片描述
(3)使用Caffe SSD的VGGNet训练模型,关于如何训练去调试参数,可以看我之前关于Caffe_SSD如何训练模型。
2.使用模型检测图像
(1)把以下三个文件导入到工程
在这里插入图片描述
(2)编写代码,进行识别。

void MainWindow::detectionId(Mat &input, string model_file,string model_text_file,string label_file)
{
    vector<String> objNames = readLabels(label_file);

    Ptr<dnn::Importer> importer;
    try
    {
        importer = createCaffeImporter(model_text_file, model_file);
    }
    catch (const cv::Exception &err)
    {
        cerr << err.msg << endl;
    }
    Net net;
    importer->populateNet(net);
    importer.release();

    Mat input_image = preprocess(input);
    Mat blobImage = blobFromImage(input_image);

    net.setInput(blobImage, "data");
    Mat detection = net.forward("detection_out");
    Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());
    float confidence_threshold = 0.2;
    for (int i = 0; i < detectionMat.rows; i++)
    {
        float confidence = detectionMat.at<float>(i, 2);
        if (confidence > confidence_threshold)
        {
            size_t objIndex = (size_t)(detectionMat.at<float>(i, 1));
            float tl_x = detectionMat.at<float>(i, 3) * input.cols;
            float tl_y = detectionMat.at<float>(i, 4) * input.rows;
            float br_x = detectionMat.at<float>(i, 5) * input.cols;
            float br_y = detectionMat.at<float>(i, 6) * input.rows;

            Rect object_box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));
            rectangle(input, object_box, Scalar(i*10, 0, 255), 2, 8, 0);
            putText(input, format("%s", objNames[objIndex].c_str()), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);
        }
    }

}

Mat MainWindow::getMean(size_t &w, size_t &h)
{
    Mat mean;
    vector<Mat> channels;
    for (int i = 0; i < 3; i++)
    {
        Mat channel(h, w, CV_32F, Scalar(meanValues[i]));
        channels.push_back(channel);
    }
    merge(channels, mean);
    return mean;
}

Mat MainWindow::preprocess(Mat &frame)
{
    Mat preprocessed;
    frame.convertTo(preprocessed, CV_32F);
    cv::resize(preprocessed, preprocessed, Size(width, height));
    Mat mean = getMean(width, height);
    subtract(preprocessed, mean, preprocessed);
    return preprocessed;
}

3.识别结果
(1)打开图像

在这里插入图片描述
(2)检测当前图像中的身份证和身份证上的头像。
反面与头像
在这里插入图片描述
正面,这个图像多框了一个框。
在这里插入图片描述

结语

1.这是我用深度学习caffe_ssd的vgg做的身份证检测识别,也可以用传统的opencv方法去做。传统方法可以参考车牌识别项目。
2.关于整个工程的源码,运行程序时的bug,或者有如何优化的想法都可以加这个群(487350510)互相讨论学习
3.身份证边缘检测提取可以转到这个博客
4.yolov5训练可以转到这个博客
5.yolov5 C++ OpenCV DNN推理和最终模型以上传,想试试可以转这个博客

猜你喜欢

转载自blog.csdn.net/matt45m/article/details/92069082