Example usage for org.opencv.objdetect CascadeClassifier load

List of usage examples for org.opencv.objdetect CascadeClassifier load

Introduction

In this page you can find the example usage for org.opencv.objdetect CascadeClassifier load.

Prototype

public boolean load(String filename) 

Source Link

Usage

From source file:com.ibm.streamsx.edgevideo.device.FaceDetector.java

License:Open Source License

/**
 * Create a new instance using the specified {@link CascadeClassifier}.
 * @param faceClassifierPath path to the classifier - e.g. haarcascade_frontalface_alt.xml
 * @throws IOException // w  w  w .ja  v  a 2s  . c  o  m
 */
public FaceDetector(File faceClassifierPath) throws IOException {
    CascadeClassifier faceClassifier = new CascadeClassifier();
    boolean loaded = faceClassifier.load(faceClassifierPath.toString());
    if (!loaded) {
        throw new IOException("Error loading face classifier path: " + faceClassifierPath);
    }
    this.faceClassifier = faceClassifier;
}

From source file:com.raulh82vlc.face_detection_sample.opencv.presentation.FDOpenCVPresenter.java

License:Apache License

private synchronized CascadeClassifier initializeJavaDetector(File cascadeFile) {
    CascadeClassifier detector = new CascadeClassifier(cascadeFile.getAbsolutePath());
    detector.load(cascadeFile.getAbsolutePath());
    if (detector.empty()) {
        Log.e(TAG, "Failed to load cascade classifier");
        detector = null;/*ww  w  . j  a v  a2  s.  co  m*/
    } else {
        Log.i(TAG, "Loaded cascade classifier from " + cascadeFile.getAbsolutePath());
    }
    return detector;
}

From source file:detectors.CCdetection.java

/**
 * Method to scan image with x classifier
 *//*w  w  w.  ja  va  2  s  .  c o  m*/
public void scanImage(String path) {
    CascadeClassifier faceCC = new CascadeClassifier();
    faceCC.load("res/haarcascade_frontalface_alt.xml");

    //Matrix to hold image to be scanned
    Mat image = new Mat();
    image = Highgui.imread(path, IMREAD_COLOR);

    Mat rgb = new Mat();
    Mat gray = new Mat();
    image.copyTo(rgb);
    image.copyTo(gray);

    //Create integral image from the grayscale version of frame 
    Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_BGR2GRAY);
    Imgproc.equalizeHist(gray, gray);

    //MatOfRect detection window initialized
    MatOfRect detects = new MatOfRect();
    faceCC.detectMultiScale(gray, detects);

    //Print out number of detected rect objects detected
    if (detects.toArray().length != 0) {
        System.out.printf("Detected %s\n", detects.toArray().length);
    }
}

From source file:detectors.CCdetection.java

/**
 * Method to scan video with x classifier
 * NOT COMPLETE/*from   ww  w  . j  a  v  a2 s  . co m*/
 */
public void scanVideo() {
    //Initialize video capture object to 0(default camera device)
    VideoCapture vc = new VideoCapture(0);

    //String path = "";

    //Initialize and load in classifier
    CascadeClassifier faceCC = new CascadeClassifier();
    faceCC.load("res/haarcascade_frontalface_default.xml");

    //Matrix(opencv core image object) to hold the image frame coming from the capture stream
    Mat image = new Mat();

    boolean isFound = false;
    while (!isFound) {
        //VideoCapture read combines grab and retrieve methods to decode and return the frame
        vc.read(image);
        if (image != null) {

            //Initialize new Mats to hold the original colored frame and the grayscaled frame
            Mat rgb = new Mat();
            Mat gray = new Mat();
            image.copyTo(rgb);
            image.copyTo(gray);

            //Create integral image from the grayscale version of frame 
            Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_BGR2GRAY);
            Imgproc.equalizeHist(gray, gray);

            //MatOfRect detection window initialized
            MatOfRect detects = new MatOfRect();
            faceCC.detectMultiScale(gray, detects);

            //Print out number of detected rect objects detected
            if (detects.toArray().length != 0) {
                System.out.printf("Detected %s\n", detects.toArray().length);
                //prodFound(UPC,"T");
                isFound = true;
            }

        } else {
            break;
        }
    }
}

From source file:org.wso2.carbon.cep.objectdetection.siddhiextension.ObjectDetectionExtension.java

License:Open Source License

/**
 * Detect objects using OpenCV.// www. j a  v  a  2 s  .  co  m
 *
 * @param imageHex
 *            the image hex string
 * @param cascadePath
 *            the cascade path
 * @return the detected object count
 */
private long detectObjects(String imageHex, String cascadePath) {
    long objectCount = 0;
    try {
        // conversion to Mat
        byte[] imageByteArr = (byte[]) new Hex().decode(imageHex);
        Mat image = Highgui.imdecode(new MatOfByte(imageByteArr), Highgui.IMREAD_UNCHANGED);

        // initializing classifier
        CascadeClassifier cClassifier = new CascadeClassifier();
        cClassifier.load(cascadePath);

        // pre-processing
        Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY);
        Imgproc.equalizeHist(image, image);

        // detecting objects
        MatOfRect imageRect = new MatOfRect();
        cClassifier.detectMultiScale(image, imageRect);

        // image count
        objectCount = ((Integer) imageRect.toList().size()).longValue();
    } catch (DecoderException e) {
        e.printStackTrace();
    }

    return objectCount;
}