JavaCV LBPHFaceRecognizer

2,459 views
Skip to first unread message

Tand

unread,
Oct 12, 2012, 12:05:35 PM10/12/12
to jav...@googlegroups.com
Hi,

I try to use LBPH Face Recognizer in Android + JavaCV. However I get stuck here "trainImages.put(i, inputImage);". My codes don't further than this line and my app stops.

Question: do I use the correct variables/classes?

Here is a snipper of my codes:

     MatVector trainImages = new MatVector(numberOfEigenFaces);
    CvArr trainLabels = new CvArr();
    PointerPointer labelPointer;

for (int i=0; i<cNumberOfFacesToLearn; i++) {
String filename = Environment.getExternalStorageDirectory() + File.separator + "face" + Integer.toString(i) + ".jpg";

labelPointer = new PointerPointer(i);
trainLabels.put(labelPointer);
        CvArr inputImage = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE);
               
        trainImages.put(i, inputImage);
}

    FaceRecognizerPtr model = createLBPHFaceRecognizer(1, 8, 8, 8, Double.MAX_VALUE);
    model.get().train(trainImages, trainLabels);

    CvArr testImage = cvLoadImage(filenameFaceToTest, CV_LOAD_IMAGE_GRAYSCALE);
   
    int result = model.get().predict(testImage);

Thanks,
Tand

Samuel Audet

unread,
Oct 13, 2012, 6:54:32 AM10/13/12
to jav...@googlegroups.com
On 2012-10-13 01:05, Tand wrote:
> Hi,
>
> I try to use LBPH Face Recognizer in Android + JavaCV. However I get
> stuck here "trainImages.put(i, inputImage);". My codes don't further
> than this line and my app stops.
>
> Question: do I use the correct variables/classes?
>
> Here is a snipper of my codes:
>
> MatVector trainImages = new MatVector(numberOfEigenFaces);
> CvArr trainLabels = new CvArr();

Don't allocate a CvArr, this code does nothing useful, and actually I am
blocking that in future releases of JavaCV. Allocate a CvMat, as shown
in the README.txt file.

Tand

unread,
Oct 13, 2012, 11:52:48 AM10/13/12
to jav...@googlegroups.com
Ok, thanks. I have replaced CvMat now and used "cvLoadImageM" to load the image. However my code does not go further than "model.get().train(trainImages, trainLabels);" and I don't see any related errors. Can you help? Is my label construction "labelPointer = new PointerPointer(i);" valid?


int numberOfLbphFaces = cNumberOfFacesToLearn;
    MatVector trainImages = new MatVector(numberOfLbphFaces);
    CvMat trainLabels = new CvMat();
    PointerPointer labelPointer;
int faceCounter = 1;
for (int i=0; i<cNumberOfFacesToLearn; i++) {
String filename = Environment.getExternalStorageDirectory() + File.separator + "face" + Integer.toString(i) + ".jpg";
labelPointer = new PointerPointer(i);
trainLabels.put(labelPointer);
        CvMat inputImage = cvLoadImageM(filename, CV_LOAD_IMAGE_GRAYSCALE);
         
        trainImages.put(i, inputImage);
}
    FaceRecognizerPtr model = createLBPHFaceRecognizer(1, 8, 8, 8, Double.MAX_VALUE);
   
    model.get().train(trainImages, trainLabels);

    /* PREDICT */
    CvMat testImage = cvLoadImageM(filenameFaceToTest, CV_LOAD_IMAGE_GRAYSCALE);
   
    int result = model.get().predict(testImage);



Op zaterdag 13 oktober 2012 12:54:35 UTC+2 schreef Samuel Audet het volgende:

Tand

unread,
Oct 14, 2012, 8:10:29 AM10/14/12
to jav...@googlegroups.com
I think that the creation of the labels causes the problem. I have changed to:

CvMat trainLabels = CvMat.create(numberOfLbphFaces, CV_32SC1); // numberOfLbphFaces = 4 in this case.
...
trainLabels.put(i, i);

Once the next code executes:
model.get().train(trainImages, trainLabels);

