अपने ऐप्लिकेशन में सब्जेक्ट सेगमेंटेशन की सुविधाएं आसानी से जोड़ने के लिए, ML Kit का इस्तेमाल करें.
| सुविधा | जानकारी |
|---|---|
| SDK का नाम | play-services-mlkit-subject-segmentation |
| लागू करना | अनबंडल्ड: इस मॉडल को Google Play services का इस्तेमाल करके डाइनैमिक तरीके से डाउनलोड किया जाता है. |
| ऐप्लिकेशन के साइज़ पर असर | साइज़ में ~200 केबी की बढ़ोतरी. |
| डेटा लेयर में इवेंट बनाने की प्रोसेस में लगने वाला समय | पहली बार इस्तेमाल करने से पहले, उपयोगकर्ताओं को मॉडल डाउनलोड होने का इंतज़ार करना पड़ सकता है. |
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन का इस्तेमाल करें.
शुरू करने से पहले
- अपने प्रोजेक्ट-लेवल की
build.gradleफ़ाइल में, पक्का करें कि आपने Google की Maven रिपॉज़िटरी को अपनेbuildscriptऔरallprojects, दोनों सेक्शन में शामिल किया हो. - अपने मॉड्यूल की ऐप्लिकेशन-लेवल की 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>
इसके अलावा, ModuleInstallClient API की मदद से, Google Play services के ज़रिए मॉडल की उपलब्धता की जांच की जा सकती है. साथ ही, उसे डाउनलोड करने का अनुरोध किया जा सकता है.
अगर आपने इंस्टॉल-टाइम मॉडल डाउनलोड करने की सुविधा चालू नहीं की है या मॉडल को साफ़ तौर पर डाउनलोड करने का अनुरोध नहीं किया है, तो सेगमेंट करने वाले टूल को पहली बार चलाने पर मॉडल डाउनलोड हो जाता है. डाउनलोड पूरा होने से पहले किए गए अनुरोधों से कोई नतीजा नहीं मिलता.
1. इनपुट इमेज तैयार करना
किसी इमेज पर सेगमेंटेशन करने के लिए, डिवाइस पर मौजूद Bitmap, media.Image, ByteBuffer, बाइट ऐरे या किसी फ़ाइल से InputImage ऑब्जेक्ट बनाएं.
अलग-अलग सोर्स से InputImage ऑब्जेक्ट बनाया जा सकता है. इनके बारे में यहां बताया गया है.
media.Image का इस्तेमाल करना
media.Image ऑब्जेक्ट से InputImage ऑब्जेक्ट बनाने के लिए, InputImage.fromMediaImage() को media.Image ऑब्जेक्ट और इमेज का रोटेशन पास करें. ऐसा तब किया जाता है, जब किसी डिवाइस के कैमरे से इमेज कैप्चर की जाती है.
अगर
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();
फ़ोरग्राउंड बिटमैप
इसी तरह, आपको फ़ोरग्राउंड में मौजूद ऑब्जेक्ट का बिटमैप भी मिल सकता है.
विकल्पों में मौजूद Call 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
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()); }
परफ़ॉर्मेंस को बेहतर बनाने के लिए सुझाव
मॉडल के शुरू होने की वजह से, हर ऐप्लिकेशन सेशन के लिए, पहले अनुमान में अक्सर बाद के अनुमानों की तुलना में ज़्यादा समय लगता है. अगर कम लेटेंसी ज़रूरी है, तो समय से पहले "डमी" अनुमान को कॉल करें.
नतीजों की क्वालिटी, इनपुट इमेज की क्वालिटी पर निर्भर करती है:
- सेगमेंटेशन का सटीक नतीजा पाने के लिए, इमेज का साइज़ कम से कम 512x512 पिक्सल होना चाहिए.
- इमेज के फ़ोकस में गड़बड़ी होने पर भी, सटीक नतीजे नहीं मिलते. अगर आपको सही नतीजे नहीं मिलते हैं, तो उपयोगकर्ता से इमेज को फिर से कैप्चर करने के लिए कहें.