6

I'm implementing a project which can be based on Detecting faces and I want to use Firebase Ml Kit to detect the faces. But When I select the One image and search over the Camera and gallery images and get the same image object result. and my question is how to get two images (Maybe its differ and maybe it's same) of an object is the same or not in android. Here is the java code for single image Ml and I don't know how to compare another image ML into the first one Image ML Please have you guys any logic then share.

public class MainActivity extends AppCompatActivity {

    TextView mText;
    Button mButton;
    ImageView mImageView;
    ProgressBar mBar;
    private GraphicOverlay mGraphicOverlay;
    private static final String TAG = "FaceTracker";
    private CameraSource mCameraSource = null;
    private CameraSourcePreview mPreview;

    private static final int RC_HANDLE_GMS = 9001;
    // permission request codes need to be < 256
    private static final int RC_HANDLE_CAMERA_PERM = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PermissionCheck();
    }

    private void PermissionCheck() {
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            // Permission is not granted
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.ACCESS_NETWORK_STATE},
                        1);

                // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                // app-defined int constant. The callback method gets the
                // result of the request.
            }
        } else {

            Initilization();
        }
    }

    private void Initilization() {

        mPreview = (CameraSourcePreview) findViewById(R.id.preview);
        mGraphicOverlay = (GraphicOverlay) findViewById(R.id.faceOverlay);
        mImageView = (ImageView) findViewById(R.id.imageview);
        mButton = (Button) findViewById(R.id.detect_buttton);
        mText = (TextView) findViewById(R.id.text_view);
        mBar = (ProgressBar) findViewById(R.id.progressbar);

        mImageView.setImageDrawable(getDrawable(R.drawable.dhoni1));
        runFaceDetector(imageView2Bitmap(mImageView));


    }

    private Bitmap imageView2Bitmap(ImageView view) {
        Bitmap bitmap = ((BitmapDrawable) view.getDrawable()).getBitmap();
        return bitmap;
    }



    private void runFaceDetector(Bitmap bitmap) {

        mBar.setVisibility(View.VISIBLE);
        FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
        FirebaseVisionFaceDetectorOptions options = new FirebaseVisionFaceDetectorOptions.Builder().build();
        FirebaseVisionFaceDetector detector = FirebaseVision.getInstance().getVisionFaceDetector(options);
        detector.detectInImage(image).addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionFace>>() {
            @Override
            public void onSuccess(List<FirebaseVisionFace> firebaseVisionFaces) {
                processFaceResult(firebaseVisionFaces);
            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        mBar.setVisibility(View.GONE);
                        Toast.makeText(MainActivity.this, "Error Face " + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

    }

    private void processFaceResult(List<FirebaseVisionFace> firebaseVisionFaces) {
        for (FirebaseVisionFace face : firebaseVisionFaces) {
            Rect bounds = face.getBoundingBox();
            face.getContour(0);
            face.getHeadEulerAngleY();

            mText.setText(face.getTrackingId() + " ||  " + face.getHeadEulerAngleY() + " ||  " + face.getHeadEulerAngleZ()
                    + " || " + face.getLeftEyeOpenProbability() + " ||  " + face.getRightEyeOpenProbability() + " || " + face.getSmilingProbability()
                    + " ||  " + face.getBoundingBox().bottom + " || " + face.getBoundingBox().top + " || " + face.getBoundingBox().right
                    + " || " + face.getBoundingBox().left + " || " + face.getLandmark(0));

        }
        mBar.setVisibility(View.GONE);
    }

}

XML code:

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageview"
    android:layout_width="match_parent"
    android:layout_height="250dp" />
<Button
    android:id="@+id/detect_button"
    android:layout_width="match_parent"
    android:text="Detect"
    android:layout_below="@+id/imageview"
    android:layout_height="wrap_content" />
<TextView
    android:layout_width="match_parent"
    android:layout_below="@+id/detect_buttton"
    android:id="@+id/text_view"
    android:layout_height="wrap_content" />
<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:id="@+id/progressbar"/>
Mohit Lakhanpal
  • 341
  • 1
  • 5
  • 15

0 Answers0