Untitled

mail@pastecode.io avatar
unknown
java
5 months ago
6.7 kB
1
Indexable
package com.zetbo;

import org.bytedeco.javacv.*;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.RectVector;
import org.bytedeco.opencv.opencv_objdetect.CascadeClassifier;

import java.io.IOException;

import static org.bytedeco.opencv.global.opencv_imgproc.*;

public class DetectionWithClassifier {

    final int width = 640;
    final int height = 480;

    public void start() throws Exception {

        CascadeClassifier cascade = new CascadeClassifier("haarcascade_frontalface_alt2.xml");
        OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
        CanvasFrame canvas = new CanvasFrame("Video capture");
        canvas.setCanvasSize(width, height);

        boolean detecting = true;
        boolean recording = false;

        try (FrameGrabber grabber = new OpenCVFrameGrabber(0);
             FrameRecorder recorder = FrameRecorder.createDefault("output.mpeg", width, height)) {

            Mat videoMat;
            Frame videoFrame;

            grabber.start();
            grabber.setFrameRate(25);
            recorder.setFrameRate(25);
//            recorder.start();

            System.out.println("Grabbing frames...");

            long recordingStartTime = 0;

            while (canvas.isVisible() && (videoFrame = grabber.grab()) != null) {
//                    grabbedImage = frameConverter.convert(grabber.grab());
//                videoFrame = grabber.grab();

                videoMat = converterToMat.convert(videoFrame);
                Mat videoMatGray = new Mat();

                // Convert the current frame to grayscale:
                cvtColor(videoMat, videoMatGray, COLOR_BGRA2GRAY);
                equalizeHist(videoMatGray, videoMatGray);

                RectVector faces = new RectVector();
                // Find the faces in the frame:
                cascade.detectMultiScale(videoMatGray, faces);



                if (faces.size() > 0) {
                    if (recording) {
                        if (System.currentTimeMillis() > recordingStartTime + 5000) {
                            System.out.println("Stopping recording...");
                            recording = false;
                            recorder.stop();
                        } else {
                            // no timeout
                            recorder.record(videoFrame);
                        }

                    } else {
                        System.out.println("Starting recording...");
                        recorder.start();
                        recording = true;
                        recordingStartTime = System.currentTimeMillis();
                    }
                } else {
                    // no detection, are we recording?
                    if (recording) {
                        if (System.currentTimeMillis() > recordingStartTime + 5000) {
                            System.out.println("Stopping recording...");
                            recording = false;
                            recorder.stop();
                        } else {
                            // no timeout
                            recorder.record(videoFrame);
                        }
                    }

                }


                canvas.showImage(videoFrame);
            }

            System.out.println("Recording saved?");
            canvas.dispose();
            recorder.stop();
            grabber.stop();

        } catch (IOException e) {
            System.err.println("Exception throws while running detection: " + e.getMessage());
        }

        System.out.println("Done");


//        FrameGrabber grabber = new OpenCVFrameGrabber(0);
//        grabber.start();
//
//
//        CanvasFrame canvas = new CanvasFrame("Detection");
//        canvas.setCanvasSize(500, 500);
//
//        Frame frame;
//
//        while (true) {
//            frame = grabber.grabFrame();
//            canvas.showImage(frame);
//        }
    }



//    public void start() throws Exception {
//
//        CascadeClassifier cascade = new CascadeClassifier("haarcascade_frontalface_alt2.xml");
//
//        CanvasFrame canvas = new CanvasFrame("Video capture");
//        canvas.setCanvasSize(width, height);
//
//        try (FrameGrabber grabber = new OpenCVFrameGrabber(0);
//             FrameRecorder recorder = FrameRecorder.createDefault("output.mpeg", width, height)) {
//
//            Mat videoMat;
//
//            grabber.start();
//            grabber.setFrameRate(25);
//            recorder.setFrameRate(25);
//            recorder.start();
//
//            Frame grabbedFrame;
//            Frame videoFrame;
//
//            OpenCVFrameConverter.ToMat converterToMat = new OpenCVFrameConverter.ToMat();
//
//            System.out.println("Grabbing frames...");
//            while (canvas.isVisible() && (grabbedFrame = grabber.grab()) != null) {
////                    grabbedImage = frameConverter.convert(grabber.grab());
//                videoFrame = grabber.grab();
//                videoMat = converterToMat.convert(videoFrame);
//                Mat videoMatGray = new Mat();
//                // Convert the current frame to grayscale:
//                cvtColor(videoMat, videoMatGray, COLOR_BGRA2GRAY);
//                equalizeHist(videoMatGray, videoMatGray);
//
////                Point p = new Point();
//                RectVector faces = new RectVector();
//                // Find the faces in the frame:
//                cascade.detectMultiScale(videoMatGray, faces);
//
//
//                if (faces.size() > 0) {
//                    System.out.println("detected face");
//                }
//
//                canvas.showImage(grabbedFrame);
//                recorder.record(grabbedFrame);
//
//            }
//
//            System.out.println("Recording saved?");
//            canvas.dispose();
//            recorder.stop();
//            grabber.stop();
//
//
////            try (OpenCVFrameConverter.ToMat frameConverter = new OpenCVFrameConverter.ToMat()) {
//////                System.out.println("Converting frame...");
////
////            }
//
//
//        } catch (IOException e) {
//            System.err.println("Exception throws while running detection: " + e.getMessage());
//        }
//
//        System.out.println("Done");
//
//
////        FrameGrabber grabber = new OpenCVFrameGrabber(0);
////        grabber.start();
////
////
////        CanvasFrame canvas = new CanvasFrame("Detection");
////        canvas.setCanvasSize(500, 500);
////
////        Frame frame;
////
////        while (true) {
////            frame = grabber.grabFrame();
////            canvas.showImage(frame);
////        }
//    }
}
Leave a Comment