我需要使用android studio和haarcascade
工具对耳朵进行简单的检测,所有这些操作都是在Java中完成的(由于内部限制,我无法使用c ++)而不使用本机库。
这是我当前的代码,但是相机过了一会儿就关闭了,什么也没检测到。
我在这种事情上是全新的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
disableRotationAnim();
findViewById(R.id.ToogleCameraFAB).getBackground().setAlpha(120);
findViewById(R.id.saveButton).getBackground().setAlpha(120);
if(getIntent().getStringExtra("selectedEar").equals("Destro")) {
haarcascadeResource = R.raw.haarcascade_mcs_rightear;
}
else if(getIntent().getStringExtra("selectedEar").equals("Sinistro")) {
haarcascadeResource = R.raw.haarcascade_mcs_leftear;
}
NUMBER_OF_FRAMES_TO_DETECT = 15;
MARGIN_ERROR_OF_FRAMES = 2;
FIXED_BORDER = 20;
currentMarginError = MARGIN_ERROR_OF_FRAMES;
Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
rotation = display.getRotation();
mOpenCvCameraView.setRotationValue(rotation);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
private void disableRotationAnim() {
int rotationAnimation = WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.rotationAnimation = rotationAnimation;
win.setAttributes(winParams);
}
@Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_4_0, this, mOpenCVCallBack);
}
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped() {
mRgba.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
Mat mGray = inputFrame.gray();
mRgba = inputFrame.rgba();
if(rotation == Surface.ROTATION_0) {
Core.flip(mGray, mGray, Core.ROTATE_90_COUNTERCLOCKWISE);
Core.flip(mRgba, mRgba, Core.ROTATE_90_COUNTERCLOCKWISE);
} else if(rotation == Surface.ROTATION_270){
Core.flip(mGray, mGray, Core.ROTATE_180);
Core.flip(mRgba, mRgba, Core.ROTATE_180);
}
MatOfRect earRect = new MatOfRect();
if(haarCascade != null)
haarCascade.detectMultiScale(mGray, earRect, 1.1, 2, 2, new Size(200,200), new Size());
Rect[] earArray = earRect.toArray();
Mat cropedMat = mRgba.clone();
if(canTakePicture && earArray.length>0){
int fixedX = earArray[0].x - (FIXED_BORDER);
int fixedY = earArray[0].y - (FIXED_BORDER);
int fixedWidth = earArray[0].width + (FIXED_BORDER * 2);
int fixedHeight = earArray[0].height + (FIXED_BORDER * 2);
if (fixedX>=0 && fixedY >= 0 && fixedWidth+fixedX<=cropedMat.width() && fixedHeight+fixedY<=cropedMat.height()){
Imgproc.rectangle(mRgba, earArray[0].tl(), earArray[0].br(), new Scalar(100), 3);
consecutiveDetects++;
} else {
if(currentMarginError>0 && consecutiveDetects>0)
currentMarginError--;
else {
consecutiveDetects = 0;
currentMarginError = MARGIN_ERROR_OF_FRAMES;
}
}
if(consecutiveDetects>NUMBER_OF_FRAMES_TO_DETECT) {
earArray[0].x = fixedX;
earArray[0].y = fixedY;
earArray[0].height = fixedHeight;
earArray[0].width = fixedWidth;
cropedMat = new Mat(cropedMat, earArray[0]);
preview = Bitmap.createBitmap(cropedMat.cols(), cropedMat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(cropedMat, preview);
canTakePicture = false;
consecutiveDetects = 0;
currentMarginError = MARGIN_ERROR_OF_FRAMES;
}
}
if(rotation == Surface.ROTATION_0) {
Core.flip(mRgba, mRgba, Core.ROTATE_90_COUNTERCLOCKWISE);
} else if(rotation == Surface.ROTATION_270){
Core.flip(mRgba, mRgba, Core.ROTATE_180);
}
return mRgba;
}
@Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
public void onDestroy(){
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
mItemSwitchCamera = menu.add("Toggle Front/Back camera");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item == mItemSwitchCamera)
onToggleCamera(null);
return true;
}
public void onToggleCamera(View v){
String toastMessage = "";
mOpenCvCameraView.setVisibility(SurfaceView.GONE);
mIsFrontCamera = !mIsFrontCamera;
if (mIsFrontCamera) {
mOpenCvCameraView.setCameraIndex(1);
toastMessage = "Front Camera";
} else {
mOpenCvCameraView.setCameraIndex(-1);
toastMessage = "Back Camera";
}
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
mOpenCvCameraView.enableView();
Toast toast = Toast.makeText(this, toastMessage, Toast.LENGTH_LONG);
toast.show();
}
private final BaseLoaderCallback mOpenCVCallBack = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
try {
InputStream is = getResources().openRawResource(haarcascadeResource);
File cascadeDir = getDir("cascade", Context.MODE_PRIVATE);
mCascadeFile = new File(cascadeDir, "cascade.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();;
haarCascade = new CascadeClassifier(mCascadeFile.getAbsolutePath());
if (haarCascade.empty()) {
Log.i("Cascade Error", "Failed to load cascade classifier");
haarCascade = null;
}
} catch (Exception e) {
Log.i("Cascade Error: ", "Cascase not found");
}
mOpenCvCameraView.enableView();
break;
default:
super.onManagerConnected(status);
break;
}
}
};