Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void face_train()
- {
- // Training Images for me
- images.push_back(imread("me/me1.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
- images.push_back(imread("me/me2.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
- images.push_back(imread("me/me3.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
- // Training Images for Brad Pitt
- images.push_back(imread("Pitt/Brad_Pitt_1.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
- images.push_back(imread("Pitt/Brad_Pitt_2.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
- images.push_back(imread("Pitt/Brad_Pitt_3.jpeg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
- }
- void face_detect()
- {
- Mat image1;
- Mat gray;
- //G E T I M A G E
- if ( r_c % 2 == 0 )
- image1 = imread("Cam_Pic1.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
- else if ( r_c % 2 != 0 )
- image1 = imread("Cam_Pic2.jpeg", CV_LOAD_IMAGE_GRAYSCALE);
- if (image1.empty())
- {
- cout << "!!! Failed imread(): image not found" << endl;
- }
- // D E T E C T F A C E
- cvtColor(image1, gray, CV_BGR2GRAY);
- //equalizeHist(gray, gray);
- vector<Rect_<int>> faces;
- face_cascade.load("haarcascade_frontalface_alt.xml"); // xml file with facial features algorithm
- face_cascade.detectMultiScale(gray, faces);
- // T R A I N R E C O G N I Z E R
- face_train();
- int im_width = images[0].cols;
- int im_height = images[0].rows;
- model_fisher = createFisherFaceRecognizer();
- model_fisher->train(images, labels);
- Mat original = image1.clone();
- // look for all faces on the image
- for(unsigned int i = 0; i < faces.size(); i++)
- {
- Rect face_i = faces[i];
- Mat face = gray(face_i);
- Mat face_resized;
- resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
- int prediction = model_fisher->predict(face_resized);
- // Draw rectangle around faces
- rectangle(original , face_i, CV_RGB(0, 255,0), 1);
- //Point pt1(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
- //Point pt2(faces[i].x, faces[i].y);
- string box_text = format("Match found = %d", prediction);
- //string box_text = "Match Found: Alan Alban";
- // text position and output
- int pos_x = max(face_i.tl().x - 4, 0);
- int pos_y = max(face_i.tl().y - 4, 0);
- putText(original, box_text, Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
- }
- // S H O W I M A G E
- if ( r_c % 2 == 0 )
- imwrite("Detected_image1.jpeg", image1);
- else if ( r_c % 2 != 0 )
- imwrite("Detected_image2.jpeg", image1);
- r_c++;
- }
- model_fisher->train(images, labels);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement