I'm trying to recognize two different objects using two different cascade classifiers. I modified the face recognition samples but I have a problem: when I try to recognize object from object1.xml it prints both rectangles, when I try to recognize object from object2.xml it doesn't print nothign...
Where is the problem??
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Load native library after(!) OpenCV initialization
System.loadLibrary("detection_based_tracker");
try {
// load cascade file from application resources
InputStream is = getResources().openRawResource(R.raw.object1);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "object1.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
/* By me */
InputStream is2 = getResources().openRawResource(R.raw.object2);
mCascadeFile2 = new File(cascadeDir, "object2.xml");
FileOutputStream os2 = new FileOutputStream(mCascadeFile2);
byte[] buffer2 = new byte[4096];
int bytesRead2;
while ((bytesRead2 = is2.read(buffer2)) != -1) {
os2.write(buffer2, 0, bytesRead2);
}
is2.close();
os2.close();
/* ............... */
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
/* By me */
mJavaDetector2 = new CascadeClassifier(mCascadeFile2.getAbsolutePath());
/* ........ */
if (mJavaDetector.empty() || mJavaDetector2.empty()) {
Log.e(TAG, "Failed to load cascade classifier");
mJavaDetector = null;
/* By me */
mJavaDetector2 = null;
} else
Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath() + mCascadeFile2.getAbsolutePath());
mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);
/**/
mNativeDetector2 = new DetectionBasedTracker(mCascadeFile2.getAbsolutePath(), 0);
cascadeDir.delete();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
>
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
if (mAbsoluteFaceSize == 0) {
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0) {
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
/* ...... */
mNativeDetector2.setMinFaceSize(mAbsoluteFaceSize);
}
MatOfRect faces = new MatOfRect();
/* ....... */
MatOfRect faces2 = new MatOfRect();
if (mDetectorType == JAVA_DETECTOR) {
if (mJavaDetector != null && mJavaDetector2 != null) {
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
/* ......... */
mJavaDetector2.detectMultiScale(mGray, faces2, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
}
}
else if (mDetectorType == NATIVE_DETECTOR) {
if (mNativeDetector != null && mNativeDetector2 != null){
mNativeDetector.detect(mGray, faces);
/* ......... */
mNativeDetector2.detect(mGray, faces2);
}
}
else {
Log.e(TAG, "Detection method is not selected!");
}
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);
Core.putText(mRgba, "Obj1", facesArray[i].tl(), 1, 2, new Scalar(255, 0, 0, 255), 3);
}
/* ...... */
Rect[] facesArray2 = faces2.toArray();
for (int i = 0; i < facesArray2.length; i++){
Core.rectangle(mRgba, facesArray2[i].tl(), facesArray2[i].br(), FACE_RECT_COLOR, 3);
Core.putText(mRgba, "Obj2", facesArray2[i].tl(), 1, 2, new Scalar(255, 0, 0, 255), 3);
}
return mRgba;
}
Or there is a way to detect different objects from the same .xml??
SOLVED
I update my code in this way:
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i(TAG, "OpenCV loaded successfully");
// Load native library after(!) OpenCV initialization
System.loadLibrary("detection_based_tracker");
try {
// load cascade file from application resources
InputStream is = getResources().openRawResource(R.raw.lbpcascade_frontalface);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "lbpcascade_frontalface.xml");
FileOutputStream os = new FileOutputStream(mCascadeFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
is.close();
os.close();
/* By me */
InputStream is2 = getResources().openRawResource(R.raw.haarcascade_frontalface_alt);
File cascadeDir2 = getDir("cascade2", Context.MODE_PRIVATE);
mCascadeFile2 = new File(cascadeDir2, "haarcascade_frontalface_alt.xml");
FileOutputStream os2 = new FileOutputStream(mCascadeFile2);
byte[] buffer2 = new byte[4096];
int bytesRead2;
while ((bytesRead2 = is2.read(buffer2)) != -1) {
os2.write(buffer2, 0, bytesRead2);
}
is2.close();
os2.close();
/* ............... */
mJavaDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
if (mJavaDetector.empty()) {
Log.e(TAG, "Failed to load cascade classifier");
mJavaDetector = null;
} else
Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath());
/* By me */
mJavaDetector2 = new CascadeClassifier(mCascadeFile2.getAbsolutePath());
/* ........ */
if (mJavaDetector2.empty()) {
Log.e(TAG, "Failed to load cascade classifier" + mCascadeFile.getAbsolutePath());
mJavaDetector2 = null;
} else
Log.i(TAG, "Loaded cascade classifier from " + mCascadeFile.getAbsolutePath() + mCascadeFile2.getAbsolutePath());
mNativeDetector = new DetectionBasedTracker(mCascadeFile.getAbsolutePath(), 0);
/**/
mNativeDetector2 = new DetectionBasedTracker(mCascadeFile2.getAbsolutePath(), 0);
cascadeDir.delete();
cascadeDir2.delete();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to load cascade. Exception thrown: " + e);
}
mOpenCvCameraView.enableView();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
if (mAbsoluteFaceSize == 0) {
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0) {
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
mNativeDetector.setMinFaceSize(mAbsoluteFaceSize);
/* ...... */
mNativeDetector2.setMinFaceSize(mAbsoluteFaceSize);
}
MatOfRect faces = new MatOfRect();
/* ....... */
MatOfRect faces2 = new MatOfRect();
if (mDetectorType == JAVA_DETECTOR) {
if (mJavaDetector != null && mJavaDetector2 != null) {
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
/* ......... */
mJavaDetector2.detectMultiScale(mGray, faces2, 1.1, 2, 2, new Size(mAbsoluteFaceSize, mAbsoluteFaceSize), new Size());
}
}
else if (mDetectorType == NATIVE_DETECTOR) {
if (mNativeDetector != null && mNativeDetector2 != null){
mNativeDetector.detect(mGray, faces);
/* ......... */
mNativeDetector2.detect(mGray, faces2);
}
}
else {
Log.e(TAG, "Detection method is not selected!");
}
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);
Core.putText(mRgba, "Pista Ciclabile", facesArray[i].tl(), 1, 2, new Scalar(255, 0, 0, 255), 3);
}
/* ...... */
Rect[] facesArray2 = faces2.toArray();
for (int j = 0; j < facesArray2.length; j++){
Core.rectangle(mRgba, facesArray2[j].tl(), facesArray2[j].br(), FACE_RECT_COLOR, 3);
Core.putText(mRgba, "Viso", facesArray2[j].tl(), 1, 2, new Scalar(255, 0, 0, 255), 3);
}
return mRgba;
}
And this is the result: http://i43.tinypic.com/15eqvl.png