| 1 | initial version |
After a bit of extra work, I got the groovy problem solved as well! For some reason the System.loadLibrary(CORE.NATIVE_LIBRARY_NAME) does not work well with Groovy.
I wrote the following (somewhat trivial) Java class:
import org.opencv.core.Core;
public class LibLoader {
public static void load() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
Compile with:
javac -cp ../build/bin/opencv-245.jar:. LibLoader.javaIn the groovy script use the static class as follows:
LibLoader.load()I still have to pre-compile first with groovyc and then run the compiled java class. Here is the DetectFaceDemo as a groovy script:
/*
* Detects faces in an image, draws boxes around them, and writes the results
* to "faceDetection.png".
*/
LibLoader.load()
println("\nRunning DetectFaceDemo")
// Create a face detector from the cascade file in the resources directory.
// Note: I got rid of the getClass.getResource.getPath construction (lazy)
def faceDetector = new CascadeClassifier("resources/lbpcascade_frontalface.xml")
def image = Highgui.imread("resources/AverageMaleFace.jpg")
// Detect faces in the image.
// MatOfRect is a special container class for Rect.
def faceDetections = new MatOfRect()
faceDetector.detectMultiScale(image, faceDetections)
println(String.format("Detected %s faces", faceDetections.toArray().length))
// Draw a bounding box around each face.
for (Rect rect : faceDetections.toArray()) {
Core.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.
def filename = "faceDetection.png"
println(String.format("Writing %s", filename))
Highgui.imwrite(filename, image)
Compile and run with:
groovyc -cp ../build/bin/opencv-245.jar:. DetectFace.groovy java -cp ../build/bin/opencv-245.jar:/usr/local/groovy/embeddable/groovy-all-2.1.4.jar:. -Djava.library.path=../build/lib DetectFace
Results:
Running DetectFaceDemo Detected 1 faces Writing faceDetection.png
I reckon I will get groovy to work without the need for compilation first (there seems to be a problem with java.library.path).
What's neat about groovy is the simplicity of the code. I used to work with Java Advanced Imaging, but now that OpenCV has gpu-enabled routines, I want to go this road.