Ask Your Question

Simon Tang's profile - activity

2016-07-09 08:26:40 -0600 received badge  Student (source)
2013-12-19 13:07:12 -0600 received badge  Supporter (source)
2013-12-18 08:16:03 -0600 received badge  Organizer (source)
2013-12-18 08:15:27 -0600 asked a question VideoCapture::grab() hangs

I have a two threads, one is the main loop and the other handle server requests. When the camera is being used [by Skype], it is being hanged on the noted line.

However, if this was all in one function, this wouldn't hang. Can anyone give me an explanation why that is so or what am I doing wrong? Thank you.

VideoCapture cap;
void mainloop() {
  cap.open(this->currentCaptureCamera);

  if (!cap.isOpened())
    return;

  ...

  while(isRunning){
    ...
  }
}

void snapshot_callback() {
  if (!cap.isOpened()) {
    cap.open(this->currentCaptureCamera);
  }
  cap >> latest; // Hangs here.
  ...
}
2013-11-14 14:56:10 -0600 asked a question VideoWriter Not Working on Some Macintosh Machines

I am trying to capture the video from the camera onto disk. I have a script that works on a Retina MacBook Pro 18-inch, Late 2013 but does not work on MacBook Pro 13-inch, Mid 2010. Both have similar environments: running on Maverick and have OpenCV 2.4.7 installed. However, I cannot get the older Macbook to write to disk correctly. The file size of output remains constant (414 bytes) and does not grow.

What is the problem that I'm having? What should I look into or check?

#include "opencv2/opencv.hpp"
using namespace cv;
#define fps 15

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    VideoWriter m_vw;

    bool m_isRecording = true;


    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);

    int skip = 30;
    int n=0;
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        std::cout << "skip: " << skip << std::endl;
        if (frame.empty()) {
            if(frame.rows != 480 && frame.cols != 640){ exit(0); }
            std::cout << "no frame" << std::endl;
            if (cap.grab()) {
                std::cout << "grab" << std::endl;
                if (cap.retrieve(frame)) {
                    imshow("edges", frame);
                }

            }
        } else if (skip-- > 0) {
            std::cout << "hi" << std::endl;
        } else {
            edges = frame;

            double width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
            double height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);

            if (m_isRecording && !m_vw.isOpened()) {
                m_vw = cv::VideoWriter("~/ctrythis.avi", CV_FOURCC('M','J','P','G'), fps, cv::Size(width,height), true);
                std::cerr << m_vw.isOpened()<<std::endl;
            }
            putText( edges, "caption",
                    cv::Point( width/4, height/2),
                    CV_FONT_HERSHEY_COMPLEX, 1, cv::Scalar(255, 255, 255) );
            imshow("edges", edges);

            std::cerr << n++ << " " <<  m_vw.isOpened() << std::endl;
            m_vw << edges;
        }

        if(waitKey(1000/fps) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}