Android के लिए ML किट की मदद से, चीज़ों को अलग-अलग ग्रुप में बांटना

अपने ऐप्लिकेशन में, सब्जेक्ट सेगमेंटेशन सुविधाओं को आसानी से जोड़ने के लिए, एमएल किट का इस्तेमाल करें.

सुविधा जानकारी
Sdk का नाम play-services-mlkit-subject-segmentation
लागू करने का तरीका बंडल न किया गया: मॉडल को Google Play services का इस्तेमाल करके डाइनैमिक तौर पर डाउनलोड किया जाता है.
ऐप्लिकेशन के साइज़ का असर ~200 केबी का साइज़ बढ़ाया जा सकता है.
शुरू करने का समय पहली बार इस्तेमाल करने से पहले, उपयोगकर्ताओं को मॉडल डाउनलोड होने का इंतज़ार करना पड़ सकता है.

इसे आज़माएं

शुरू करने से पहले

  1. प्रोजेक्ट-लेवल की build.gradle फ़ाइल में, अपने buildscript और allprojects, दोनों सेक्शन में Google की Maven रिपॉज़िटरी को शामिल करना न भूलें.
  2. अपने मॉड्यूल की ऐप्लिकेशन-लेवल की ग्रेडल फ़ाइल में, एमएल किट की सब्जेक्ट सेगमेंटेशन लाइब्रेरी के लिए डिपेंडेंसी जोड़ें. आम तौर पर, यह app/build.gradle होती है:
dependencies {
   implementation 'com.google.android.gms:play-services-mlkit-subject-segmentation:16.0.0-beta1'
}

जैसा कि ऊपर बताया गया है, यह मॉडल Google Play services उपलब्ध कराता है. 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 services की मदद से, मॉडल को डाउनलोड करने का अनुरोध करने का विकल्प भी होता है. इसके लिए, आपको ModuleInstallClient API का इस्तेमाल करना होगा.

अगर इंस्टॉल के समय मॉडल डाउनलोड करने की सुविधा चालू नहीं की जाती है या अश्लील कॉन्टेंट डाउनलोड करने का अनुरोध नहीं किया जाता है, तो सेगमेंटर को पहली बार इस्तेमाल करने पर मॉडल डाउनलोड हो जाता है. डाउनलोड पूरा होने से पहले किए गए अनुरोधों का कोई नतीजा नहीं मिलता.

1. इनपुट इमेज तैयार करें

किसी इमेज को सेगमेंट में बांटने के लिए, Bitmap, media.Image, ByteBuffer, बाइट कलेक्शन या डिवाइस पर मौजूद किसी फ़ाइल से InputImage ऑब्जेक्ट बनाएं.

अलग-अलग सोर्स से InputImage ऑब्जेक्ट बनाया जा सकता है. हर सोर्स के बारे में नीचे बताया गया है.

media.Image का इस्तेमाल करके

किसी media.Image ऑब्जेक्ट से InputImage ऑब्जेक्ट बनाने के लिए, जैसे कि किसी डिवाइस के कैमरे से इमेज कैप्चर करने पर, 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);

फ़ाइल यूआरआई का इस्तेमाल करना

किसी फ़ाइल यूआरआई से InputImage ऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन कॉन्टेक्स्ट और फ़ाइल यूआरआई को 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 का इस्तेमाल करके

किसी ByteBuffer या ByteArray से InputImage ऑब्जेक्ट बनाने के लिए, सबसे पहले इमेज के रोटेशन डिग्री का हिसाब लगाएं, जैसा कि 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 का इस्तेमाल करके

किसी Bitmap ऑब्जेक्ट से InputImage ऑब्जेक्ट बनाने के लिए, यह एलान करें:

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() को कॉल करें और बाद में, इमेज प्रोसेस करने के बाद दिखाए गए SubjectSegmentationResult ऑब्जेक्ट पर getForegroundMask() को कॉल करके, फ़ोरग्राउंड मास्क वापस पाया जा सकता है.

Kotlin

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

Java

SubjectSegmenterOptions options = new SubjectSegmenterOptions.Builder()
        .enableForegroundConfidenceMask()
        .build();
फ़ोरग्राउंड बिट मैप

इसी तरह, आपको फ़ोरग्राउंड सब्जेक्ट का बिट मैप भी मिल सकता है.

विकल्पों में enableForegroundBitmap() को कॉल करने से, आपको बाद में फ़ोरग्राउंड बिट मैप को वापस पाने की सुविधा मिलती है. इसके लिए, इमेज को प्रोसेस करने के बाद मिले SubjectSegmentationResult ऑब्जेक्ट पर getForegroundBitmap() को कॉल किया जा सकता है.

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 के विकल्प तय करने के बाद, getClient() को कॉल करने वाला SubjectSegmenter इंस्टेंस बनाएं और विकल्पों को पैरामीटर के तौर पर पास करें:

Kotlin

val segmenter = SubjectSegmentation.getClient(options)

Java

SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);

3. इमेज प्रोसेस करना

तैयार किए गए InputImage ऑब्जेक्ट को SubjectSegmenter के process तरीके से पास करें:

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

परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह

हर ऐप्लिकेशन सेशन के लिए, मॉडल शुरू होने की वजह से पहला अनुमान अक्सर बाद के अनुमानों की तुलना में धीमा होता है. अगर इंतज़ार का समय कम करना ज़रूरी है, तो पहले से ही "डमी" अनुमान लगा दें.

आपके नतीजों की क्वालिटी, इनपुट इमेज की क्वालिटी पर निर्भर करती है:

  • एमएल किट के लिए, इमेज को कम से कम 512x512 पिक्सल का होना चाहिए, ताकि सेगमेंटेशन का सटीक नतीजा मिले.
  • खराब इमेज फ़ोकस भी सटीक जानकारी पर असर डाल सकता है. अगर आपको स्वीकार करने लायक नतीजे नहीं मिलते हैं, तो उपयोगकर्ता से इमेज को फिर से कैप्चर करने के लिए कहें.