Ask Your Question

CLeBeR's profile - activity

2018-11-07 04:48:58 -0600 received badge  Popular Question (source)
2016-10-28 15:54:18 -0600 received badge  Organizer (source)
2016-10-28 15:53:46 -0600 asked a question FisherFace detect me even if my image is note in database

System information (version) - OpenCV => 2.4.9.1 - Operating System / Platform => Linux Ubuntu 64 Bit - Compiler => CMake 3.5.1

Detailed description

I use imgshow to prompt the webcam capture and add a rectangle and the subject found with FisherFace algorithm AT&T orl_face photo base. The problem is FisherFace algorithm detect me even if I am not in the database, it confuses me with 2 subjects... I changed the minNeighbours parameter but it doesn't change anything.

Thanks to the FaceRecognizer() Thresholds documentation, I updated the code :

// Create a FaceRecognizer and train it on the given images:
Ptr<FaceRecognizer> model = createFisherFaceRecognizer(10,0.0);
model->train(images, labels);
// The following line reads the threshold from the Eigenfaces model:
double current_threshold = model->getDouble("threshold");
// And this line sets the threshold to 0.0:
model->set("threshold", 0.0);
.
.
.
haar_cascade.detectMultiScale(gray, faces, 1.1, 5,0, cvSize(30,30), cvSize(30,30));
.
.
.
Mat face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
// Now perform the prediction, see how easy that is:
int prediction = model->predict(face_resized);
// And finally write all we've found out to the original image!
// First of all draw a green rectangle around the detected face:
cout << prediction << endl;

(See full code just below)

Steps to reproduce

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat m = imread(path, 1);
            if (m.empty())
            {
                cerr << path << " could not be read." << endl;
                continue;
            }
            Mat m2;
            cvtColor(m,m2,CV_BGR2GRAY);
            images.push_back(m2);
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
    cout << endl << "Read finish";
}

int main(int argc, const char *argv[]) {
    if (argc != 4) {
        cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- 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);
    }
    string fn_haar = string(argv[1]);
    string fn_csv = string(argv[2]);
    int deviceId = atoi(argv[3]);
    vector<Mat> images;
    vector<int> labels;
    try {
        read_csv(fn_csv, images, labels);
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        // nothing more we can do
        exit(1);
    }
    int im_width = images[0].cols;
    int im_height = images[0].rows;
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer(10,0.0);
    model->train(images, labels);
    double current_threshold = model->getDouble("threshold");
    model->set("threshold", 0.0);
    CascadeClassifier haar_cascade;
    haar_cascade.load(fn_haar ...
(more)
2016-10-28 15:53:35 -0600 asked a question Webcam capture and imgshow low FPS

System information (version) - OpenCV => 2.4.9.1 - Operating System / Platform => Linux Ubuntu 64 Bit - Compiler => CMake 3.5.1

Detailed description

I use imgshow to prompt the webcam capture and add a rectangle and the subject found with FisherFace algorithm AT&T orl_face photo base. The problem is the program use 80% of CPU (i3) and the FPS of camera are very slow.

EDIT : I tweak the detectMultiScale parameters (flag,minSize,maxSize). It's the maxSize which change the FPS of camera. But, if I change it, there is no prediction (even if I change the minNeighbors parameter).

Steps to reproduce

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat m = imread(path, 1);
            if (m.empty())
            {
                cerr << path << " could not be read." << endl;
                continue;
            }
            Mat m2;
            cvtColor(m,m2,CV_BGR2GRAY);
            images.push_back(m2);
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
    cout << endl << "Read finish";
}

