Ask Your Question

Syed Hasnain's profile - activity

2017-05-19 13:52:15 -0500 asked a question OpenCV - predict_label returns a huge value

I have trained faceRecognizer in Java. But when i predict a label it returns a huge value which is not the actual label which i stored during training. Please have a look at my training code and tell me how this error can be resolved.

String trainingDir = "res/";
        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);
    ArrayList <mat> images = new ArrayList(imageFiles.length);
    Mat labels = new Mat(imageFiles.length,1,CvType.CV_32SC1);
    ArrayList <integer>labelsBuf = new ArrayList<>(imageFiles.length);

    int counter =0;
    for(File image: imageFiles){
        Mat img = Imgcodecs.imread(image.getAbsolutePath(),Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
        int label = Integer.parseInt(image.getName().split("\\.")[2]);
        //images.add(label, img);
        labelsBuf.add(counter, label); 
    FaceRecognizer faceRecognizer = Face.createFisherFaceRecognizer();

    faceRecognizer.train(images, labels);

Thanks in advance

2017-05-19 13:30:53 -0500 commented answer Wrong Shapes for Given Matrices in OpenCv 3.1 - Java

Thanks you so much this really worked. Now i am facing another problem. When i use the function predict_label it shows me a huge number how could i get the exact label which i stored during train.

2017-05-19 13:30:05 -0500 received badge  Supporter (source)
2017-05-19 13:30:03 -0500 received badge  Scholar (source)
2017-05-19 06:30:30 -0500 asked a question Wrong Shapes for Given Matrices in OpenCv 3.1 - Java

I have trained the FaceRecognizer all the images are of same size and passed the grayscale image to the predictor. But i face this error

OpenCV Error: Bad argument (Wrong shapes for given matrices. Was size(src) = (1,150544), size(W) = (37636,5).) in subspaceProject, file /home/**/opencv/modules/core/src/lda.cpp, line 182

Here is the code

if (videoDevice.isOpened()) {
            while (true) {      
                Mat frameCapture = new Mat();
                MatOfRect faces = new MatOfRect();
                cascadeFaceClassifier.detectMultiScale(frameCapture, faces, 2.0, 5, 0,new Size(),new Size());

                for (Rect rect : faces.toArray()) {
                    Mat resizedCapture = new Mat(frameCapture,rect);
                    Imgproc.cvtColor(resizedCapture, resizedCapture, Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
                    Size imageSize = new Size(194,194);
                    Imgproc.resize(resizedCapture,resizedCapture , imageSize);

                        x = faceRecognizer.predict_label(resizedCapture);
                    }catch(Exception e){
                    Imgproc.putText(frameCapture, "Face"+x, new Point(rect.x,rect.y-5), 1, 2, new Scalar(0,0,255));
                    Imgproc.rectangle(frameCapture, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),new Scalar(0, 100, 0),3);

                //System.out.println(String.format("FACES = %s EYES =  %s detected.", faces.toArray().length,eyes.toArray().length,nose.toArray().length));
2017-05-17 13:54:04 -0500 asked a question OpenCV Java Face Trainer Core Dump Error

package mainpackage; import; import; import; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; import org.opencv.core.; import org.opencv.face.; import org.opencv.imgcodecs.; import org.opencv.imgproc.; import java.nio.Buffer; public class Trainer{

public static void main(String[] args){ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); String trainingDir = "res/"; //Mat testImage = Imgcodecs.imread("res/User.Hasnain.1.jpg",Imgcodecs.IMREAD_GRAYSCALE); 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[] imagesFile = root.listFiles(imgFilter); Mat labels = new Mat(imagesFile.length); ArrayList<mat> images = new ArrayList<mat>() ; IntBuffer labelsBuff = IntBuffer.allocate(imagesFile.length); int counter=0; for(File image: imagesFile ){ Mat img = Imgcodecs.imread(image.getAbsolutePath(),Imgcodecs.IMREAD_GRAYSCALE); int label = Integer.parseInt(image.getName().split("\.")[2]); labelsBuff.put(counter,label);

        images.add(counter, img);
              }       FaceRecognizer f = Face.createFisherFaceRecognizer();
  f.train(images, labels);"trainingData.yml");     } }

When ever i execute the following code eclipse gives me this error #

A fatal error has been detected by the Java Runtime Environment:


SIGSEGV (0xb) at pc=0x00007f8d59df4d00, pid=3167, tid=0x00007f8dac5b3700


JRE version: OpenJDK Runtime Environment (8.0_121-b13) (build 1.8.0_121-8u121-b13-0ubuntu1.16.04.2-b13)

Java VM: OpenJDK 64-Bit Server VM (25.121-b13 mixed mode linux-amd64 compressed oops)

Problematic frame:

C [] cv::face::Fisherfaces::train(cv::_InputArray const&, cv::_InputArray const&)+0x2fa0


Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again


An error report file with more information is saved as:



If you would like to submit a bug report, please visit:

The crash happened outside the Java Virtual Machine in native code.

See problematic frame for where to report the bug.


2017-05-12 03:51:23 -0500 asked a question Unable to create Recognzer Object

Hello, i am new to openCV java library. I want to create a face recognizer object in my java file but there is no face recognizer exist in my java code. Which files are necessary to import Facerecognizer.

import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
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.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;
import org.opencv.videoio.VideoCapture;
import java.util.Scanner;
public class FaceTrainer{
    public static void main(String[] args){
        FaceRecognizer rec=createLBPHFaceRecognizer();