Ask Your Question

How to tag persons in an image in OpenCV?

asked 2014-10-26 21:42:31 -0600

Sowmya gravatar image

I am working on the problem of face recognition. I need a help to display, the name of that person in the image by tagging(Display the name of the person by performing face recognition). Can anyone help me with this?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-10-27 05:04:50 -0600

updated 2014-10-27 05:05:26 -0600

Hi, I would decompose the problem in two steps : create a learning database of the person you want to tag, and the recognition step. For the first step, you will first have to create a database containing multiple view of the face of each person you want to recognize. That database will consist of a list of pictures with the corresponding name. So to sum up:

vector<Mat> images;
vector<int> labels;
vector<string> names;
images = [pic1;pic2;pic3;pic4;...]
labels = [0   ;0   ;1   ;2   ;...]
names  = [nom1;nom2;nom3]

In this example, you have 3 persons, with some pictures of them. labels correspond to the index in the names vector.

You can then use a classifier (I suggest you the LBP face detector):

Ptr<FaceRecognizer> model =  createLBPHFaceRecognizer();
model->train(images, labels);

Now model contains everything you need to tag peoples. To get the class, you just have to do that:

int predicted = model->predict(askingImg);
cout<<"Hello "<<names[predicted]<<"!"<<endl;
edit flag offensive delete link more


Thanks for your suggestion. I tried the following code for face recognition in videos And I made necessary changes according to your answer to display names in the program. But I failed to do it. When I say this int predicted = model->predict(askingImg); cout<<names[prediction]<<endl; nothing is displayed in the command window.

Please let me know if there is a way to tag people in a video.

Sowmya gravatar imageSowmya ( 2014-11-14 03:07:57 -0600 )edit

answered 2014-11-14 03:01:28 -0600

Sowmya gravatar image

I actually tried with your answer. But the name of the person is not getting displayed in the video. I am now trying to display names of person in a video. For that I am using the code from the following link

and I tried your answer to display the names of the persons in the video. But it is failing. Below is the code //g++ -ggdb pkg-config --cflags opencv -o basename online_faceRec_video.cpp .cpp online_faceRec_video.cpp pkg-config --libs opencv

/* * Copyright (c) 2011. Philipp Wagner <bytefish[at]gmx[dot]de>. * Released to public domain under terms of the BSD Simplified license.

  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions are met:
    • Redistributions of source code must retain the above copyright
  • notice, this list of conditions and the following disclaimer.
    • Redistributions in binary form must reproduce the above copyright
  • notice, this list of conditions and the following disclaimer in the
  • documentation and/or other materials provided with the distribution.
    • Neither the name of the organization nor the names of its contributors
  • may be used to endorse or promote products derived from this software
  • without specific prior written permission.

include "/usr/local/include/opencv2/core/core.hpp"

include "/usr/local/include/opencv2/contrib/contrib.hpp"

include "/usr/local/include/opencv2/highgui/highgui.hpp"

include "/usr/local/include/opencv2/imgproc/imgproc.hpp"

include "/usr/local/include/opencv2/objdetect/objdetect.hpp"

include <iostream>

include <fstream>

include <sstream>

using namespace cv; //using namespace cv::face; using namespace std;

static void read_csv(const string& filename, vector<mat>& images, vector<int>& labels, vector<string>& names, char separator = ';') { std::ifstream file(filename.c_str(), ifstream::in); if (!file) { string error_message = "No valid input file was given, please check the given filename."; CV_Error(CV_StsBadArg, error_message); } string line, path, classlabel,classnames; while (getline(file, line)) { stringstream liness(line); getline(liness, path, separator); getline(liness, classlabel); getline(liness, classnames); if(!path.empty() && !classlabel.empty()) { images.push_back(imread(path, 0)); labels.push_back(atoi(classlabel.c_str())); names.push_back(classnames.c_str()); } } } int main(int argc, const char *argv[]) { // Check for valid command line arguments, print usage // if no arguments were given. if (argc != 4) { cout << "usage: " << argv[0] << " </path> </path> </path>" << endl; cout << "\t </path> -- Path to the Haar Cascade for face detection." << endl; cout << "\t </path> -- Path to the CSV file with the face database." << endl; cout << "\t <device id=""> -- The webcam device id to grab frames from." << endl; exit(1); } // Get the path to your CSV: string fn_haar = string(argv[1]); string fn_csv = string(argv[2]); int deviceId = atoi(argv[3]); // These vectors hold the images and corresponding labels: vector<mat> images; vector<int> labels; vector<string> names;

try {
    read_csv(fn_csv, images, labels,names);
} catch (cv::Exception& e) {
    cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;        

int im_width = images[0].cols;
int im_height = images[0].rows;
Ptr<FaceRecognizer> model = createFisherFaceRecognizer ...
edit flag offensive delete link more

Question Tools



Asked: 2014-10-26 21:42:31 -0600

Seen: 2,050 times

Last updated: Nov 14 '14