int main(int argc, const char *argv[]) {
    if (argc != 4) {
        cout << "usage: " << argv[0] << " </path/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- 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);
    }
    string fn_haar = string(argv[1]);
    string fn_csv = string(argv[2]);
    int deviceId = atoi(argv[3]);
    vector<Mat> images;
    vector<int> labels;
    try {
        read_csv(fn_csv, images, labels);
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        // nothing more we can do
        exit(1);
    }
    int im_width = images[0].cols;
    int im_height = images[0].rows;
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
    model->train(images, labels);
    CascadeClassifier haar_cascade;
    haar_cascade.load(fn_haar);
    VideoCapture cap(deviceId);
    if(!cap.isOpened()) {
        cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
        return -1;
    }
    Mat frame;
    for(;;) {
        cap >> frame;
        Mat original = frame.clone();
        Mat gray;
        if(original.empty()){
            cout << "An empty matrice has been detected" << endl;
            break;
        }
        else if(original.channels()>1){
            cout << "Matrice has been converted";
            cvtColor(original, gray, CV_BGR2GRAY);
        }
        else gray = original;
        vector< Rect_<int> > faces;
        haar_cascade.detectMultiScale(gray, faces, 1.1, 3);
        for(int i = 0; i < faces.size(); i++) {
            Rect face_i = faces[i];
            Mat face = gray(face_i);
            Mat face_resized;
            cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
            int prediction = model->predict(face_resized);
            rectangle(original, face_i, CV_RGB(0, 255,0), 1);
            string box_text = format("Prediction = %d", prediction);
            int pos_x = std::max(face_i.tl().x - 10 ...
(more)
2016-10-03 13:46:56 -0600 commented question Fisherface is very slow

If I change minNeighbours, haar_cascade.detectMultiScale(gray, faces, 1.1, 3); I have the same problem. The difference is in the speed of detection but there is also errors.

2016-09-28 12:32:16 -0600 received badge  Enthusiast
2016-09-25 13:53:39 -0600 commented question Fisherface is very slow

Hello, I just deleted the GUI part to try and there is also subject detection problems. Even if there is no face on the cam, subjects are detected.

2016-09-22 10:58:48 -0600 asked a question Fisherface is very slow

Hello, I am using the fisherface algorithm explained in this tutorial , it works but the GUI is very slow. I think it affects the facial recognition of subjects because if I move my hand for example, It detect a subject from this database. Even if I don't put my person in the database, the problem of slow GUI is present.

I remove the GUI part to try if there is also detection's problems and it is. Even if there is no face on the cam, subjects are detected. This is the code :

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat m = imread(path, 1);
            if (m.empty())
            {
                cerr << path << " could not be read." << endl;
                continue;
            }
            Mat m2;
            cvtColor(m,m2,CV_BGR2GRAY);
            images.push_back(m2);
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
    cout << endl << "Read finish";
}

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/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- 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;
    // Read in the data (fails if no valid input filename is given, but you'll get an error message):
    try {
        read_csv(fn_csv, images, labels);
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        // nothing more we can do
        exit(1);
    }
    int im_width = images[0].cols;
    int im_height = images[0].rows;
    // Create a FaceRecognizer and train it on the given images:
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
    model->train(images, labels);
    CascadeClassifier haar_cascade;
    haar_cascade.load(fn_haar);
    VideoCapture cap(deviceId);
    if(!cap.isOpened()) {
        cerr << "Capture Device ID " << deviceId << "cannot be opened." << endl;
        return -1;
    }
    // Holds the current frame from the Video device:
    Mat frame;
    for(;;) {
        cap >> frame;
        // Clone the current frame:
        Mat original = frame.clone();
        // Convert the current frame to grayscale:
        Mat gray;
        if(original.empty()){
            cout << "An empty matrice has been detected" << endl;
            break;
        }
        else if(original.channels()>1){
            cout << "Matrice has been converted" << endl;
            cvtColor(original, gray, CV_BGR2GRAY);
        }

        else gray = original;
        // Find ...
(more)
2016-09-08 05:21:06 -0600 received badge  Editor (source)
2016-09-07 13:47:54 -0600 commented question imread can't read my png

I already test it and it doesn't solve the problem.

2016-09-07 12:36:00 -0600 commented question imread can't read my png

It was a test to see if there was the same error with double g and it is. So unfortunally is not the problem.

2016-09-07 12:13:05 -0600 asked a question imread can't read my png

Hello, I would like to follow this tutorial but in the read CSV function, I have a problem with imread. Normally, a Mat should be populate by thanks to imread function but it's empty for each read attempt. This is my code :

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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;
    int numberImageReaded = 0;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat m = imread(path, 1);
            if (m.empty())
            {
                cerr << path << " could not be read." << endl;
                continue;
            }
            Mat m2;
            cout << endl << "New image read";
            cvtColor(m,m2,CV_BGR2GRAY);
            numberImageReaded++;
            cout << endl << "Number of image read = " << numberImageReaded;
            images.push_back(m2);
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
    cout << endl << "Read finish";
}


int main() {
    string fn_csv = string("data.csv");
    vector<Mat> images;
    vector<int> labels;
    try {
        read_csv(fn_csv, images, labels);
    } catch (cv::Exception& e) {
        cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
        exit(1);
    }
    return 0;
}

I read the documentation and for my case (Ubuntu 16.04), I should install libjpeg-de. I install it but it doesn't solve the problem. I need to add the flag OPENCV_BUILD_3RDPARTY_LIBS but I don't know where I should add it. I compile with this command :

clang++ -std=c++11 main.cpp -o w pkg-config --cflags --libs opencv

This is the result when I launch the program :

user@user-PC:~/Bureau/readCSVopenCV$ ./w ��./S1/27.png could not be read. ./S1/12.png could not be read. ./S1/9.png could not be read. ...... ...... ./S1/6.png could not be read. ./S1/20.png could not be read. could not be read. Read finish

I create an archive with all the files so you will can reproduce the problem. (I replaced my photos by George Clooney as in the tutorial). http://www.filedropper.com/readcsvopencv

2016-09-06 14:12:19 -0600 commented answer Assertion error in cvtColor

No idea how to solve the problem?

2016-09-04 08:42:58 -0600 commented answer Assertion error in cvtColor

Hello, thanks for your response. Effectively, for each picture, I have this result in the console : /home/user/project/S1/12.png could not be read. It was a JPG at origin but I convert it in PNG to use with OpenCV. I just installed libjpeg-dev as said in the documentation but I have the same problem, I don't know what is the OPENCV_BUILD_3RDPARTY_LIBS flag in CMake. I compile with this command : clang++ -std=c++11 main.cpp -o w pkg-config --cflags --libs opencv Thanks a lot !

2016-09-04 07:37:46 -0600 asked a question Assertion error in cvtColor

Hello, I use the Face Recognition's tutorial with OpenCV in C++ avaible on the official documentation. I have no problem during the compilation but when I run the program I get the following error:

OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, line 3737 New image readError opening file "data.csv". Reason: /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtColor

I read a lot of topics about this problem but I don't arrive to solve it. I put some cout in the code to see where the program crash. The problem is in this line :

cvtColor(m,m2,CV_BGR2GRAY);

This is the complete code :

    /*
 * 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.
 *
 *   See <http://www.opensource.org/licenses/bsd-license>
 */

#include "opencv2/core/core.hpp"
#include "opencv2/contrib/contrib.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"

#include <iostream>
#include <fstream>
#include <sstream>

using namespace cv;
using namespace std;

static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, 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;
    int numberImageReaded = 0;
    while (getline(file, line)) {
        stringstream liness(line);
        getline(liness, path, separator);
        getline(liness, classlabel);
        if(!path.empty() && !classlabel.empty()) {
            Mat m = imread(path, 1);
            Mat m2;
            cout << endl << "New image read";
            cvtColor(m,m2,CV_BGR2GRAY);
            numberImageReaded++;
            cout << endl << "Number of image read = " << numberImageReaded;
            images.push_back(m2);
            labels.push_back(atoi(classlabel.c_str()));
        }
    }
    cout << endl << "Read finish";
}

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/to/haar_cascade> </path/to/csv.ext> </path/to/device id>" << endl;
        cout << "\t </path/to/haar_cascade> -- Path to the Haar Cascade for face detection." << endl;
        cout << "\t </path/to/csv.ext> -- 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 ...
(more)