2019-05-14 15:08:11 3652瀏覽
在現(xiàn)如今生活中,時(shí)時(shí)刻刻都在使用智能機(jī)器,今天千鋒扣丁學(xué)堂Python培訓(xùn)老師給大家介紹一下關(guān)于Dlib+OpenCV深度學(xué)習(xí)人臉識(shí)別的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),下面我們一起來(lái)看一下吧。在這個(gè)系統(tǒng)中我預(yù)先存儲(chǔ)了下面幾位明星的正面頭像的128D人臉特征,當(dāng)然你可以存儲(chǔ)和導(dǎo)入更多的人臉。然后經(jīng)過(guò)人臉檢測(cè)、人臉圖像處理,和人臉識(shí)別等步驟識(shí)別出相應(yīng)的人臉信息,識(shí)別效果如下(怕大家被丑到所以用了明星的圖片,沒(méi)有用真實(shí)的人臉–沒(méi)有做活體檢測(cè)):
int FACE_RECOGNITION::load_db_faces(void) { intrc = -1; longhFile = 0; struct_finddata_tfileinfo; frontal_face_detectordetector =get_frontal_face_detector(); // We will also use a face landmarking model to align faces to a standard pose: (see face_landmark_detection_excpp for an introduction) deserialize("shape_predictor_68_face_landmarksdat") >>sp; // And finally we load the DNN responsible for face recognition deserialize("dlib_face_recognition_resnet_model_vdat") >>net; if ((hFile =_findfirst("\\faces\\*jpg", &fileinfo)) != -1) { do { if ((fileinfoattrib &_A_ARCH)) { if (strcmp(fileinfoname,"") != 0 && strcmp(fileinfoname,"") != 0) { if (!strcmp(strstr(fileinfoname,"") + 1 , "jpg")) { cout <<"This file is an image file!" <<fileinfoname <<endl; matrix<rgb_pixel>img; charpath[260]; sprintf_s(path,"\\faces\\%s",fileinfoname); load_image(img,path); image_windowwin(img); for (autoface :detector(img)) { autoshape =sp(img,face); matrix<rgb_pixel>face_chip; extract_image_chip(img,get_face_chip_details(shape, 150, 25),face_chip); //Record the all this face's information FACE_DESCsigle_face; sigle_faceface_chip =face_chip; sigle_facename =fileinfoname; std::vector<matrix<rgb_pixel>>face_chip_vec; std::vector<matrix<float, 0, 1>>face_all; face_chip_vecpush_back(move(face_chip)); //Asks the DNN to convert each face image in faces into a 128D vector face_all =net(face_chip_vec); //Get the feature of this person std::vector<matrix<float, 0, 1>>::iteratoriter_begin = face_allbegin(), iter_end =face_allend(); if (face_allsize() > 1)break; sigle_faceface_feature = *iter_begin; //all the person description into vector face_desc_vecpush_back(sigle_face); winadd_overlay(face); } } else { cout <<"This file is not image file!" <<fileinfoname <<endl; } } } else { //filespush_back(passign(path)append("\\")append(fileinfoname)); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } returnrc; }
intcapture_face(Matframe,Mat&out) { Matgray; Matface; intrc = -1; if (frame.empty() || !frame.data)return -1; cvtColor(frame,gray,CV_BGR2GRAY); int *pResults =NULL; unsignedchar *pBuffer = (unsignedchar *)malloc(DETECT_BUFFER_SIZE); if (!pBuffer) { fprintf(stderr,"Can not alloc buffer.\n"); return -1; } //pResults = facedetect_frontal_tmp((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step, // 1.2f, 5, 24); pResults =facedetect_multiview_reinforce(pBuffer, (unsignedchar*)(gray.ptr(0)),gray.cols,gray.rows, (int)gray.step, 1.2f, 2, 48, 0, 1); //printf("%d faces detected.\n", (pResults ? *pResults : 0));//重復(fù)運(yùn)行 //print the detection results if (pResults !=NULL) { for (inti = 0;i < (pResults ? *pResults : 0);i++) { short *p = ((short*)(pResults + 1)) + 6 *i; intx =p[0]; inty =p[1]; intw =p[2]; inth =p[3]; intneighbors =p[4]; Rect_<float>face_rect =Rect_<float>(x,y,w, h); face =frame(face_rect); printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n",x,y, w,h,neighbors); Pointleft(x,y); Pointright(x +w,y + h); cv::rectangle(frame,left,right, Scalar(230, 255, 0), 4); } //imshow("frame", frame); if (face.empty() || !face.data) { face_detect_count = 0; return -1; } if (face_detect_count++ > 30) { imshow("face",face); out =face.clone(); return 0; } } else { //face is moving, and reset the detect count face_detect_count = 0; } returnrc; }
matrix<rgb_pixel> face_cap; //save the capture in the project directory load_image(face_cap, ".\\cap.jpg"); //Display the raw image on the screen image_window win1(face_cap); frontal_face_detector detector = get_frontal_face_detector(); std::vector<matrix<rgb_pixel>> vect_faces; for (auto face : detector(face_cap)) { auto shape = face_recognize.sp(face_cap, face); matrix<rgb_pixel> face_chip; extract_image_chip(face_cap, get_face_chip_details(shape, 150, 0.25), face_chip); vect_faces.push_back(move(face_chip)); win1.add_overlay(face); } if (vect_faces.size() != 1) { cout <<"Capture face error! face number "<< vect_faces.size() << endl; cap.release(); goto CAPTURE; } //Use DNN and get the capture face's feature with 128D vector std::vector<matrix<float, 0, 1>> face_cap_desc = face_recognize.net(vect_faces); //Browse the face feature from the database, and find the match one std::pair<double,std::string> candidate_face; std::vector<double> len_vec; std::vector<std::pair<double, std::string>> candi_face_vec; candi_face_vec.reserve(256); for (size_t i = 0; i < face_recognize.face_desc_vec.size(); ++i) { auto len = length(face_cap_desc[0] - face_recognize.face_desc_vec[i].face_feature); if (len < 0.45) { len_vec.push_back(len); candidate_face.first = len; candidate_face.second = face_recognize.face_desc_vec[i].name.c_str(); candi_face_vec.push_back(candidate_face); #ifdef _FACE_RECOGNIZE_DEBUG char buffer[256] = {0}; sprintf_s(buffer, "Candidate face %s Euclid length %f", face_recognize.face_desc_vec[i].name.c_str(), len); MessageBox(CString(buffer), NULL, MB_YESNO); #endif } else { cout << "This face from database is not match the capture face, continue!" << endl; } } //Find the most similar face if (len_vec.size() != 0) { shellSort(len_vec); int i(0); for (i = 0; i != len_vec.size(); i++) { if (len_vec[0] == candi_face_vec[i].first) break; } char buffer[256] = { 0 }; sprintf_s(buffer, "The face is %s -- Euclid length %f", candi_face_vec[i].second.c_str(), candi_face_vec[i].first); if (MessageBox(CString(buffer), NULL, MB_YESNO) == IDNO) { face_record(); } } else { if (MessageBox(CString("Not the similar face been found"), NULL, MB_YESNO) == IDYES) { face_record(); } } face_detect_count = 0; frame.release(); face.release();
當(dāng)人臉或是物體快速的在攝像頭前活動(dòng)時(shí),會(huì)導(dǎo)致系統(tǒng)異常拋出,異常提示如下:
Mat frame; Mat face; VideoCapture cap(0); if (!cap.isOpened()) { AfxMessageBox(_T("Please check your USB camera's interface num.")); } try { while (1) { check_close(cap); cap >> frame; if (!frame.empty()) { if (capture_face(frame, face) == 0) { //convert to IplImage format and then save with .jpg format IplImage face_Img; face_Img = IplImage(face); //save the capture face to the project directory cvSaveImage("./cap.jpg", &face_Img); break; } imshow("view", frame); } int c = waitKey(10); if ((char)c == 'c') { break; } } } catch (exception& e) { cout << "\nexception thrown!" << endl; cout << e.what() << endl; #ifdef _CAPTURE_DEBUG MessageBox(CString(e.what()), NULL, MB_YESNO); #endif goto CAPTURE; }
在catch中將捕獲到的異常信息打印出來(lái):
【關(guān)注微信公眾號(hào)獲取更多學(xué)習(xí)資料】 【掃碼進(jìn)入Python全棧開(kāi)發(fā)免費(fèi)公開(kāi)課】
查看更多關(guān)于"Python開(kāi)發(fā)資訊"的相關(guān)文章>