日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

身份证识别——iOS端实现身份证检测

發(fā)布時(shí)間:2025/3/21 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 身份证识别——iOS端实现身份证检测 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

1.之前在PC端做過(guò)身份證檢測(cè)識(shí)別相關(guān)的項(xiàng)目,用的環(huán)境是Caffe-SSD訓(xùn)練的VGG16,模型大小大概為90M左右,在PC下,不調(diào)用GPU加速的話,處理檢測(cè)速度并不理想。之后想把這個(gè)項(xiàng)目移植到移動(dòng)端,然后在IPhone XR Max 上做了測(cè)試,速度比PC端更慢了,而且體積巨大,根本沒(méi)有辦法應(yīng)用到項(xiàng)目上。
2.為了能在移動(dòng)端運(yùn)行目標(biāo)檢測(cè)模型,那只能重新訓(xùn)練,看了一堆資料和測(cè)試各種官方Demo,之后選了MobileNetv2-SSDLite,訓(xùn)練框架還是用Caffe。

模型訓(xùn)練

1.關(guān)于前期的數(shù)據(jù)準(zhǔn)備與數(shù)據(jù)樣本標(biāo)注,可以看我之前身份證識(shí)別的博客,我訓(xùn)練時(shí)還是用VOC2007這種數(shù)據(jù)格式。
2.MobileNetv2-SSDLite什么訓(xùn)練自己的數(shù)據(jù)集,可以看這個(gè)博客
,博主寫得很詳細(xì)。
3.在訓(xùn)練過(guò)程中發(fā)現(xiàn),同樣的數(shù)據(jù)集,同樣的迭代次數(shù),caffe-ssd訓(xùn)練出來(lái)的模型精度要高出MobileNetv2-SSDLite幾個(gè)百分點(diǎn),而且MobileNetv2-SSDLite對(duì)特征弱的物體識(shí)別很容易出現(xiàn)誤檢的現(xiàn)象,為了精度能達(dá)到可用的級(jí)別,唯一的辦法是加樣本,但身份證這種數(shù)據(jù)集又比較敏感,很不好收集,想了各種辦法,才收集了一萬(wàn)張左右的數(shù)據(jù),再寫個(gè)仿真算法,把數(shù)據(jù)擴(kuò)增到十萬(wàn)張左右,迭代20萬(wàn)代左右,精度可以達(dá)99.5%。
4.最終的模型大小在14M左右,我放了6個(gè)類型在里面,在真機(jī)下檢測(cè)一張圖像的速度大概在0.02秒左右,基本上可以達(dá)到實(shí)時(shí)。

應(yīng)用代碼

1.在OpenCV3之后的版本都有dnn這個(gè)模塊,很好的對(duì)接深度學(xué)習(xí)的模型,我這里用的是OpenCV4.2這個(gè)版本,iOS是不支持直接顯示OpenCV的Mat這種圖像格式的,要把Mat轉(zhuǎn)成UIImage才能在iOS上顯示,關(guān)于轉(zhuǎn)換的代碼可以看我之前的博客。
2.OC是可以直接與C++交互的,所以檢測(cè)的代碼我直接用C++寫的。
代碼:

bool idDetection(cv::Mat &cv_src, cv::Mat &cv_dst, std::string &model_path, std::string &proto_path, std::vector<std::string> &label) {if (cv_src.empty()){return false;}cv_dst = cv_src.clone();cv::Size reso(300, 300);cv::dnn::Net net = cv::dnn::readNet(model_path,proto_path);if (net.empty()){return false;}cv::Mat blob = cv::dnn::blobFromImage(cv_src, 1.0, reso, cv::Scalar(0, 0, 0), true, false);net.setInput(blob);cv::Mat out = net.forward();cv::Mat detectionMat(out.size[2], out.size[3], CV_32F, out.ptr<float>());float confThreshold = 0.25f;float nmsThreshold = 0.5f;std::vector<int> classIds;std::vector<float> confidences;std::vector<cv::Rect> boxes;for (int i = 0; i < detectionMat.rows; i++){float confidence = detectionMat.at<float>(i, 2);if (confidence > confThreshold){size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));int left = static_cast<int>(detectionMat.at<float>(i, 3) * cv_src.cols);int top = static_cast<int>(detectionMat.at<float>(i, 4) * cv_src.rows);int right = static_cast<int>(detectionMat.at<float>(i, 5) * cv_src.cols);int bottom = static_cast<int>(detectionMat.at<float>(i, 6) * cv_src.rows);int width = right - left + 1;int height = bottom - top + 1;classIds.push_back(objectClass);boxes.push_back(cv::Rect(left, top, width, height));confidences.push_back(confidence);}}std::vector<int> indices;cv::dnn::NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);std::vector<int> id;for (size_t i = 0; i < indices.size(); ++i){int idx = indices[i];cv::Rect box = boxes[idx];rectangle(cv_dst, filter_ida.at(i).tl(), filter_ida.at(i).br(), cv::Scalar(0, 0, 255), 2, 8, 0);//id.push_back(classIds[idx]);} }

3.在Xcode里面,把要與C++交互的源碼文件.m更改成.mm,定義一個(gè)點(diǎn)擊事件,然后添加代碼:

-(void)idDetectioBtn {NSString* const model_file_name = @"inference";NSString* const model_file_type = @"caffemodel";NSString* const proto_file_name = @"inference";NSString* const proto_file_type = @"prototxt";NSString* model_path = [[NSBundle mainBundle] pathForResource:model_file_name ofType:model_file_type];NSString* prototxt_path = [[NSBundle mainBundle] pathForResource:proto_file_name ofType:proto_file_type];std::string str_proto = [prototxt_path UTF8String];std::string str_model = [model_path UTF8String];cv::Mat cv_src,cv_dst;UIImageToMat(self.ui_selected_image, cv_src);std::vector<std::string> id_label;idDetection(cv_src, cv_dst, str_model, str_proto, id_label);UIImage *ui_image = MatToUIImage(cv_dst);self.ui_show_view.image = ui_image; }

4.運(yùn)行效果


注:
對(duì)圖像處理有興趣的可以可以加

總結(jié)

以上是生活随笔為你收集整理的身份证识别——iOS端实现身份证检测的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。