פילוח לפי נושאים באמצעות ML Kit ל-Android

אתם יכולים להשתמש ב-ML Kit כדי להוסיף בקלות לאפליקציה תכונות של פילוח נושאים.

תכונה פרטים
שם ה-SDK play-services-mlkit-subject-segmentation
הטמעה ללא חבילה: המודל מוריד באופן דינמי באמצעות Google Play Services.
ההשפעה של גודל האפליקציה הגדלת גודל של כ-200KB.
זמן האתחול יכול להיות שהמשתמשים יצטרכו להמתין להורדת המודל לפני השימוש הראשון.

רוצה לנסות?

לפני שמתחילים

  1. בקובץ build.gradle ברמת הפרויקט, חשוב לכלול את מאגר Maven של Google גם בקטע buildscript וגם בקטע allprojects.
  2. מוסיפים את התלות בספריית פילוח הנושאים של ML Kit לקובץ ה-Gradle ברמת האפליקציה של המודול, שבדרך כלל הוא app/build.gradle:
dependencies {
   implementation 'com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1'
}

כפי שצוין למעלה, המודל מסופק על ידי Google Play Services. אפשר להגדיר שהאפליקציה תוריד את המודל למכשיר באופן אוטומטי אחרי ההתקנה שלה מחנות Play. כדי לעשות זאת, מוסיפים את ההצהרה הבאה לקובץ AndroidManifest.xml של האפליקציה:

<application ...>
      ...
      <meta-data
          android:name="com.google.mlkit.vision.DEPENDENCIES"
          android:value="subject_segment" >
      <!-- To use multiple models: android:value="subject_segment,model2,model3" -->
</application>

אפשר גם לבדוק באופן מפורש את הזמינות של המודל ולבקש הורדה דרך Google Play Services באמצעות ModuleInstallClient API.

אם לא מפעילים הורדות של מודלים בזמן ההתקנה או מבקשים הורדה מפורשת, המודלים יורדו בפעם הראשונה שתפעילו את הכלי ליצירת פלחים. בקשות שתשלחו לפני שההורדה תושלם לא יחזירו תוצאות.

1. הכנת קובץ הקלט

כדי לבצע פילוח של תמונה, יוצרים אובייקט InputImage מ-Bitmap, מ-media.Image, מ-ByteBuffer, ממערך בייטים או מקובץ במכשיר.

אפשר ליצור אובייקט InputImage ממקורות שונים, והסבר על כל אחד מהם מופיע בהמשך.

שימוש ב-media.Image

כדי ליצור אובייקט InputImage מאובייקט media.Image, למשל כשמעבירים תמונה ממצלמת המכשיר, מעבירים את האובייקט media.Image ואת סיבוב התמונה אל InputImage.fromMediaImage().

אם אתם משתמשים בספרייה CameraX, הערך של הזווית מחושב בשבילכם על ידי הכיתות OnImageCapturedListener ו-ImageAnalysis.Analyzer.

Kotlin

private class YourImageAnalyzer : ImageAnalysis.Analyzer {

    override fun analyze(imageProxy: ImageProxy) {
        val mediaImage = imageProxy.image
        if (mediaImage != null) {
            val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
            // Pass image to an ML Kit Vision API
            // ...
        }
    }
}

Java

private class YourAnalyzer implements ImageAnalysis.Analyzer {

    @Override
    public void analyze(ImageProxy imageProxy) {
        Image mediaImage = imageProxy.getImage();
        if (mediaImage != null) {
          InputImage image =
                InputImage.fromMediaImage(mediaImage, imageProxy.getImageInfo().getRotationDegrees());
          // Pass image to an ML Kit Vision API
          // ...
        }
    }
}

אם אתם לא משתמשים בספריית מצלמה שמספקת את מידת הסיבוב של התמונה, תוכלו לחשב אותה לפי מידת הסיבוב של המכשיר והכיוון של חיישן המצלמה במכשיר:

Kotlin

private val ORIENTATIONS = SparseIntArray()

init {
    ORIENTATIONS.append(Surface.ROTATION_0, 0)
    ORIENTATIONS.append(Surface.ROTATION_90, 90)
    ORIENTATIONS.append(Surface.ROTATION_180, 180)
    ORIENTATIONS.append(Surface.ROTATION_270, 270)
}

