从摄像头捕获视频时,JavaCV不会打开另一个JFrame

时间:2017-12-23 18:45:43

标签: java swing opencv javacv

我有PyRecognizer类,它扩展了一个JFrame,并且有方法" captura()":

public void captura() throws FrameGrabber.Exception{
    int nAmostras = 25, amostras = 1;
    KeyEvent tecla = null;
    OpenCVFrameConverter.ToMat convertMat = new OpenCVFrameConverter.ToMat();
    camera = new OpenCVFrameGrabber(0);

    CascadeClassifier detectorFace = new opencv_objdetect.CascadeClassifier("/home/tallys/NetBeansProjects/PyRecognizer/src/utils/haarcascade-frontalface-alt.xml");

    camera.start();

    CanvasFrame cFrame = new CanvasFrame("Video",CanvasFrame.getDefaultGamma()/camera.getGamma());
    Frame frameCapturado = null;
    Mat imagemColorida = new Mat();
    while((frameCapturado=camera.grab())!=null){
        imagemColorida = convertMat.convert(frameCapturado);
        imagemCinza = new Mat();
        cvtColor(imagemColorida,imagemCinza,opencv_imgproc.COLOR_BGRA2GRAY);
        RectVector facesDetectadas = new RectVector();
        detectorFace.detectMultiScale(imagemCinza, facesDetectadas,1.1,1,0,new Size(100,100),new Size(500,500));
        for(int i=0;i<facesDetectadas.size();i++){
            dadosFace = facesDetectadas.get(0);
            rectangle(imagemColorida,dadosFace, new Scalar(0,0,255,0));
        }
        if(cFrame.isVisible())
            cFrame.showImage(frameCapturado);
    }
    cFrame.dispose();
    camera.stop();

}

从网络摄像头捕获视频并检测面部。但我有另一个名为&#34; Painel&#34;具有main方法并从PyRecognizer(captura()方法的所有者)扩展,构造函数调用PyRecognize.class的方法。

public Painel() throws FrameGrabber.Exception, InterruptedException {
    initComponents();
    captura();
}

但是当我运行应用程序时,唯一打开的框架是捕获类的画布,我的名为Painel的JFrame无法打开。我认为问题是while循环从相机获取图像。

我已经尝试了线程,并在使用captura()方法的画布后暂停代码以等待屏幕打开。

0 个答案:

没有答案