Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

"Is there some way to activate OpenCV logging in the Java version? " - none of the functions used will throw exceptions(not even in c++), you will have to do manual checks:

import org.opencv.imgproc.*; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.objdetect.CascadeClassifier; // // Detects faces in an image, draws boxes around them, and writes the results // to "faceDetection.png". // class DetectFaceDemo { public void run() { System.out.println("\nRunning DetectFaceDemo"); // Create a face detector from the cascade file in the resources directory: String cascadePath = getClass().getResource("/lbpcascade_frontalface.xml").getPath(); // // i'm unsure, but maybe we should get rid of the whole getClass().getResource()getPath() cruft // in favour of a simple String containing the (absolute) path, like "d:/ocv/lbpcascade_frontalface.xml" // CascadeClassifier faceDetector = new CascadeClassifier(cascadePath);

// sanity check:
if (faceDetector.empty()) {
    System.out.println("could not load cascade from " + cascadePath);
}

String imgPath = getClass().getResource("/lena.png").getPath();
Mat image = Imgcodecs.imread(imgPath);

// sanity check:
if (image.empty()) {
    System.out.println("could not load image from " + imgPath);
}

// Detect faces in the image.
// MatOfRect is a special container class for Rect.
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));
boolean ok = Imgcodecs.imwrite(filename, image);
// sanity check:
if (! ok) {
    System.out.println("could not save image to " + filename);
}

} } public class HelloOpenCV { public static void main(String[] args) { System.out.println("Hello, OpenCV"); // Load the native library. System.loadLibrary(Core.NATIVE_LIBRARY_NAME); new DetectFaceDemo().run(); } }

"Is there some way to activate OpenCV logging in the Java version? " - none of the functions used will throw exceptions(not even in c++), you will have to do manual checks:

import org.opencv.imgproc.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
class DetectFaceDemo {
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");
    // Create a face detector from the cascade file in the resources directory:
    String cascadePath = getClass().getResource("/lbpcascade_frontalface.xml").getPath();
    //
    // i'm unsure, but maybe we should get rid of the whole getClass().getResource()getPath() cruft
    // in favour of a simple String containing the (absolute) path, like "d:/ocv/lbpcascade_frontalface.xml"
    //
    CascadeClassifier faceDetector = new CascadeClassifier(cascadePath);

CascadeClassifier(cascadePath);

    // sanity check:
 if (faceDetector.empty()) {
     System.out.println("could not load cascade from " + cascadePath);
}

    }

    String imgPath = getClass().getResource("/lena.png").getPath();
 Mat image = Imgcodecs.imread(imgPath);

 // sanity check:
 if (image.empty()) {
     System.out.println("could not load image from " + imgPath);
}

    }

    // Detect faces in the image.
 // MatOfRect is a special container class for Rect.
 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));
 boolean ok = Imgcodecs.imwrite(filename, image);
 // sanity check:
 if (! ok) {
     System.out.println("could not save image to " + filename);
}

} } } public class HelloOpenCV { public static void main(String[] args) { System.out.println("Hello, OpenCV"); // Load the native library. System.loadLibrary(Core.NATIVE_LIBRARY_NAME); new DetectFaceDemo().run(); } }

}

"Is there some way to activate OpenCV logging in the Java version? " - none of the functions used will throw exceptions(not even in c++), you will have to do manual checks:

import org.opencv.imgproc.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
class DetectFaceDemo {
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");
    // Create a face detector from the cascade file in the resources directory:
    String cascadePath = getClass().getResource("/lbpcascade_frontalface.xml").getPath();
    //
    // i'm unsure, but maybe we should get rid of the whole getClass().getResource()getPath() cruft
    // in favour of a simple String containing the (absolute) path, 
    // like "d:/ocv/lbpcascade_frontalface.xml"
    //
    CascadeClassifier faceDetector = new CascadeClassifier(cascadePath);

    // sanity check:
    if (faceDetector.empty()) {
        System.out.println("could not load cascade from " + cascadePath);
    }

    String imgPath = getClass().getResource("/lena.png").getPath();
    Mat image = Imgcodecs.imread(imgPath);

    // sanity check:
    if (image.empty()) {
        System.out.println("could not load image from " + imgPath);
    }

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    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));
    boolean ok = Imgcodecs.imwrite(filename, image);
    // sanity check:
    if (! ok) {
        System.out.println("could not save image to " + filename);
    }

  }
}
public class HelloOpenCV {
  public static void main(String[] args) {
    System.out.println("Hello, OpenCV");
    // Load the native library.
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    new DetectFaceDemo().run();
  }
}

"Is there some way to activate OpenCV logging in the Java version? " - none of the functions used will throw exceptions(not even in c++), you will have to do manual checks:

import org.opencv.imgproc.*;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.objdetect.CascadeClassifier;
//
// Detects faces in an image, draws boxes around them, and writes the results
// to "faceDetection.png".
//
class DetectFaceDemo {
  public void run() {
    System.out.println("\nRunning DetectFaceDemo");
    // Create a face detector from the cascade file in the resources directory:
    String cascadePath = getClass().getResource("/lbpcascade_frontalface.xml").getPath();
    //
    // maybe we should get rid of the whole getClass().getResource()getPath() cruft
    // in favour of a simple String containing the (absolute) path, 
    // like "d:/ocv/lbpcascade_frontalface.xml"
    //
    // also note, that opencv functions cannot read from a zip/jar/apk file,
    // so the whole 'resource' approach might be inappropriate.
    //
    CascadeClassifier faceDetector = new CascadeClassifier(cascadePath);

    // sanity check:
    if (faceDetector.empty()) {
        System.out.println("could not load cascade from " + cascadePath);
    }

    String imgPath = getClass().getResource("/lena.png").getPath();
    Mat image = Imgcodecs.imread(imgPath);

    // sanity check:
    if (image.empty()) {
        System.out.println("could not load image from " + imgPath);
    }

    // Detect faces in the image.
    // MatOfRect is a special container class for Rect.
    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));
    boolean ok = Imgcodecs.imwrite(filename, image);
    // sanity check:
    if (! ok) {
        System.out.println("could not save image to " + filename);
    }

  }
}
public class HelloOpenCV {
  public static void main(String[] args) {
    System.out.println("Hello, OpenCV");
    // Load the native library.
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    new DetectFaceDemo().run();
  }
}