I'm developing a project about face recognition on android. I meet a problem after detect face region [closed]
I meet a problem in the onCameraFrame() function. After detect face in the function, I want to get the face region, and then create a new Mat to save the face area. but there are a problem.My code is:
mRgba = inputFrame.rgba();
Imgproc.cvtColor(mRgba, mGray, Imgproc.COLOR_RGB2GRAY);
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0) {
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
MatOfRect faces = new MatOfRect();
if (mJavaDetector != null)
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++)
Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(), FACE_RECT_COLOR, 3);
Mat faceImg = new Mat(mGray,facesArray[0]);
return mRgba;
I want to get the face area, so I use the gray image to init the new Mat data. My error is
while the program is right if I move the penultimate sentence
Mat faceImg = new Mat(mGray,facesArray[0]);
I want to know why is it. thanks
you will be getting an access violation, if it did not find any faces : facesArray[0]
(would you be so nice, to copy/paste your logcat output , and change the unreadable image above to plain text ?)
yes, the facesArray[0] may be null, can I succeed atfer adding the code : if(facesArray[0]!=null) Mat faceImg = new Mat(mGray,facesArray[0]);
no, still the same problem. you just can't use [0] on an empty array.
if (facesArray.length > 0 ) // do something with facesArray[0]
maybe even put
Mat faceImg = new Mat(mGray,facesArray[0]);
inside the loop, and break out after the 1st )thank you very much, I have sloved this problem. you help me a lot, thanks again