i really need some help with this one. this error has been haunting me for a month and i still couldn't get the solution right.
so i'm trying to make a face detection using android for my essay.
public class MainActivity extends AppCompatActivity {
//static {System.loadLibrary("openCVLibrary310");}
ImageView imageView;
Button BtnGallery, BtnCamera, BtnProcess;
int x, y, height, width;
Imgproc imgproc = new Imgproc();
Imgcodecs imgcodecs = new Imgcodecs();
String PathImage;
Bitmap btmp, TempImage;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
@Override
protected void onCreate(Bundle savedInstanceState) {
if (!OpenCVLoader.initDebug()) {
// Handle initialization error
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.ShowImage);
BtnGallery = (Button) findViewById(R.id.btnGallery);
BtnCamera = (Button) findViewById(R.id.btnCamera);
BtnProcess = (Button) findViewById(R.id.btnProcess);
BtnGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);//one can be replaced with any action code
}
});
BtnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//zero can be replaced with any action code
}
});
BtnProcess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Uri selectedImage = imageReturnedIntent.getData();
//Uri myImageUri = imageReturnedIntent.getData;
//Uri selectedImage = getImageUri(this, UriImage);
Detector(PathImage);
imageView.setImageURI(getImageUri(MainActivity.this, btmp));
//Intent SendImage = new Intent();
//SendImage.putExtra("imageUri", imageView.toString());
//SendImage.setData(UriImage); //throw ImageUri to another function
//startActivityForResult(SendImage, 2);
}
});
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
PathImage = selectedImage.getPath();
}
break;
case 1:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
PathImage = selectedImage.getPath();
}
break;
}
}
private void Detector(String myImagePath){
InputStream is = getResources().openRawResource(R.raw.haarcascade_frontalface_alt);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
File mCascadeFile = new File(cascadeDir,
"haarcascade_frontalface_alt.xml");
//String detect = Environment.getExternalStorageDirectory()+"/data/haarcascade_frontalface_alt.xml";
CascadeClassifier faceDetector = new CascadeClassifier(mCascadeFile.getAbsolutePath());
faceDetector.load(mCascadeFile.getAbsolutePath());
if (faceDetector.empty()) {
Log.w("FAILED", "Failed to load cascade classifier"+ mCascadeFile.getAbsolutePath());
faceDetector = null;
} else
Log.w("SUCCESSFULL", "Loaded cascade classifier from "
+ mCascadeFile.getAbsolutePath());
Log.e("STEP 2", "ADDING .JPEG");
String trueFilePath = myImagePath+".JPEG";
Mat image = imgcodecs.imread(myImagePath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
MatOfRect facedetect = new MatOfRect();
org.opencv.core.Size MinS = new org.opencv.core.Size(0, 30);
org.opencv.core.Size MaxS = new org.opencv.core.Size(30, 0);
faceDetector.detectMultiScale(image, facedetect, 1.1, 3, 0, MinS, MaxS);
//faceDetector.detectMultiScale(image, facedetect, 1.1, 2);
Mat tmp = new Mat(height, width, CvType.CV_8U, new Scalar(4));
for (Rect rect : facedetect.toArray()) {
imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height),
new Scalar(0, 255, 0));
}//create rectangle
try {
//Imgproc.cvtColor(seedsImage, tmp, Imgproc.COLOR_RGB2BGRA);
Imgproc.cvtColor(image, tmp, Imgproc.COLOR_GRAY2RGBA, 4);
btmp = Bitmap.createBitmap(tmp.cols(), tmp.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(tmp, btmp);
//change Mat to Bitmap
} catch (CvException e) {
Log.d("Exception", e.getMessage());
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
so my application will ask an image from the user it can be either an image from gallery or from camera, after that the application will throw the Uri to String Image to the OpenCV Haarcascade to be processed and then show it again on the main menu. i think my image Uri to String is kinda wrong too but i don't have any idea how to make the OpenCV able to process the image beside of that and when i tried to run the program i got 2 errors. the first one is
CvException [org.opencv.core.CvException: cv::Exception: /Volumes/Linux/builds/master_pack-android/opencv/modules/objdetect/src/cascadedetect.cpp:1639: error: (-215) !empty() in function void cv::CascadeClassifier::detectMultiScale(cv::InputArray, std::vector >&, double, int, int, cv::Size,
and the second one is this one. i got this one after trying to make this line of code
org.opencv.core.Size MinS = new org.opencv.core.Size(0, 30);
org.opencv.core.Size MaxS = new org.opencv.core.Size(30, 0);
faceDetector.detectMultiScale(image, facedetect, 1.1, 3, 0, MinS, MaxS);
java.lang.NullPointerException: Attempt to invoke virtual method 'void org.opencv.objdetect.CascadeClassifier.detectMultiScale(org.opencv.core.Mat, org.opencv.core.MatOfRect, double, int, int, org.opencv.core.Size, org.opencv.core.Size)' on a null object
i think the error is because my program can't read the haarcascade_frontalface_alt.xml. i have place the XML file on android resource, on android external and internal device and trying to call them using function Environment.getExternalStorageDirectory() and Environment.getDataDirectory()but still can't detect them.
please help i'm very desperate here. my lecturer suggest using a web service but i didn't tell me what should i learn to make it. i mean i don't know what should i learn first to make it. i really appreciate for any input here. Thank you