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:
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.
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:
public void run()
{
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);
……
………
}
System.out.println("Execution End");
isFinished = true;
}
In the loadwebcam() & loadVideo() I have :
public void loadwebcam()
{
webcam = true;
}
public void videoFile(String file)
{
video = true;
vidInput = file;
}
And in the method vCapture() and webcamCapture()
I have:
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!