身份證識別(一)——身份證正反面與頭像檢測

前言

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)互相討論學習

發佈了69 篇原創文章 · 獲贊 38 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章