การแบ่งกลุ่มเรื่องด้วย ML Kit สำหรับ Android

ใช้ ML Kit เพื่อเพิ่มฟีเจอร์การแบ่งกลุ่มวัตถุลงในแอปได้อย่างง่ายดาย

ฟีเจอร์ รายละเอียด
ชื่อ SDK play-services-mlkit-subject-segmentation
การใช้งาน Unbundled: ระบบจะดาวน์โหลดโมเดลแบบไดนามิกโดยใช้บริการ Google Play
ผลกระทบต่อขนาดแอป ขนาดเพิ่มขึ้นประมาณ 200 KB
เวลาในการเริ่มต้น ผู้ใช้อาจต้องรอให้ดาวน์โหลดโมเดลก่อนจึงจะใช้งานครั้งแรกได้

ลองเลย

ก่อนเริ่มต้น

  1. ในไฟล์ build.gradle ระดับโปรเจ็กต์ ให้ตรวจสอบว่าได้รวมที่เก็บ Maven ของ Google ไว้ในทั้งส่วน buildscript และ allprojects
  2. เพิ่มทรัพยากร Dependency สำหรับไลบรารีการแบ่งกลุ่มวัตถุของ ML Kit ลงในไฟล์ Gradle ระดับแอปของโมดูล ซึ่งโดยปกติคือ app/build.gradle
dependencies {
   implementation 'com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1'
}

ตามที่กล่าวไว้ข้างต้น บริการ Google Play จะเป็นผู้จัดหาโมเดล คุณสามารถกำหนดค่าแอปให้ดาวน์โหลดโมเดลลงในอุปกรณ์โดยอัตโนมัติหลังจากติดตั้งแอปจาก Play Store โดยเพิ่มการประกาศต่อไปนี้ลงในไฟล์ 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 อย่างชัดแจ้งได้ด้วย 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() ซึ่งจะมีประโยชน์เมื่อคุณใช้ Intent 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

กำหนดตัวเลือก Segmenter

หากต้องการแบ่งกลุ่มรูปภาพ ให้สร้างอินสแตนซ์ของ 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();
บิตแมปเบื้องหน้า

ในทำนองเดียวกัน คุณยังรับบิตแมปของวัตถุที่อยู่เบื้องหน้าได้ด้วย

การเรียก 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()
บิตแมปหลายวัตถุ

และในทำนองเดียวกัน คุณสามารถเปิดใช้บิตแมปสำหรับแต่ละวิชาได้ดังนี้

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
);

นอกจากนี้ คุณยังเข้าถึงบิตแมปของวัตถุที่แบ่งกลุ่มแต่ละรายการได้ดังนี้

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 พิกเซล
  • การโฟกัสรูปภาพที่ไม่ดีอาจส่งผลต่อความถูกต้องด้วยเช่นกัน หากไม่ได้ผลลัพธ์ที่ยอมรับได้ โปรดขอให้ผู้ใช้จับภาพอีกครั้ง