अपने ऐप्लिकेशन में, विषय के हिसाब से सेगमेंटेशन की सुविधाएं आसानी से जोड़ने के लिए, ML Kit का इस्तेमाल करें.
| सुविधा | जानकारी |
|---|---|
| एसडीके का नाम | play-services-mlkit-subject-segmentation |
| लागू करना | बंडल में शामिल नहीं: मॉडल को Google Play services का इस्तेमाल करके, डाइनैमिक तरीके से डाउनलोड किया जाता है. |
| ऐप्लिकेशन के साइज़ पर पड़ने वाला असर | साइज़ में करीब 200 केबी की बढ़ोतरी. |
| शुरू होने में लगने वाला समय | पहली बार इस्तेमाल करने से पहले, उपयोगकर्ताओं को मॉडल डाउनलोड होने का इंतज़ार करना पड़ सकता है. |
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन का इस्तेमाल करें.
शुरू करने से पहले
- अपने प्रोजेक्ट-लेवल की
build.gradleफ़ाइल में, पक्का करें किbuildscriptऔरallprojectsदोनों सेक्शन में, Google की Maven रिपॉज़िटरी शामिल हो. - अपने मॉड्यूल की ऐप्लिकेशन-लेवल की Gradle फ़ाइल में, ML Kit के विषय के हिसाब से सेगमेंटेशन की लाइब्रेरी के लिए डिपेंडेंसी जोड़ें. आम तौर पर, यह फ़ाइल
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>
अगर आपने इंस्टॉल के दौरान मॉडल डाउनलोड करने की सुविधा चालू नहीं की है या साफ़ तौर पर डाउनलोड करने का अनुरोध नहीं किया है, तो सेगमेंटर को पहली बार चलाने पर मॉडल डाउनलोड हो जाता है. डाउनलोड पूरा होने से पहले किए गए अनुरोधों के कोई नतीजे नहीं मिलते.
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 विकल्प तय करने के बाद, SubjectSegmenter का इंस्टेंस बनाने के लिए getClient() को कॉल करें और विकल्पों को पैरामीटर के तौर पर पास करें:
Kotlin
val segmenter = SubjectSegmentation.getClient(options)
Java
SubjectSegmenter segmenter = SubjectSegmentation.getClient(options);
3. किसी इमेज को प्रोसेस करना
तैयार किए गए InputImage
ऑब्जेक्ट को, SubjectSegmenter's 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
Listsubjects = 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
Listbitmaps = new ArrayList<>(); for (Subject subject : subjects) { bitmaps.add(subject.getBitmap()); }
परफ़ॉर्मेंस बेहतर करने के लिए सलाह
हर ऐप्लिकेशन सेशन के लिए, मॉडल के शुरू होने की वजह से, पहला अनुमान अक्सर बाद के अनुमानों से ज़्यादा समय लेता है. अगर कम समय में नतीजे पाना ज़रूरी है, तो पहले से ही "डमी" अनुमान को कॉल करने पर विचार करें.
नतीजों की क्वालिटी, इनपुट इमेज की क्वालिटी पर निर्भर करती है:
- ML Kit को सटीक सेगमेंटेशन का नतीजा पाने के लिए, इमेज कम से कम 512x512 पिक्सल की होनी चाहिए.
- इमेज का फ़ोकस खराब होने से भी सटीक नतीजे नहीं मिलते. अगर आपको सही नतीजे नहीं मिलते हैं, तो उपयोगकर्ता से इमेज को दोबारा कैप्चर करने के लिए कहें.