Ask Your Question

blink's profile - activity

2020-07-27 05:39:31 -0600 received badge  Notable Question (source)
2019-11-13 09:52:22 -0600 received badge  Popular Question (source)
2017-08-20 06:42:48 -0600 received badge  Student (source)
2017-07-26 01:23:47 -0600 asked a question How do I store the captured image in OpenCV (saving the picture to computer)

I am still quite new to OpenCV and c++ programming in general. I am doing on a project that stores image from my webcam. I was able to display camera image and detect faces somehow but I don't know how to save the images.

What should i do in order for the faces that my webcam detects gets captured and store into my computer?

Using my code below, how can i edit it to:

  1. capture an image from the live cam 5 seconds after detecting face
  2. save the images to a folder in jpg format

Thanks so much for any help you can provide!

my code:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // capture from web camera init

    VideoCapture cap(0);
    cap.open(0);

    Mat img;

    // Initialize the inbuilt Harr Cascade frontal face detection
    // Below mention the path of where your haarcascade_frontalface_alt2.xml     file is located

    CascadeClassifier face_cascade;
    face_cascade.load("C:/OpenCV/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
    // i tried changing this line to match my folder in C Drive

    for (;;)
    {

        // Image from camera to Mat

        cap >> img;

        // Just resize input image if you want
        resize(img, img, Size(1000, 640));

        // Container of faces
        vector<Rect> faces;

        // Detect faces
        face_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(140, 140));
        // error message appears here

        //Show the results
        // Draw circles on the detected faces
        for (int i = 0; i < faces.size(); i++)
        {
            Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
            ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
        }

        // To draw rectangles around detected faces 
        /* for (unsigned i = 0; i<faces.size(); i++)
        rectangle(img,faces[i], Scalar(255, 0, 0), 2, 1);*/

        imshow("wooohooo", img);
        int key2 = waitKey(20);

    }

    return 0;
}
2017-07-26 01:20:46 -0600 asked a question How do I store the captured image in OpenCV (saving the picture to computer)

I am still quite new to OpenCV and c++ programming in general. I am doing on a project that stores image from my webcam. I was able to display camera image and detect faces somehow but I don't know how to save the images.

What should i do in order for the faces that my webcam detects gets captured and store into my computer?

Using my code below, how can i edit it to:

  1. capture an image from the live cam 5 seconds after detecting face
  2. save the images to a folder in jpg format

Thanks so much for any help you can provide!

my code:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    // capture from web camera init

    VideoCapture cap(0);
    cap.open(0);

    Mat img;

    // Initialize the inbuilt Harr Cascade frontal face detection
    // Below mention the path of where your haarcascade_frontalface_alt2.xml     file is located

    CascadeClassifier face_cascade;
    face_cascade.load("C:/OpenCV/sources/data/haarcascades/haarcascade_frontalface_alt2.xml");
    // i tried changing this line to match my folder in C Drive

    for (;;)
    {

        // Image from camera to Mat

        cap >> img;

        // Just resize input image if you want
        resize(img, img, Size(1000, 640));

        // Container of faces
        vector<Rect> faces;

        // Detect faces
        face_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(140, 140));
        // error message appears here

        //Show the results
        // Draw circles on the detected faces
        for (int i = 0; i < faces.size(); i++)
        {
            Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
            ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
        }

        // To draw rectangles around detected faces 
        /* for (unsigned i = 0; i<faces.size(); i++)
        rectangle(img,faces[i], Scalar(255, 0, 0), 2, 1);*/

        imshow("wooohooo", img);
        int key2 = waitKey(20);

    }

    return 0;
}
2017-07-25 20:51:24 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

I checked the lib files that i should type into the additional dependencies and found out that opencv_world.lib already contained all the files required. That is why i don't know any ways to solve the problem. Moreover, when i try to redo teh whole project with the same coding, i got a different error which says "Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol".

2017-07-25 20:44:17 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

I checked the lib files that i should type into the additional dependencies and found out that opencv_world.lib already contained all the files required. That is why i don't know any ways to solve the problem.

2017-07-25 02:09:41 -0600 received badge  Enthusiast
2017-07-21 04:26:19 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

I did try it with opencv_world320d.lib but it did not work for some reason.

2017-07-20 21:33:53 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

Now i got a link error that says cannot open file 'opencv_calib3d245d.lib' even though i did add the required files inside the Additional Dependencies under the Linker option. Is it because i added the wrong link path?

2017-07-20 21:33:32 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

Now i got a link error that says cannot open file 'opencv_calib3d245d.lib' even though i did add the required files inside the Additional Dependencies under the Linker option. Is it because i added the wrong path link?

2017-07-20 05:00:03 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

I used the codings from opencv website but it doesn't work for me. Do you need me to put up the codes to check by any chance?

2017-07-20 01:06:54 -0600 commented answer (Face Recognizer) error 0020: Identifier problems

