-2

I've reviewed lots of questions related to my topic because of which i am able to understand that how this exception occurs but do not understand why this exception is occurring in the code i am executing. The class below takes two arguments:The path to the directory containing the training faces and the path to the image you want to classify. This is actually not my code it was written by Petter Christian Bjelland... Following is the code

public class OpenCVFaceRecognizer {
    public static void main(String[] args) {
        String trainingDir = args[0];
        Object testImage = Highgui.imread(args[1], 0);

        File root = new File(trainingDir);
        FilenameFilter imgFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                name = name.toLowerCase();
                return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
            }
        };
        File[] imageFiles = root.listFiles(imgFilter);
        MatVector images = new MatVector(imageFiles.length);
        Mat labels = new Mat(imageFiles.length, 1, CV_32SC1);
        IntBuffer labelsBuf = labels.createBuffer();
        int counter = 0;
        for (File image : imageFiles) {
            Object img = Highgui.imread(image.getAbsolutePath(), 0);
            int label = Integer.parseInt(image.getName().split("\\-")[0]);
            images.put(counter, (Mat) img);
            labelsBuf.put(counter, label);
            counter++;
        }
        FaceRecognizer faceRecognizer = createFisherFaceRecognizer();
        faceRecognizer.train(images, labels);
        int predictedLabel = faceRecognizer.predict((Mat) testImage);
        System.out.println("Predicted label: " + predictedLabel);
    }
}

Following is the complete exception

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at OpenCVFaceRecognizer.main(OpenCVFaceRecognizer.java:40)

This is the line 40 String trainingDir = args[0];

  • 2
    Please refer to http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Unknown Sep 03 '16 at 07:31
  • Don't use an index which doesn't exist. You don't appear to have any command line arguments to lookup. – Peter Lawrey Sep 03 '16 at 07:35

4 Answers4

0

Well, you need to actually pass these arguments while launching your code

java OpenCVFaceRecognizer <pathToDirectory> <pathToImageToClassify>

If you're using Eclipse, there's a way to pass arguments while runnning your code.

Check out this other SO post :

Eclipse : how we take arguments for main when run

Community
  • 1
  • 1
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0

As ArrayIndexOutOfBoundsException is at main method string arguments it means at time of running application you need to pass string array as command line arguments. If you are using any editor then you required to set run time argument in run configurations of editor

for command line java OpenCVFaceRecognizer <pathToDirectory> <pathToImageToClassify>

Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
0

This code is expecting command line arguement and as you have not passed command line arguement it is returning this error.

enter image description here

Or from terminal use

java OpenCVFaceRecognizer <foldername>  <imagePath>
Naruto
  • 4,221
  • 1
  • 21
  • 32
  • like this in the program argument _trainingImages_ _jon_doe_1.png_?? – Farwa Ansari Sep 03 '16 at 07:50
  • Yes. Your program is expecting two arguements. So if you are using IDE then you can provide it using as per screenshot attached or if you are using terminal to run then use second approach – Naruto Sep 03 '16 at 11:22
0

The args array holds as many items as have been passed to the main method via the command line. For example, if you start your program like this:

java OpenCVFaceRecognizer c:\data\images c:\data\tobeclassified.jpg

args will contain two elements, "c:\data\images" and "c:\data\tobeclassified.jpg". The exception indicates that the program was started with no command line arguments at all (since the index of 0 is already outside of the range of allowed indices, which is the case when the array is empty).

Markus Fischer
  • 1,326
  • 8
  • 13