/**
 * Get the angle by which an image must be rotated given the device's current
 * orientation.
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Throws(CameraAccessException::class)
private fun getRotationCompensation(cameraId: String, activity: Activity, isFrontFacing: Boolean): Int {
    // Get the device's current rotation relative to its "native" orientation.
    // Then, from the ORIENTATIONS table, look up the angle the image must be
    // rotated to compensate for the device's rotation.
    val deviceRotation = activity.windowManager.defaultDisplay.rotation
    var rotationCompensation = ORIENTATIONS.get(deviceRotation)

    // Get the device's sensor orientation.
    val cameraManager = activity.getSystemService(CAMERA_SERVICE) as CameraManager
    val sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION)!!

    if (isFrontFacing) {
        rotationCompensation = (sensorOrientation + rotationCompensation) % 360
    } else { // back-facing
        rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360
    }
    return rotationCompensation
}

Java

private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
    ORIENTATIONS.append(Surface.ROTATION_0, 0);
    ORIENTATIONS.append(Surface.ROTATION_90, 90);
    ORIENTATIONS.append(Surface.ROTATION_180, 180);
    ORIENTATIONS.append(Surface.ROTATION_270, 270);
}

/**
 * Get the angle by which an image must be rotated given the device's current
 * orientation.
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private int getRotationCompensation(String cameraId, Activity activity, boolean isFrontFacing)
        throws CameraAccessException {
    // Get the device's current rotation relative to its "native" orientation.
    // Then, from the ORIENTATIONS table, look up the angle the image must be
    // rotated to compensate for the device's rotation.
    int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    int rotationCompensation = ORIENTATIONS.get(deviceRotation);

    // Get the device's sensor orientation.
    CameraManager cameraManager = (CameraManager) activity.getSystemService(CAMERA_SERVICE);
    int sensorOrientation = cameraManager
            .getCameraCharacteristics(cameraId)
            .get(CameraCharacteristics.SENSOR_ORIENTATION);

    if (isFrontFacing) {
        rotationCompensation = (sensorOrientation + rotationCompensation) % 360;
    } else { // back-facing
        rotationCompensation = (sensorOrientation - rotationCompensation + 360) % 360;
    }
    return rotationCompensation;
}

לאחר מכן מעבירים את האובייקט media.Image ואת הערך של דרגת הסיבוב אל InputImage.fromMediaImage():

Kotlin

val image = InputImage.fromMediaImage(mediaImage, rotation)

Java

InputImage image = InputImage.fromMediaImage(mediaImage, rotation);

שימוש ב-URI של קובץ

כדי ליצור אובייקט InputImage מכתובת URI של קובץ, מעבירים את הקשר של האפליקציה ואת כתובת ה-URI של הקובץ ל-InputImage.fromFilePath(). אפשר להשתמש באפשרות הזו כשמשתמשים בכוונה ACTION_GET_CONTENT כדי לבקש מהמשתמש לבחור תמונה מאפליקציית הגלריה שלו.

Kotlin

val image: InputImage
try {
    image = InputImage.fromFilePath(context, uri)
} catch (e: IOException) {
    e.printStackTrace()
}

Java

InputImage image;
try {
    image = InputImage.fromFilePath(context, uri);
} catch (IOException e) {
    e.printStackTrace();
}

שימוש ב-ByteBuffer או ב-ByteArray

כדי ליצור אובייקט InputImage מ-ByteBuffer או מ-ByteArray, קודם מחשבים את מידת הסיבוב של התמונה כפי שמתואר למעלה לגבי קלט media.Image. לאחר מכן, יוצרים את האובייקט InputImage עם המאגר או המערך, יחד עם הגובה, הרוחב, פורמט קידוד הצבע ומידת הסיבוב של התמונה:

Kotlin

val image = InputImage.fromByteBuffer(
        byteBuffer,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
)
// Or:
val image = InputImage.fromByteArray(
        byteArray,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
)

Java

InputImage image = InputImage.fromByteBuffer(byteBuffer,
        /* image width */ 480,
        /* image height */ 360,
        rotationDegrees,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
);
// Or:
InputImage image = InputImage.fromByteArray(
        byteArray,
        /* image width */480,
        /* image height */360,
        rotation,
        InputImage.IMAGE_FORMAT_NV21 // or IMAGE_FORMAT_YV12
);

