Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was wondering if anyone could help me out with this problem I'm having. I'm doing a face and eye detection using the webcam on my laptop.
I have two main classes that initialise the starting operation of opening the webcam and doing the detection :

FeaturesDetection class which implements JPanel and Runnable
This class has all the detection methods and override runnable
methods. Initially in my run() method I have this code below:

Java
CvCapture grabber = opencv_highgui.cvCreateCameraCapture(CAMERA_ID);
    		opencv_highgui.cvSetCaptureProperty(grabber,
    				opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
    		opencv_highgui.cvSetCaptureProperty(grabber,
    				opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);


Another class that implements JFrame and main method

When I run the project - the application will straight away load the webcam and the detection starts right away.

But now, I want to have an option in the beginning so user can either load a video file or choose webcam. So what I did was I added 2 buttons on jframe class
with actionlister attached to them. These two buttons then invoke their respective methods in the jpanel class.

Java
JButton camBut = new JButton("Webcam");
    camBut.addActionListener( new ActionListener() {
       public void actionPerformed(ActionEvent e)
       { FeaturesDetection.webcam();  }
    });

    JButton vidBut = new JButton("Load Video");
    vidBut.addActionListener( new ActionListener() {
       public void actionPerformed(ActionEvent e)
       {

           JFileChooser chooser = new JFileChooser();
           int returnValue = chooser.showOpenDialog( null ) ;
           File file = null;
            if( returnValue == JFileChooser.APPROVE_OPTION ) {
                   file = chooser.getSelectedFile() ;
            }
            if(file != null)
            {
                filePath = file.getPath();
            }

           FeaturesDetection.videoFile(filePath);  }
    });


What I changed was, before the run() method has the above code that displays the webcam
instead I made an if statements that checks if user chose video file button or webcam button.

So in my updated run() method I have the following:

Java
public void run()
   /*
    * display the current webcam image/vid */
   {

       // define a grabber
       if(webcam == true)
       {

           webcamCapture();
       }

       if(video == true)
       {

            vCapture();
       }


       if (grabber == null)
           return;

       long duration;
       isRunning = true;
       isFinished = false;

       while (isRunning) {
           long startTime = System.currentTimeMillis();

           img = cvQueryFrame(grabber);

           ……
           ………

       }
       // closeGrabber(grabber, CAMERA_ID);
       System.out.println("Execution End");
       isFinished = true;
   } // end of run()


In the loadwebcam() & loadVideo() I have :

Java
public void loadwebcam()
      {
          webcam = true;
      }
  public void videoFile(String file)
      {
          video = true;
          vidInput = file;
      }


And in the method vCapture() and webcamCapture()
I have:

Java
public CvCapture webcamCapture()
    {
        grabber = opencv_highgui.cvCreateCameraCapture(CAMERA_ID);
        opencv_highgui.cvSetCaptureProperty(grabber,
                opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
        opencv_highgui.cvSetCaptureProperty(grabber,
                opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);
        return grabber;
    }

    public CvCapture vCapture()
    {
        grabber = opencv_highgui.cvCreateFileCapture(vidInput);
          opencv_highgui.cvSetCaptureProperty(grabber,
                  opencv_highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);
          opencv_highgui.cvSetCaptureProperty(grabber,
                  opencv_highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);

          return grabber;
    }


The problem that I have is that, when I click any of the two buttons nothing happens/ when I click on webcam button no webcam loads up? I'm not sure what I should add to fix this?
Any help please.
Thank you!
Posted
Comments
SOHAM_GANDHI 21-Apr-14 9:19am    
what is CAMERA_ID value
whateverme19 23-Apr-14 4:22am    
the value is set to 0
SOHAM_GANDHI 21-Apr-14 9:19am    
it must be CAMERA_ID = 0 for first indexed webcam,
SOHAM_GANDHI 21-Apr-14 9:20am    
if having 2 or more cam to loaded CAMERA_ID = 0, CAMERA_ID_2 =1 like wise

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900