Search in sources :

Example 6 with Frame

use of com.google.android.gms.vision.Frame in project android-vision by googlesamples.

the class PhotoViewerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_viewer);
    InputStream stream = getResources().openRawResource(R.raw.face);
    Bitmap bitmap = BitmapFactory.decodeStream(stream);
    // A new face detector is created for detecting the face and its landmarks.
    // 
    // Setting "tracking enabled" to false is recommended for detection with unrelated
    // individual images (as opposed to video or a series of consecutively captured still
    // images).  For detection on unrelated individual images, this will give a more accurate
    // result.  For detection on consecutive images (e.g., live video), tracking gives a more
    // accurate (and faster) result.
    // 
    // By default, landmark detection is not enabled since it increases detection time.  We
    // enable it here in order to visualize detected landmarks.
    FaceDetector detector = new FaceDetector.Builder(getApplicationContext()).setTrackingEnabled(false).setLandmarkType(FaceDetector.ALL_LANDMARKS).build();
    // This is a temporary workaround for a bug in the face detector with respect to operating
    // on very small images.  This will be fixed in a future release.  But in the near term, use
    // of the SafeFaceDetector class will patch the issue.
    Detector<Face> safeDetector = new SafeFaceDetector(detector);
    // Create a frame from the bitmap and run face detection on the frame.
    Frame frame = new Frame.Builder().setBitmap(bitmap).build();
    SparseArray<Face> faces = safeDetector.detect(frame);
    if (!safeDetector.isOperational()) {
        // Note: The first time that an app using face API is installed on a device, GMS will
        // download a native library to the device in order to do detection.  Usually this
        // completes before the app is run for the first time.  But if that download has not yet
        // completed, then the above call will not detect any faces.
        // 
        // isOperational() can be used to check if the required native library is currently
        // available.  The detector will automatically become operational once the library
        // download completes on device.
        Log.w(TAG, "Face detector dependencies are not yet available.");
        // Check for low storage.  If there is low storage, the native library will not be
        // downloaded, so detection will not become operational.
        IntentFilter lowstorageFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
        boolean hasLowStorage = registerReceiver(null, lowstorageFilter) != null;
        if (hasLowStorage) {
            Toast.makeText(this, R.string.low_storage_error, Toast.LENGTH_LONG).show();
            Log.w(TAG, getString(R.string.low_storage_error));
        }
    }
    FaceView overlay = (FaceView) findViewById(R.id.faceView);
    overlay.setContent(bitmap, faces);
    // Although detector may be used multiple times for different images, it should be released
    // when it is no longer needed in order to free native resources.
    safeDetector.release();
}
Also used : IntentFilter(android.content.IntentFilter) Frame(com.google.android.gms.vision.Frame) SafeFaceDetector(com.google.android.gms.samples.vision.face.patch.SafeFaceDetector) InputStream(java.io.InputStream) Bitmap(android.graphics.Bitmap) FaceDetector(com.google.android.gms.vision.face.FaceDetector) SafeFaceDetector(com.google.android.gms.samples.vision.face.patch.SafeFaceDetector) Face(com.google.android.gms.vision.face.Face)

Aggregations

Frame (com.google.android.gms.vision.Frame)6 ByteBuffer (java.nio.ByteBuffer)3 Bitmap (android.graphics.Bitmap)2 IOException (java.io.IOException)2 IntentFilter (android.content.IntentFilter)1 MainThread (android.support.annotation.MainThread)1 UiThread (android.support.annotation.UiThread)1 SurfaceHolder (android.view.SurfaceHolder)1 SurfaceView (android.view.SurfaceView)1 SafeFaceDetector (com.google.android.gms.samples.vision.face.patch.SafeFaceDetector)1 Detector (com.google.android.gms.vision.Detector)1 Barcode (com.google.android.gms.vision.barcode.Barcode)1 Face (com.google.android.gms.vision.face.Face)1 FaceDetector (com.google.android.gms.vision.face.FaceDetector)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1