שימוש ב-Bitmap

כדי ליצור אובייקט InputImage מתוך אובייקט Bitmap, צריך להצהיר על כך באופן הבא:

Kotlin

val image = InputImage.fromBitmap(bitmap, 0)

Java

InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);

התמונה מיוצגת על ידי אובייקט Bitmap יחד עם מעלות הסיבוב.

2. יצירת מופע של SubjectSegmenter

הגדרת האפשרויות של הכלי ליצירת פלחים

כדי לפלח את התמונה, קודם יוצרים מופע של SubjectSegmenterOptions באופן הבא:

Kotlin

val options = SubjectSegmenterOptions.Builder()
       // enable options
       .build()

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        // enable options
        .build();

אלה הפרטים של כל אחת מהאפשרויות:

מסכת אמינות של חזית

המסכה של מידת האמון בחזית מאפשרת להבחין בין נושא החזית לבין הרקע.

קריאה ל-enableForegroundConfidenceMask() באפשרויות מאפשרת לאחזור את המסכה של חזית התמונה על ידי קריאה ל-getForegroundMask() באובייקט SubjectSegmentationResult שמוחזר אחרי עיבוד התמונה.

Kotlin

val options = SubjectSegmenterOptions.Builder()
        .enableForegroundConfidenceMask()
        .build()

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        .enableForegroundConfidenceMask()
        .build();
מפת סיביות של חזית

באופן דומה, אפשר גם לקבל קובץ bitmap של הנושא שבחזית.

קריאה לפונקציה enableForegroundBitmap() באפשרויות מאפשרת לאחזור מאוחר יותר את הבייטמאפ של חזית התמונה על ידי קריאה לפונקציה getForegroundBitmap() באובייקט SubjectSegmentationResult שמוחזר אחרי עיבוד התמונה.

Kotlin

val options = SubjectSegmenterOptions.Builder()
        .enableForegroundBitmap()
        .build()

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        .enableForegroundBitmap()
        .build();
מסכת אמון של כמה נושאים

בדומה לאפשרויות של חזית התמונה, אפשר להשתמש ב-SubjectResultOptions כדי להפעיל את מסכת האמון לכל נושא בחזית התמונה באופן הבא:

Kotlin

val subjectResultOptions = SubjectSegmenterOptions.SubjectResultOptions.Builder()
    .enableConfidenceMask()
    .build()

val options = SubjectSegmenterOptions.Builder()
    .enableMultipleSubjects(subjectResultOptions)
    .build()

Java

SubjectResultOptions subjectResultOptions =
        new SubjectSegmenterOptions.SubjectResultOptions.Builder()
            .enableConfidenceMask()
            .build()

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
      .enableMultipleSubjects(subjectResultOptions)
      .build()
מפת סיביות עם כמה נושאים

באופן דומה, אפשר להפעיל את קובץ ה-bitmap לכל נושא:

Kotlin

val subjectResultOptions = SubjectSegmenterOptions.SubjectResultOptions.Builder()
    .enableSubjectBitmap()
    .build()

val options = SubjectSegmenterOptions.Builder()
    .enableMultipleSubjects(subjectResultOptions)
    .build()

Java

SubjectResultOptions subjectResultOptions =
      new SubjectSegmenterOptions.SubjectResultOptions.Builder()
        .enableSubjectBitmap()
        .build()

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
      .enableMultipleSubjects(subjectResultOptions)
      .build()

יצירת הכלי לחלוקת נושאים

אחרי שמציינים את האפשרויות של SubjectSegmenterOptions, יוצרים מופע SubjectSegmenter שמפעיל את getClient() ומעביר את האפשרויות כפרמטר:

Kotlin

val segmenter = SubjectSegmentation.getClient(options)

Java

SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);

3. עיבוד תמונה

מעבירים את האובייקט InputImage שהכנו לשיטה process של SubjectSegmenter:

Kotlin

