wrting to csv file in c++ fails [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Im working with this code which is supposed to detect faces. The code works totally fine, but as soon as I try to insert three lines for writing to csv file, it breaks down with lengthy error of some 100+ lines -ERROR FILE LOG.
This code was taken from :- https://github.com/shunyaos/shunyaface
// Header file for Face-Recognition/Detection
#include "shunyaface.h"
#include "opencv2/opencv.hpp"
#include <bits/stdc++.h>
#include "fstream"
using namespace std;
using namespace cv;
int main(int argc, char** argv){
// Create instance of class FaceRec
std::ofstream filename("test.csv");
filename<< "TESTING CSV WRITE";
FaceRec facerec;
Mat frame;
Mat frame2;
clock_t start, end; //This will hold the start and end-time
int count = 0; //Variable which hold the number of frames elapsed
VideoCapture cap(0);
time(&start);
while(1)
{
// Capture a frame
cap >> frame;
// Pass the frame to the detect function which will return the frame with a bounding-box on the face and points on the lips and eyes
frame2 = facerec.detect(frame);
count++; //Increment count
// Display the frame to the user
imshow("face-detect", frame2);
if(waitKey(1) == 'q')
break;
}
time(&end); // Stop the time
cout<< "Output FPS is:"<<count/(end-start)<<endl; //Display Output-FPS
filename.close();
return 0;
}
So basically as shown above,after inclusion of these lines the code is breaking :-
std::ofstream filename("test.csv");
filename<< "TESTING CSV WRITE";
filename.close()

You forget to end this line cout<< "In while"<<.
Somewhere in your code is this snippet
out<< "In while"<<
// Capture a frame
~~~~~~~~~~~~~~~~~~
cap >> frame;
You should try to fix error from top to bottom. The first error is regarding cout, operator<< and cv::VideoCapture cap.

Related

OpenCV memory leak using cvCreateFileCapture and cvQueryFrame

I am new to OpenCV (OpenCV 3.2 / opencv_ffmpeg320_64.dll / Windows 10 / Visual Studio 2017) and wrote a program dealing with a video stream of a webcam. Unfortunately the program has a memory leak. After hours of searching and googling I managed to break down the problem to the following minimal example:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <thread>
CvCapture *capture;
IplImage *frame;
int main(int argc, char **argv)
{
capture = cvCreateFileCapture("http://192.168.1.123:8080/CamStream");
while (true)
{
cvWaitKey(1);
frame = cvQueryFrame(capture);
if (frame)
{
std::cout << "New image" << std::endl;
}
}
return 0;
}
As you can see I am capturing of a simple HTTP stream. After the capture is created new frames are received. Unfortunately the task manager shows a never stopping increase of memory:
What could cause this problem and what are possible approaches to solve it?

OpenCV VideoCapture working properly only after breakpoint

I'm currently using OpenCV 2.3.1 with Visual Studio 2008. I'm trying to read the frames from a Hauppauge Usb Live-2 using VideoCapture, but I'm ran into a strange issue. Below is the relevant part of my code:
VideoCapture vc(0);
if (!vc.isOpened()) return -1;
Mat frame;
namedWindow("Camera");
bool success;
while (true)
{
success = vc.read(frame);
if (!success) continue;
imshow("Camera", frame);
if (waitkey(30) == 27) break;
}
Initially, when running my code in debug mode, the window displaying the captured frames shows only a solid gray image. Attempting to debug my program, I placed breakpoint a breakpoint at the start of my code and stepped through each line. At imshow, however, the window started displaying the grabbed frames properly, showing what was captured by my camera. Subsequently, I realized that so long as I enter a breakpoint between opening my device and displaying it on the window, the frames will start showing up properly.
Does anyone have any idea how entering a breakpoint may affect the execution of a program in debug mode (in this case allowing the VideoCapture object to start reading the frames properly)?
Note: Running the executable gave no problems either, so I'm posting this question out of curiosity.
I believe your code is trying to display the image (which is empty) before your camera gets ready. Try to slow down for one or two seconds, by first include files like:
#include <chrono>
#include <thread>
Then before your while statement, add this line:
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
If you are using C++ with lower version than 11, then the sleep_for method might be different. Take a reference here.
The camera has an initialisation period so you need to check for empty frames.
Now there are two options, you could do what #Derman has said and put in a wait but how do you know how long you need to wait for?
Or you can check for empty frames and only show the window if they are not empty
VideoCapture vc(0);
if ( !vc.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
Mat frame;
namedWindow("Camera");
bool success;
while (true)
{
vc.read(frame);
if(frame.empty()){
std::cerr<<"frame is empty"<<std::endl;
break;
}
imshow("Camera", frame);
if (waitkey(30) == 27) break;
}
I don't see any reason why this code shouldn't start showing the frames once they are avaliable from the camera

OpenCV: Reading the frames of a video sequence

Anyone help me ,I am trying to run code to read frames from video in folder its success in building but when debugging there isn't any output
* I am using Visual studio 2012 ,opencv 2.4.11 version
the code is :
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
int main()
{
// Open the video file
cv::VideoCapture capture("C:/Users/asus/Desktop/A.mp4");
// check if video successfully opened
if (!capture.isOpened())
return 1;
// Get the frame rate
int rate= capture.get(CV_CAP_PROP_FPS);
bool stop(false);
cv::Mat frame; // current video frame
cv::namedWindow("Extracted Frame");
// Delay between each frame in ms
// corresponds to video frame rate
int delay= 1000/rate;
// for all frames in video
while (!stop) {
// read next frame if any
if (!capture.read(frame))
break;
cv::imshow("Extracted Frame",frame);
// introduce a delay
// or press key to stop
if (cv::waitKey(delay)>=0)
stop= true;
}
// Close the video file.
// Not required since called by destructor
capture.release();
}
Your main() function is never executed. The only thing, that gets executed is _tmain(), which does nothing and returns immediately.
I haven't done much Windows programming in a while, but if I remember correctly this is how it works:
When Unicode is enabled for your compiler
int _tmain(int argc, _TCHAR* argv[])
gets compiled as
int wmain(int argc, wchar * argv[])
which is then used as the program entry point.
Since you seem not to be using any Windows-APIs in your code I would ignore the Microsoft specific way of doing multibyte character strings, which is non-portable, and simply use plain ASCII strings as you did in the main() function, that you intended to use.
So to solve your problem simply throw out the _tmain() function. Maybe you also need to disable Unicode in your project settings if you get linker errors.

C++, OpenCV : Assertion failed in Resize

As a C++ beginner, I am currently facing a problem I somehow can't solve, even if the code is pretty simple.
I've been searching for answers all over the Internet, but none was applicable for my problem.
I am currently coding basic SVMs with C++, under VS2013, using OpenCV 2.4.8.
I was able to work on images of same size, specifying fixed height, width at the beginning of my code.
Now, I'm trying to : open images of different sizes, resize them to a certain lower size, and apply the previous code to the now-resized dataset. Simple as that.
Here's the beginning of my code :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/ml/ml.hpp>
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <sstream>
#include <windows.h>
using namespace cv;
using namespace std;
int main(){
Input parameters are :
int Nb_Data_Class_1 = 10;
int Nb_Data_Class_0 = 5;
int Height_Zone = 200;
int Width_Zone = 200;
so I will resize all my files to 200x200 format.
string Path = "C:\\Users\\....";
string Format = ".jpg";
int Nb_Files = Nb_Data_Class_1 + Nb_Data_Class_0;
Mat TrainingMat(Nb_Files, Zone_Image, CV_32FC1);
Mat TrainingLabels(Nb_Files, 1, CV_32FC1);
For every file of the class labelled {1} - they are all named Tree01, Tree02, etc. - I open, and resize.
for (int i = 0; i < Nb_Data_Class_1; ++i)
{
stringstream ss;
ss << Path << "\\Tree0" << i + 1 << Format;
Mat Image = cv::imread(ss.str(), 0);
resize(Image, Image, Size(Width_Zone, Height_Zone));}
Things worked perfectly without the last line. I had a Mat array, filled with 0-t0-255 numbers. Now, I get the following error :
OpenCV Error: Assertion failed <ssize.area<> >0> in cv::resize,
file C:\builds\2-4-PackSlave-win32-vc12-shared\opencv\modules\imgproc\serc\imgwarp.cpp, line 1824
What could be the problem ?
I thought that maybe OpenCV wasn't properly opening the files ; but, in that case, how everything could have been previously working ?
Still wondering.
Any help would be much appreciated ! Thanks in advance.
The only reason for resize to crush is absence of Image. Even if you checked that some of the images were read properly it doesn't mean that all of them were - some of them may be missing. Reading files from disk is a very common point of failure for programs because you never can be sure if the read was successfully or not. As a result every time you read an image you really really should verify that it is not empty:
if (Image.cols == 0) {
cout << "Error reading file " << ss << endl;
return -1;
}
Not going to solve the problem in this case, but this assertion can also be caused by trying to resize a Mat with a signed type like CV_8SC3. For example:
Mat wrong = Mat::zeros(4, 4, CV_8SC3); // <- Notice 'S'
Mat right = Mat::zeros(4, 4, CV_8UC3); // <- Notice 'U'
imshow("OK", right);
imshow("ASSERTS", wrong);
Note that checking wrong.cols != 0 will not prevent this from crashing.
Your line:
ss << Path << "\Tree0" << i + 1 << Format;
will produce (where i=0):
"C:\Users\....\Tree01.jpg".
Solution
Change "string Path = "C:\Users\....";" line to:
string Path = "C:\Users";
and
change "ss << Path << "\Tree0" << i + 1 << Format;" line to:
ss << Path << "Tree0" << i + 1 << Format;

OpenCV capture loops video/Does not detect last frame

I am capturing an avi file and processing it. My code has worked for sometime without problem but now it does not seem to stop after the last frame of the video is captured. Instead it keeps looping back to the beginning of the video. I do not understand why this is happening and I can not think of anything changing with regards to Eclipse or OpenCV. I have tried the same code on my Ubuntu pc with the same video and it works without problems. I have even tried as much as reinstalling the OS and apps without success.
Sample code:
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat frame;
VideoCapture capture;
const string inputVideo = argv[1];
char buff[PATH_MAX];
getcwd( buff, PATH_MAX );
std::string fileName( buff );
fileName.append("/");
fileName.append(inputVideo);
capture.open(inputVideo);
while(true)
{
capture >> frame;
if(!frame.empty())
{
imshow("frame", frame);
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
int key = waitKey(10);
if((char)key == 'c')
{
break;
}
}
return 0;
}
I am running this on a Mac OS X (10.8.2), Eclipse Juno, and OpenCV 2.4.3.
Any advice or comments are appreciated. Thanks in advance
The solution that I used was posted as a comment by #G B. I am creating a solution so that it may be marked as one.
I used capture.get(CV_CAP_PROP_POS_FRAMES) before and after frame grabbing, if the value "after" is less than the value "before", then I've reached the end of the video.
Get the frame count like below,
int frameCnt = capture.get(CV_CAP_PROP_FRAME_COUNT);
And check to exit the loop when the frame count exceeds..