可选择视频文件输入或使用网络摄像头进行人脸检测(Java / JavaCV) [英] Face detection with an option of video file input or using webcam (Java/JavaCV)
问题描述
我想知道是否有人可以帮助我解决我遇到的这个问题。我正在使用笔记本电脑上的网络摄像头进行面部和眼睛检测。
我有两个主要课程,初始化打开网络摄像头的开始操作并进行检测:
FeaturesDetection类实现JPanel和Runnable
这个类具有所有检测方法并覆盖runnable
方法。最初在我的run()方法中,我有以下代码:
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);
另一个实现JFrame和main方法的类
当我运行项目时 - 应用程序将立即加载网络摄像头并开始检测马上就好了。
但是现在,我想在开头有一个选项,这样用户可以加载视频文件或选择网络摄像头。所以我做的是在jframe类上添加了2个按钮
并附加了actionlister。这两个按钮然后在jpanel类中调用它们各自的方法。
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); }
});
我改变的是,在run()方法之前有上面显示网络摄像头的代码
而不是我做了一个if语句来检查用户是否选择了视频文件按钮或网络摄像头按钮。
所以在我更新的run()方法中我有以下内容:
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()
/*
* 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()
在loadwebcam()& loadVideo()我有:
In the loadwebcam() & loadVideo() I have :
public void loadwebcam()
{
webcam = true;
}
public void videoFile(String file)
{
video = true;
vidInput = file;
}
方法vCapture()和webcamCapture()
我有:
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!
推荐答案
这篇关于可选择视频文件输入或使用网络摄像头进行人脸检测(Java / JavaCV)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!