Then I get the following error:
10-14 14:07:21.140: WARN/System.err(4513): java.lang.RuntimeException: /home/saudet/android/OpenCV-2.4.2/modules/contrib/src/facerec.cpp:732: error: (-210) Labels must be given as integer (CV_32SC1). Expected 4, but was 6. in function virtual void cv::LBPH::train(const cv::_InputArray&, const cv::_InputArray&)

Please help me with the construction of CvMat. Thanks.


Op zaterdag 13 oktober 2012 12:54:35 UTC+2 schreef Samuel Audet het volgende:
On 2012-10-13 01:05, Tand wrote:

Samuel Audet

unread,
Oct 14, 2012, 9:11:44 PM10/14/12
to jav...@googlegroups.com
Hello,

Matrices are always two-dimensional, so something like this should do
what's needed:
CvMat trainLabels = CvMat.create(numberOfLbphFaces, 1, CV_32SC1);

Srijit Chandrashekhar Nair

unread,
Oct 17, 2012, 10:08:39 AM10/17/12
to jav...@googlegroups.com
Sorry.. My bad.. I missed a 'static' keyword in import. I am taking the question back



On Wednesday, October 17, 2012 6:55:29 AM UTC-4, Srijit Chandrashekhar Nair wrote:
Hi,
I am trying to use LBPH Face Recognizer in Java (Javacv). I am stuck at one place. Eclipse keep complaining it cannot find createLBPHFaceRecognizer

Looks like you got past this issue, can you please share your import statements.

Thanks
Srijit

Srijit Chandrashekhar Nair

unread,
Oct 17, 2012, 3:21:24 PM10/17/12
to jav...@googlegroups.com
Hi,
I am getting the below exception while I try to use the FaceRecognizer class.. Here is my code..

public class FaceRecognizerUtils {
...
..

private FaceRecognizerUtils() {
FaceRecognizerPtr ptr = createLBPHFaceRecognizer(1, 8, 8, 8, Double.MAX_VALUE);
fr = ptr.get();
loadTrainingData();
}
....

....
public int identifyFace(IplImage image) {

IplImage gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);;
cvCvtColor(image, gray, CV_BGR2GRAY);
int result = fr.predict(gray);
return result;
}

This throws the below exception

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000007feea1adf5f, pid=10548, tid=8644
#
# JRE version: 7.0_07-b11
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.3-b01 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [jniopencv_contrib.dll+0xdf5f]
#

I tried adding the C:\opencv\build\x64\vc10\bin to the path also, but still the same issue.

Can you please help me..Everything works fine if I use the cvEigenDecomposite methods and use a custom Eignenfaces method... But I want to use the LBPH algorithm.

Thanks in advance for any help
Srijit

Samuel Audet

unread,
Oct 21, 2012, 1:48:00 AM10/21/12
to jav...@googlegroups.com
On 2012-10-18 04:21, Srijit Chandrashekhar Nair wrote:
> Hi,
> I am getting the below exception while I try to use the FaceRecognizer
> class.. Here is my code..
>
> public class FaceRecognizerUtils {
> ...
> ..
>
> private FaceRecognizerUtils() {
> FaceRecognizerPtr ptr = createLBPHFaceRecognizer(1, 8, 8, 8, Double.MAX_VALUE);
> fr = ptr.get();
> loadTrainingData();
> }
> ....
>
> ....
> public int identifyFace(IplImage image) {
>
> IplImage gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1);;
> cvCvtColor(image, gray, CV_BGR2GRAY);
> int result = fr.predict(gray);
> return result;
> }
>
> This throws the below exception
>

I don't see any training being done. It won't work without training..

Srijit Chandrashekhar Nair

unread,
Oct 21, 2012, 9:43:13 AM10/21/12
to jav...@googlegroups.com
Hi Samuel,
Thanks for the quick response.


The training is already done and I did not add the code in this forum. I fixed the issue. The issue was that I had the following line of code in the constructor

 FaceRecognizerPtr ptr = createLBPHFaceRecognizer(1, 8, 8, 8, Double.MAX_VALUE); 
 fr = ptr.get(); 

The ptr was not declared at class level. So t he ptr went out of scope and garbage collected and whenever I was working on the model, it was crashing.. I fixed it.. moved the ptr to calss level, its working fine now.


