Created 11-10-2016 04:50 AM
I am following the popular DetectFaceDemo tutorial but I cant seem to run the packaged jar . [DetectFaceDemo](http://docs.opencv.org/2.4/doc/tutorials/introduction/desktop_java/java_dev_intro.html)
I have placed the following files in their respect directories as shown below :
[root@hadoop1 java]# pwd /root/openCV/opencv/samples/java/sbt/src/main/java [root@hadoop1 java]# [root@hadoop1 java]# ls build.sbt DetectFaceDemo.java.orig HelloOpenCV.java lib project target [root@hadoop1 java]# [root@hadoop1 java]# ls lib libopencv_java249.so opencv-249.jar [root@hadoop1 java]# [root@hadoop1 java]# cd .. [root@hadoop1 main]# pwd /root/openCV/opencv/samples/java/sbt/src/main [root@hadoop1 main]# ls java origscala resources [root@hadoop1 main]# ls resources AverageMaleFace.jpg img1.png img2.png lbpcascade_frontalface.xml lena.png [root@hadoop1 main]#
I am getting error when I run the jar , I have a feeling the code is not picking up the image and the xml file from the src/main/resources folder ? I have though provided the absolute path to the resource folder.
[root@hadoop1 java]# sbt run [info] Set current project to DetectFaceDemo (in build file:/root/openCV/opencv/samples/java/sbt/src/main/java/) [info] Running HelloOpenCV [info] Hello, OpenCV [info] [info] Running DetectFaceDemo [error] Exception in thread "main" java.lang.NullPointerException [error] at DetectFaceDemo.run(HelloOpenCV.java:20) [error] at HelloOpenCV.main(HelloOpenCV.java:48) java.lang.RuntimeException: Nonzero exit code returned from runner: 1 at scala.sys.package$.error(package.scala:27) [trace] Stack trace suppressed: run last compile:run for the full output. [error] (compile:run) Nonzero exit code returned from runner: 1 [error] Total time: 1 s, completed Nov 9, 2016 11:20:34 PM
The source code is below :
[root@hadoop1 java]# more HelloOpenCV.java 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.highgui.Highgui; 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. CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/root/openCV/opencv/samples/java/sbt/src/main/resources/lbpcascade_frontalface.xml").getPath()); Mat image = Highgui.imread(getClass().getResource("/root/openCV/opencv/samples/java/sbt/src/main/resources/lena.png").getPath()); // 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()) { 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. String filename = "faceDetection.png"; System.out.println(String.format("Writing %s", filename)); Highgui.imwrite(filename, image); } } 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(); } } [root@hadoop1 java]#
Created 11-10-2016 06:24 AM
found the solution online
put the following in the code to find the classpath and copy the "xml" and "png" files to this location . System.out.println(System.getProperty("java.class.path"));
Created 11-10-2016 05:09 AM
The NullPointerException seems to be coming from the following line 20 which is the following in your code:
CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("/root/openCV/opencv/samples/java/sbt/src/main/resources/lbpcascade_frontalface.xml").getPath());
I am suspecting that you might not have placed the file in that location with proper read/write permission.
As the code is using "getClass().getResource(...)" method approach here so it means the class should be present relative to your "HelloOpenCV.class" directory. So please place the XML file "lbpcascade_frontalface.xml" in the same directory where you have the "HelloOpenCV.class" and then change the code to give the relative path:
CascadeClassifier faceDetector = new CascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
Created 11-10-2016 06:24 AM
found the solution online
put the following in the code to find the classpath and copy the "xml" and "png" files to this location . System.out.println(System.getProperty("java.class.path"));
Created 11-10-2016 06:42 AM
By default the current directory (where the *.class is present) is already added to the classpath. So do not need to worry about it. The only change you need in your case is to
1. Make sure that the "HelloOpenCV.class" and "lbpcascade_frontalface.xml" are colocated with following code.
CascadeClassifier faceDetector = newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());
OR
2. You should create a JAR where the "HelloOpenCV.class" and "lbpcascade_frontalface.xml" both are present. and then you can use the same code:
CascadeClassifier faceDetector = newCascadeClassifier(getClass().getResource("lbpcascade_frontalface.xml").getPath());