I have already included the .hpp file in the "Additional Include Paths" of VS project. I did try to use #include "opencv2/face.hpp" before but it gave me more errors which include "unable to open source file". I have also tried using the full path which is "C:Users/Downloads/opencv_contrib-master/opencv_contrib-master/modules/face/include/opencv2/face.hpp"
as well but it also did not work

2017-07-19 20:28:48 -0600 asked a question (Face Recognizer) error 0020: Identifier problems

I am still quite new to OpenCV and i don't really know what functions am i suppose to call. Anyone can help by telling me what i should define? Thanks.

Error i got is:

identifier "createFisherFaceRecognizer" is undefined   
'createFisherFaceRecognizer'identifier not found

facerecognizer.cpp:

#include "opencv2/core.hpp"
#include "C:/OpenCV/build/include/opencv2/face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"

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

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

... line which got error:

    // Create a FaceRecognizer and train it on the given images:
    Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
2017-07-19 20:26:07 -0600 asked a question identifier "createFisherFace Recognizer" is undefined
#include "opencv2/core.hpp"
#include "C:\\Users\\Downloads\\opencv_contrib-master\\opencv_contrib-master\\modules\\face\\include\\opencv2\\face.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/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, 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()) {
            images.push_back(imread(path, 0));
            labels.push_back(atoi(classlabel.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/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);
    }
    // Get the height from the first image. We'll need this
    // later in code to reshape the images to their original
    // size AND we need to reshape incoming faces to this size:
    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);
    // That's it for learning the Face Recognition model. You now
    // need to create the classifier for the task of Face Detection.
    // We are going to use the haar cascade you have specified in the
    // command line arguments:
    //
    CascadeClassifier haar_cascade;
    haar_cascade.load(fn_haar);
    // Get a handle to the Video device:
    VideoCapture cap(deviceId);
    // Check if we can use this device at all:
    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;
        cvtColor(original, gray, CV_BGR2GRAY);
        // Find the faces in the frame:
        vector< Rect_<int> > faces;
        haar_cascade.detectMultiScale(gray, faces);
        // At this point you have the position of the faces in
        // faces. Now we'll get the faces, make a prediction and
        // annotate it in the video. Cool or what?
        for (int i = 0; i < faces.size(); i ...
(more)
2017-07-19 04:32:32 -0600 commented answer How can I get frames from my webcam ?

This code works for me too. I am using OpenCV 3.2.0 with visual-studio-2017

cap >> frame means the captured image will be sent to the frame which will open in a new window

2017-07-19 04:30:07 -0600 received badge  Supporter (source)
2017-07-19 01:38:55 -0600 commented answer OpenCv and Visual C++ Face detection

Thanks for the solution and also, i will heed your advice and look through docs. I actually did try and use codes from there before but there was error and so i kept on looking for alternatives, which is my biggest problem. I just don't understand how OpenCV works even though i read up the study materials.

2017-07-19 01:35:49 -0600 received badge  Scholar (source)
2017-07-18 23:49:07 -0600 received badge  Editor (source)
2017-07-18 23:39:25 -0600 asked a question OpenCv and Visual C++ Face detection

I recently came across this video which shows how to write a face recognition program and i tried using his codes to study how the program works. He indicated to change one of the lines and i followed as he said. I tried changing the line but it sill showed this error.

Assertion failed (!empty()) in cv::CascadeClassifier::detectMultiScale and
cv::Exception at memory location 0x000000000029EE10

Unhandled exception at 0x000007FEFD5CA06D in opencvTry.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000029EE10. occurred

My program - try.cpp:

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
    // capture from web camera init

    VideoCapture cap(0);
    cap.open(0);

    Mat img;

    // Initialize the inbuilt Harr Cascade frontal face detection
    // Below mention the path of where your haarcascade_frontalface_alt2.xml file is located

    CascadeClassifier face_cascade;
    face_cascade.load("C:\OpenCV\sources\data\haarcascades\haarcascade_frontalface_alt2.xml");    
    // i tried changing this line to match my folder in C Drive

    for (;;)
    {

        // Image from camera to Mat

        cap >> img;

        // obtain input image from source
        cap.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);

        // Just resize input image if you want
        resize(img, img, Size(1000, 640));

        // Container of faces
        vector<Rect> faces;


        // Detect faces
        face_cascade.detectMultiScale(img, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(140, 140));
        // error message appears here


        //Show the results
        // Draw circles on the detected faces

        for (int i = 0; i < faces.size(); i++)
        {
            Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);
            ellipse(img, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
        }

        // To draw rectangles around detected faces
        /* for (unsigned i = 0; i<faces.size(); i++)
        rectangle(img,faces[i], Scalar(255, 0, 0), 2, 1);*/


        imshow("wooohooo", img);
        int key2 = waitKey(20);

    }
    return 0;
}
2017-07-18 23:39:06 -0600 commented question Exception thrown at 0x000007FEFDB49E5D in CVpro1.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000000023F320.

make sure that your path to the lib is correct with the correct files included.