I want to thank you for your support and kudos to such a great library you created.

Thanks again
Srijit

Samuel Audet

unread,
Oct 21, 2012, 9:55:35 AM10/21/12
to jav...@googlegroups.com
On 2012-10-21 22:43, Srijit Chandrashekhar Nair wrote:
> Hi Samuel,
> Thanks for the quick response.
>
>
> The training is already done and I did not add the code in this forum. I
> fixed the issue. The issue was that I had the following line of code in
> the constructor
>
> FaceRecognizerPtr ptr = createLBPHFaceRecognizer(1, 8, 8, 8,
> Double.MAX_VALUE);
> fr = ptr.get();
>
> The ptr was not declared at class level. So t he ptr went out of scope
> and garbage collected and whenever I was working on the model, it was
> crashing.. I fixed it.. moved the ptr to calss level, its working fine now.

Ah, right, mapping C++ to Java doesn't always produce obvious results..
The OpenCV API particularly is quite inconsistent :( Thanks for sharing

> I want to thank you for your support and kudos to such a great library
> you created.

Thanks, you're welcome!

Samuel

Srijit Chandrashekhar Nair

unread,
Oct 25, 2012, 12:53:37 PM10/25/12
to jav...@googlegroups.com
Sorry.. I am on vaccation.. I will share the code once I am back..

Thanks
Srijit

On Sunday, October 21, 2012 10:47:46 PM UTC-4, Leandro Diato wrote:
Hi Srijit,

I am trying to do face reconition using the same method (createLBPHFaceRecognizer) but "train" method always throws:

# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_UNCAUGHT_CXX_EXCEPTION (0xe06d7363) at pc=0x000007fefd78caed, pid=5884, tid=7928
#
# JRE version: 6.0_34-b04
# Java VM: Java HotSpot(TM) 64-Bit Server VM (20.9-b04 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C  [KERNELBASE.dll+0xcaed]

Are  you able to share your training process code?

I'll appreciate it. I am stuck with this issue since a few days and I dont know how to resolve it at all.

Thanks


Leandro Diato

unread,
Oct 25, 2012, 5:10:26 PM10/25/12
to jav...@googlegroups.com
Sure, Thanks!

2012/10/25 Srijit Chandrashekhar Nair <sriji...@gmail.com>

Srijit Chandrashekhar Nair

unread,
Oct 26, 2012, 10:55:13 PM10/26/12
to jav...@googlegroups.com
Hi,
I have posted the full code and usage here at my blog


Thanks
Srijit

Samuel Audet

unread,
Oct 28, 2012, 4:36:33 AM10/28/12
to jav...@googlegroups.com
On 2012-10-27 11:55, Srijit Chandrashekhar Nair wrote:
> Hi,
> I have posted the full code and usage here at my blog
>
> http://tech.thecoolblogs.com/2012/10/creating-lbph-local-binary-pattern.html
>
> Please feel free to share how it worked for you

Great! If you could package the code and some of the non-graphics
instructions, the URL, etc. in comments, in one sample .java file, I
would like to add it to the samples directory of JavaCV, thanks!

Samuel

Samuel Audet

unread,
Nov 5, 2012, 6:58:15 AM11/5/12
to jav...@googlegroups.com, Srijit Chandrashekhar Nair
Hello, Srijit,

I wanted to let you know that I've changed the API a little bit for the
better I think with JavaCV 0.3.

Also, if you would like to create a Wiki page on the main site with
graphics and all, instead of providing a sample .java text file, please
let me know! I'll give you access to the Wiki, thanks!

Samuel

Samuel Audet

unread,
Dec 29, 2012, 9:40:53 PM12/29/12
to jav...@googlegroups.com
Hello,

Please try the code here instead:
http://pcbje.com/2012/12/doing-face-recognition-with-javacv/

Samuel

On 12/28/2012 06:27 AM, Iman wrote:
> I have implemented what "Srijit" have posted in both javacv 0.2 and
> javacv 0.3. The LBPH model will be trained successfully but when when i
> try to use "predict" function, i don't get a correct answer .. it always
> returns the last label trained in the model!!! so, no matter what face
> comes to predict .... it always return one label(the last label trained)
> ... please help

Iman Firouzian

unread,
Dec 30, 2012, 10:43:31 AM12/30/12
to jav...@googlegroups.com
Hi,
The website you've mentioned is in maintenance mode. but i think the code is presented here, too: "http://stackoverflow.com/questions/11699744/face-recognition-on-android"
If you (Samuel) meant this code, i should say that it doesn't say anything special. I understood the code good enough.
I have implemented the code and checked that. 
Everything (train and predict) works without any errors but the result of the "predict" function is always the same!!!
It always returns the last label trained.
What do you  think have caused this?
Do you think the problem lies in Javacv wrapper?
--

Best Regards
Iman Firouzian
MSc student in Artificial Intelligence
Computer and IT engineering Department 
Shahrood University of Technology, Iran

Samuel Audet

unread,
Jan 4, 2013, 5:16:54 AM1/4/13
to jav...@googlegroups.com
On 12/31/2012 12:43 AM, Iman Firouzian wrote:
> Hi,
> The website you've mentioned is in maintenance mode. but i think the
> code is presented here, too:
> "http://stackoverflow.com/questions/11699744/face-recognition-on-android"
> If you (Samuel) meant this code, i should say that it doesn't say
> anything special. I understood the code good enough.
> I have implemented the code and checked that.
> Everything (train and predict) works without any errors but the result
> of the "predict" function is always the same!!!
> It always returns the last label trained.
> What do you think have caused this?
> Do you think the problem lies in Javacv wrapper?

Well, if you get the same result without JavaCV then the problem is
elsewhere obviously.

Samuel

Iman Firouzian

unread,
Jan 4, 2013, 7:00:21 AM1/4/13
to jav...@googlegroups.com
Hi,
Yes it was because of the inappropriate value for BinaryThreshold and it was solved by changing it from 127 to 100. 
Thanks again!


On Fri, Jan 4, 2013 at 1:46 PM, Samuel Audet <samuel...@gmail.com> wrote:
, if you get the same result without JavaCV then the problem is elsewhere obviou



Samuel Audet

unread,
Feb 2, 2013, 12:06:00 AM2/2/13
to jav...@googlegroups.com
On 02/01/2013 10:03 PM, s.saad wrote:
> I am trying to implement "Srijit"code using JavaCV 0.3 but I can not find the class *FaceRecognizerPtr* . Do the APIs chnaged a lot in version 0.3?

Yes, it changed bit. Basically we don't need FaceRecognizerPtr anymore.
Just remove it and use FaceRecognizer instead.

Samuel

s.saad

unread,
Feb 2, 2013, 1:15:12 AM2/2/13
to jav...@googlegroups.com
Thank you very much

Samuel Audet

unread,
Feb 17, 2013, 8:01:16 AM2/17/13
to jav...@googlegroups.com
On 02/14/2013 01:46 AM, Chandrashekhar Dhakite wrote:
> I am trying to implement the face recognition using java cv . ref
> http://tech.thecoolblogs.com/2012/10/creating-lbph-local-binary-pattern.html
> However i am getting compile time error "FaceRecognizerPtr cannot be
> resolved to a type"

Try this one here instead:
http://stackoverflow.com/questions/11699744/face-recognition-on-android#11700665

Ankita Padmanabhan

unread,
Mar 13, 2013, 2:30:08 PM3/13/13
to jav...@googlegroups.com
Samuel...
I m working with javacv on face detection over age....that is say the person is currently 25...so the dataset will have his image when he was 15-20...n we should match it...
Can u help me with the code...
I m working on the periocular region of the face...for the purpose....so can u help with walsh hadamard lbp process code....
i m constantly getting errors with it...

Samuel Audet

unread,
Mar 16, 2013, 7:27:15 AM3/16/13
to jav...@googlegroups.com
Hello,

I'm no expert in face recognition, but I'm pretty sure this code works:
http://pcbje.com/2012/12/doing-face-recognition-with-javacv/
Does that help?

Samuel
Reply all
Reply to author
Forward
0 new messages