Ask Your Question

sniper101's profile - activity

2018-05-15 10:21:12 -0600 marked best answer 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);
}

}

2018-05-15 10:21:12 -0600 received badge  Scholar (source)
2018-05-15 09:59:23 -0600 edited question How to Detect video image in real-time. Using OpenCV

How to Detect video image in real-time. Using OpenCV This is the complete Source Code of the Project. It works, it detec

2018-05-15 09:59:16 -0600 edited question How to Detect video image in real-time. Using OpenCV

Help! How to Detect video image in real-time? Using OpenCV? This is the complete Source Code of the Project. It works, i

2018-05-15 09:57:36 -0600 edited question How to Detect video image in real-time. Using OpenCV

Help! How to Detect video image in real-time? Using OpenCV? This is the complete Source Code of the Project. It works, i

2018-05-15 09:31:13 -0600 commented answer How to Detect video image in real-time. Using OpenCV

Yes, I got an image from the webcam. The camera captured but there is no detection happen. Maybe you're right no process

2018-05-15 04:17:49 -0600 commented answer How to Detect video image in real-time. Using OpenCV

This is the error sir @kbarni Detected 0 faces Writing faceDetection.png Detected 0 faces Writing faceDetection.png Det

2018-05-15 04:09:18 -0600 commented answer How to Detect video image in real-time. Using OpenCV

This is the error sir @kbarni Detected 0 faces Writing faceDetection.png Detected 0 faces Writing faceDetection.png Det

2018-05-15 03:45:11 -0600 commented question How to Detect video image in real-time. Using OpenCV

I mean your camera detected (real you) when you face the camera...

2018-05-15 03:35:58 -0600 received badge  Editor (source)
2018-05-15 03:35:58 -0600 edited question How to Detect video image in real-time. Using OpenCV

Help! How to Detect video image in real-time? Using OpenCV? This is the complete Source Code of the Project. It works, i

2018-05-15 03:34:40 -0600 asked a question How to Detect video image in real-time. Using OpenCV

Help! How to Detect video image in real-time? Using OpenCV? This is the complete Source Code of the Project. It works, i