अपने ऐप्लिकेशन में विषय के हिसाब से सेगमेंट बनाने की सुविधाओं को आसानी से जोड़ने के लिए, ML Kit का इस्तेमाल करें.
सुविधा | जानकारी |
---|---|
SDK टूल का नाम | play-services-mlkit-subject-segmentation |
लागू करना | अनबंडल किया गया: मॉडल को Google Play services का इस्तेमाल करके डाइनैमिक तौर पर डाउनलोड किया जाता है. |
ऐप्लिकेशन के साइज़ का असर | साइज़ में ~200 केबी की बढ़ोतरी. |
डेटा लेयर में इवेंट बनाने की प्रोसेस में लगने वाला समय | पहली बार इस्तेमाल करने से पहले, उपयोगकर्ताओं को मॉडल डाउनलोड होने का इंतज़ार करना पड़ सकता है. |
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन आज़माएं.
शुरू करने से पहले
- प्रोजेक्ट-लेवल की
build.gradle
फ़ाइल में,buildscript
औरallprojects
, दोनों सेक्शन में Google की मेवन रिपॉज़िटरी शामिल करना न भूलें. - अपने मॉड्यूल की ऐप्लिकेशन-लेवल की 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
ऑब्जेक्ट बनाने के लिए, 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
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 पिक्सल होना चाहिए.
- इमेज का फ़ोकस खराब होने पर भी नतीजों की सटीकता पर असर पड़ सकता है. अगर आपको सही नतीजे नहीं मिलते हैं, तो उपयोगकर्ता से इमेज फिर से लेने के लिए कहें.