segmenter.process(inputImage)
    .addOnSuccessListener { result ->
        // Task completed successfully
        // ...
    }
    .addOnFailureListener { e ->
        // Task failed with an exception
        // ...
    }

Java

segmenter.process(inputImage)
    .addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(SubjectSegmentationResult result) {
                // Task completed successfully
                // ...
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Task failed with an exception
                // ...
            }
        });

4. הצגת התוצאה של פילוח הנושאים

אחזור של מסכות ותמונות ביטמפט של חזית

אחרי העיבוד, אפשר לאחזר את המסכה של החזית של התמונה שמפעילה את getForegroundConfidenceMask() באופן הבא:

Kotlin

val colors = IntArray(image.width * image.height)

val foregroundMask = result.foregroundConfidenceMask
for (i in 0 until image.width * image.height) {
  if (foregroundMask[i] > 0.5f) {
    colors[i] = Color.argb(128, 255, 0, 255)
  }
}

val bitmapMask = Bitmap.createBitmap(
  colors, image.width, image.height, Bitmap.Config.ARGB_8888
)

Java

int[] colors = new int[image.getWidth() * image.getHeight()];

FloatBuffer foregroundMask = result.getForegroundConfidenceMask();
for (int i = 0; i < image.getWidth() * image.getHeight(); i++) {
  if (foregroundMask.get() > 0.5f) {
    colors[i] = Color.argb(128, 255, 0, 255);
  }
}

Bitmap bitmapMask = Bitmap.createBitmap(
      colors, image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888
);

אפשר גם לאחזר בייטמאפ של חזית התמונה באמצעות קריאה ל-getForegroundBitmap():

Kotlin

val foregroundBitmap = result.foregroundBitmap

Java

Bitmap foregroundBitmap = result.getForegroundBitmap();

אחזור של מסכות ותמונות ביטמפי לכל נושא

באופן דומה, אפשר לאחזר את המסכה של הנושאים המפולחים על ידי קריאה ל-getConfidenceMask() בכל נושא באופן הבא:

Kotlin

val subjects = result.subjects

val colors = IntArray(image.width * image.height)
for (subject in subjects) {
  val mask = subject.confidenceMask
  for (i in 0 until subject.width * subject.height) {
    val confidence = mask[i]
    if (confidence > 0.5f) {
      colors[image.width * (subject.startY - 1) + subject.startX] =
          Color.argb(128, 255, 0, 255)
    }
  }
}

val bitmapMask = Bitmap.createBitmap(
  colors, image.width, image.height, Bitmap.Config.ARGB_8888
)

Java

List subjects = result.getSubjects();

int[] colors = new int[image.getWidth() * image.getHeight()];
for (Subject subject : subjects) {
  FloatBuffer mask = subject.getConfidenceMask();
  for (int i = 0; i < subject.getWidth() * subject.getHeight(); i++) {
    float confidence = mask.get();
    if (confidence > 0.5f) {
      colors[width * (subject.getStartY() - 1) + subject.getStartX()]
          = Color.argb(128, 255, 0, 255);
    }
  }
}

Bitmap bitmapMask = Bitmap.createBitmap(
  colors, image.width, image.height, Bitmap.Config.ARGB_8888
);

אפשר גם לגשת לקובץ ה-bitmap של כל נושא מפולח באופן הבא:

Kotlin

val bitmaps = mutableListOf()
for (subject in subjects) {
  bitmaps.add(subject.bitmap)
}

Java

List bitmaps = new ArrayList<>();
for (Subject subject : subjects) {
  bitmaps.add(subject.getBitmap());
}

טיפים לשיפור הביצועים

בכל סשן של אפליקציה, ההסקה הראשונה לרוב איטית יותר מההסקות הבאות בגלל אתחול המודל. אם זמן האחזור הנמוך קריטי, כדאי להפעיל מראש הסקת 'דמה'.

איכות התוצאות תלויה באיכות של קובץ הקלט:

  • כדי ש-ML Kit ייצור תוצאה מדויקת של פילוח, התמונה צריכה להיות בגודל 512x512 פיקסלים לפחות.
  • גם מיקוד לקוי של התמונה יכול להשפיע על הדיוק. אם התוצאות לא יהיו טובות, בקשו מהמשתמש לצלם מחדש את התמונה.