Ask Your Question

Revision history [back]

Help! How to Detect video image in real-time? Using OpenCV?

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

 package opencv;
 import org.opencv.highgui.HighGui; 
 import java.io.ByteArrayInputStream;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import javafx.application.Application;
 import javafx.event.ActionEvent;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 import org.opencv.core.Core;
 import org.opencv.core.Mat;
 import org.opencv.core.MatOfByte;
 import org.opencv.videoio.VideoCapture;
 import org.opencv.videoio.Videoio;
 import org.opencv.imgcodecs.Imgcodecs;
 import org.opencv.core.MatOfRect;
 import org.opencv.core.Point;
 import org.opencv.core.Rect;
 import org.opencv.core.Scalar;
 import org.opencv.imgproc.Imgproc;
 import org.opencv.objdetect.CascadeClassifier;

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image("Detection.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}

click to hide/show revision 2
retagged

updated 2018-05-15 03:35:11 -0600

berak gravatar image

Help! How to Detect video image in real-time? Using OpenCV?

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

 package opencv;
 import org.opencv.highgui.HighGui; 
 import java.io.ByteArrayInputStream;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import javafx.application.Application;
 import javafx.event.ActionEvent;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 import org.opencv.core.Core;
 import org.opencv.core.Mat;
 import org.opencv.core.MatOfByte;
 import org.opencv.videoio.VideoCapture;
 import org.opencv.videoio.Videoio;
 import org.opencv.imgcodecs.Imgcodecs;
 import org.opencv.core.MatOfRect;
 import org.opencv.core.Point;
 import org.opencv.core.Rect;
 import org.opencv.core.Scalar;
 import org.opencv.imgproc.Imgproc;
 import org.opencv.objdetect.CascadeClassifier;

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image("Detection.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}

Help! How to Detect video image in real-time? Using OpenCV?

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

 package opencv;
 import org.opencv.highgui.HighGui; 
 import java.io.ByteArrayInputStream;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 import javafx.application.Application;
 import javafx.event.ActionEvent;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.Button;
 import javafx.scene.image.Image;
 import javafx.scene.image.ImageView;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.VBox;
 import javafx.stage.Stage;
 import org.opencv.core.Core;
 import org.opencv.core.Mat;
 import org.opencv.core.MatOfByte;
 import org.opencv.videoio.VideoCapture;
 import org.opencv.videoio.Videoio;
 import org.opencv.imgcodecs.Imgcodecs;
 import org.opencv.core.MatOfRect;
 import org.opencv.core.Point;
 import org.opencv.core.Rect;
 import org.opencv.core.Scalar;
 import org.opencv.imgproc.Imgproc;
 import org.opencv.objdetect.CascadeClassifier;

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image("Detection.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}

click to hide/show revision 4
retagged

updated 2018-05-15 03:49:57 -0600

berak gravatar image

Help! How to Detect video image in real-time? Using OpenCV?

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image("Detection.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}

Help! How to Detect video image in real-time? Using OpenCV?

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image("Detection.png", mat2Image(".png", image);

            //It save to the project directory
            Imgcodecs.imwrite("capture.png", image);
            HighGui.imshow("capture.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}

Help! How to Detect video image in real-time? Using OpenCV?

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image(".png", image);

            //It save to the project directory
            Imgcodecs.imwrite("capture.png", image);
            HighGui.imshow("capture.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}

Help! How to Detect video image in real-time? real-time. Using OpenCV?OpenCV

This is the complete Source Code of the Project. It works, it detects the camera, frame set. But my problem is on how to detect the real-time detection of the image captured. I hope someone will help me regarding on this project. If you do, thanks a lot for helping me.. Best regards...

public class OpenCV extends Application {

static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

private boolean isStart = false;
private VideoCapture capture;
private ScheduledExecutorService timer;
private BorderPane root;
private VBox vboxCenter;
private ImageView frame;
private HBox hboxBottom;
private Button videoButton, exitButton;

@Override
public void start(Stage primaryStage) {

    initGui();

    capture = new VideoCapture();

    exitButton.setOnAction((ActionEvent event) -> {
        System.exit(0);
    });
    videoButton.setOnAction((ActionEvent event) -> {
        if (!isStart) {
            frame.setFitWidth(640);
            frame.setFitWidth(480);
            frame.setPreserveRatio(true);

            capture.open(0);
            capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
            capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);

            if (capture.isOpened()) {
                isStart = true;
                Runnable frameGrabber = new Runnable() {
                    @Override
                    public void run() {

                    //This code detect the camera and load the frame set
                    Image imageToShow = grabFrame();
                    frame.setImage(imageToShow); 

                CascadeClassifier faceDetector = 
                new CascadeClassifier("C:/Users/BEREDJ/Documents/NetBeansProjects/"
                        + "openCV/src/lbpcascades/lbpcascade_frontalcatface.xml");

                Mat image = Imgcodecs.imread("C:/Users/BEREDJ/Documents/"
                        + "NetBeansProjects/openCV/src/lbpcascades/image.png");  

                MatOfRect faceDetections = new MatOfRect();
                faceDetector.detectMultiScale(image, faceDetections);

                System.out.println(String.format("Detected %s faces",
                faceDetections.toArray().length));

                // Draw a bounding box around each face.
                    for (Rect rect : faceDetections.toArray()) {
                        Imgproc.rectangle(image, new Point(rect.x, rect.y),
                        new Point(rect.x + rect.width,
                        rect.y + rect.height), 
                        new Scalar(0, 255, 0));
                    }
                        //Save the visualized detection.
                        String filename = "faceDetection.png";
                        System.out.println(String.format("Writing %s", filename));
                        Imgcodecs.imwrite("faceDetection.png", image);         
                    }
                };
                timer = Executors.newSingleThreadScheduledExecutor();
                timer.scheduleAtFixedRate(frameGrabber, 0, 33, TimeUnit.MILLISECONDS);
                videoButton.setText("Stop");
            } else {
                System.err.println("Open Camera Error!");
            }
        } else {
            isStart = false;
            videoButton.setText("Start");
            try {
                timer.shutdown();
                timer.awaitTermination(33, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                System.err.println(e);
            }
            capture.release();
            frame.setImage(null);
        }
    });
    Scene scene = new Scene(root, 800, 640);
    primaryStage.setTitle("Video Capture Demo");
    primaryStage.setScene(scene);
    primaryStage.show();
}

private Image grabFrame() {
    Image result = null;
    Mat image = new Mat();

    if (capture.isOpened()) {
        capture.read(image);
        if (!image.empty()) {

            result = mat2Image(".png", image);

            //It save to the project directory
            Imgcodecs.imwrite("capture.png", image);
            HighGui.imshow("capture.png", image);
        }
    }
    return result;
}

public static Image mat2Image(String ext, Mat image) {
    MatOfByte buffer = new MatOfByte();
    Imgcodecs.imencode(ext, image, buffer);
    return new Image(new ByteArrayInputStream(buffer.toArray()));
}

private void initGui() {
    root = new BorderPane();
    vboxCenter = new VBox();
    vboxCenter.setAlignment(Pos.CENTER);
    vboxCenter.setPadding(javafx.geometry.Insets.EMPTY);
    frame = new ImageView();
    vboxCenter.getChildren().addAll(frame);
    root.setCenter(frame);

    hboxBottom = new HBox();
    hboxBottom.setAlignment(Pos.CENTER);
    hboxBottom.setPadding(javafx.geometry.Insets.EMPTY);
    videoButton = new Button("Start");
    exitButton = new Button ("Exit");
    hboxBottom.getChildren().addAll(videoButton,exitButton );
    root.setBottom(hboxBottom);
}

public static void main(String[] args) {
    launch(args);
}

}