Android पर, AutoML की मदद से ट्रेन किए गए मॉडल का इस्तेमाल करके इमेज को लेबल करना
AutoML Vision Edge का इस्तेमाल करके, अपना मॉडल ट्रेन करने के बाद, इमेज को लेबल करने के लिए, उसे अपने ऐप्लिकेशन में इस्तेमाल किया जा सकता है. AutoML Vision Edge से ट्रेन किए गए मॉडल को इंटिग्रेट करने के दो तरीके हैं: मॉडल को अपने ऐप्लिकेशन के ऐसेट फ़ोल्डर में डालकर बंडल किया जा सकता है या उसे Firebase से डाइनैमिक तौर पर डाउनलोड किया जा सकता है.| मॉडल बंडल करने के विकल्प | |
|---|---|
| आपके ऐप्लिकेशन में बंडल किया गया |
|
| Firebase की मदद से होस्ट किया गया |
|
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल के तौर पर मिले ऐप्लिकेशन को आज़माएं.
शुरू करने से पहले
1. प्रोजेक्ट-लेवल कीbuild.gradle फ़ाइल में, buildscript और allprojects, दोनों सेक्शन में Google की Maven रिपॉज़िटरी को शामिल करना न भूलें.2. अपने मॉड्यूल की ऐप्लिकेशन-लेवल की Gradle फ़ाइल में, ML Kit की Android लाइब्रेरी के लिए डिपेंडेंसी जोड़ें. आम तौर पर, यह फ़ाइल
app/build.gradle होती है:
अपने ऐप्लिकेशन के साथ किसी मॉडल को बंडल करने के लिए:
dependencies {
// ...
// Image labeling feature with bundled automl model
implementation 'com.google.mlkit:image-labeling-automl:16.2.1'
}
linkFirebase
डिपेंडेंसी जोड़ें:
dependencies {
// ...
// Image labeling feature with automl model downloaded
// from firebase
implementation 'com.google.mlkit:image-labeling-automl:16.2.1'
implementation 'com.google.mlkit:linkfirebase:16.0.1'
}
1. मॉडल लोड करें
लोकल मॉडल सोर्स कॉन्फ़िगर करना
मॉडल को अपने ऐप्लिकेशन के साथ बंडल करने के लिए:1. आपने Firebase कंसोल से जो ZIP फ़ॉर्मैट वाला आर्काइव डाउनलोड किया है उससे मॉडल और उसका मेटाडेटा एक्सट्रैक्ट करें. हमारा सुझाव है कि आप फ़ाइलों को डाउनलोड करने के बाद, उनमें कोई बदलाव न करें. फ़ाइलों के नाम भी न बदलें.
2. अपने ऐप्लिकेशन पैकेज में, मॉडल और उसकी मेटाडेटा फ़ाइलें शामिल करें:
a. अगर आपके प्रोजेक्ट में ऐसेट फ़ोल्डर नहीं है, तो
app/ फ़ोल्डर पर राइट क्लिक करके एक फ़ोल्डर बनाएं. इसके बाद, New > Folder > Assets Folder पर क्लिक करें.b. मॉडल फ़ाइलों को सेव करने के लिए, ऐसेट फ़ोल्डर में एक सब-फ़ोल्डर बनाएं.
c.
model.tflite, dict.txt, और manifest.json फ़ाइलों को सब-फ़ोल्डर में कॉपी करें. तीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए.3. अपने ऐप्लिकेशन की
build.gradle फ़ाइल में यह जानकारी जोड़ें, ताकि यह पक्का किया जा सके कि ऐप्लिकेशन बनाते समय, Gradle मॉडल फ़ाइल को कंप्रेस न करे:
android {
// ...
aaptOptions {
noCompress "tflite"
}
}
ध्यान दें: Android Gradle प्लगिन के वर्शन 4.1 से, .tflite को डिफ़ॉल्ट रूप से noCompress सूची में जोड़ दिया जाएगा. इसके बाद, ऊपर बताए गए तरीके की ज़रूरत नहीं होगी.
4.
LocalModel ऑब्जेक्ट बनाएं और मॉडल मेनिफ़ेस्ट फ़ाइल का पाथ बताएं:
Kotlin
val localModel = AutoMLImageLabelerLocalModel.Builder() .setAssetFilePath("manifest.json") // or .setAbsoluteFilePath(absolute file path to manifest file) .build()
Java
AutoMLImageLabelerLocalModel localModel = new AutoMLImageLabelerLocalModel.Builder() .setAssetFilePath("manifest.json") // or .setAbsoluteFilePath(absolute file path to manifest file) .build();
Firebase पर होस्ट किए गए मॉडल सोर्स को कॉन्फ़िगर करना
रिमोटली होस्ट किए गए मॉडल का इस्तेमाल करने के लिए, RemoteModel ऑब्जेक्ट बनाएं. इसमें वह नाम डालें जो आपने मॉडल को पब्लिश करते समय असाइन किया था:
Kotlin
// Specify the name you assigned in the Firebase console. val remoteModel = AutoMLImageLabelerRemoteModel.Builder("your_model_name").build()
Java
// Specify the name you assigned in the Firebase console. AutoMLImageLabelerRemoteModel remoteModel = new AutoMLImageLabelerRemoteModel.Builder("your_model_name").build();
इसके बाद, मॉडल डाउनलोड करने का टास्क शुरू करें. साथ ही, वे शर्तें बताएं जिनके तहत आपको डाउनलोड करने की अनुमति देनी है. अगर मॉडल डिवाइस पर मौजूद नहीं है या मॉडल का नया वर्शन उपलब्ध है, तो टास्क, Firebase से मॉडल को एसिंक्रोनस तरीके से डाउनलोड करेगा:
Kotlin
val downloadConditions = DownloadConditions.Builder() .requireWifi() .build() RemoteModelManager.getInstance().download(remoteModel, downloadConditions) .addOnSuccessListener { // Success. }
Java
DownloadConditions downloadConditions = new DownloadConditions.Builder() .requireWifi() .build(); RemoteModelManager.getInstance().download(remoteModel, downloadConditions) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(@NonNull Task task) { // Success. } });
कई ऐप्लिकेशन, अपने इनिशलाइज़ेशन कोड में डाउनलोड टास्क शुरू करते हैं. हालांकि, मॉडल का इस्तेमाल करने से पहले, किसी भी समय ऐसा किया जा सकता है.
अपने मॉडल से इमेज लेबलर बनाना
मॉडल सोर्स कॉन्फ़िगर करने के बाद, उनमें से किसी एक से ImageLabeler ऑब्जेक्ट बनाएं.
अगर आपके पास सिर्फ़ स्थानीय तौर पर बंडल किया गया मॉडल है, तो अपने AutoMLImageLabelerLocalModel ऑब्जेक्ट से लेबलर बनाएं. इसके बाद, कॉन्फ़िडेंस स्कोर की थ्रेशोल्ड को कॉन्फ़िगर करें (अपने मॉडल का आकलन करें देखें):
Kotlin
val autoMLImageLabelerOptions = AutoMLImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0) // Evaluate your model in the Firebase console // to determine an appropriate value. .build() val labeler = ImageLabeling.getClient(autoMLImageLabelerOptions)
Java
AutoMLImageLabelerOptions autoMLImageLabelerOptions = new AutoMLImageLabelerOptions.Builder(localModel) .setConfidenceThreshold(0.0f) // Evaluate your model in the Firebase console // to determine an appropriate value. .build(); ImageLabeler labeler = ImageLabeling.getClient(autoMLImageLabelerOptions)
अगर आपके पास रिमोटली-होस्ट किया गया मॉडल है, तो आपको यह देखना होगा कि उसे चलाने से पहले डाउनलोड किया गया हो. मॉडल मैनेजर के isModelDownloaded() तरीके का इस्तेमाल करके, मॉडल डाउनलोड करने के टास्क की स्थिति देखी जा सकती है.
हालांकि, आपको लेबलर को चलाने से पहले ही इसकी पुष्टि करनी होगी. अगर आपके पास रिमोट तौर पर होस्ट किया गया मॉडल और स्थानीय तौर पर बंडल किया गया मॉडल, दोनों हैं, तो इमेज लेबलर को इंस्टैंशिएट करते समय यह जांच करना सही रहेगा: अगर रिमोट मॉडल डाउनलोड किया गया है, तो उससे लेबलर बनाएं. अगर ऐसा नहीं है, तो स्थानीय मॉडल से लेबलर बनाएं.
Kotlin
RemoteModelManager.getInstance().isModelDownloaded(remoteModel) .addOnSuccessListener { isDownloaded -> val optionsBuilder = if (isDownloaded) { AutoMLImageLabelerOptions.Builder(remoteModel) } else { AutoMLImageLabelerOptions.Builder(localModel) } // Evaluate your model in the Firebase console to determine an appropriate threshold. val options = optionsBuilder.setConfidenceThreshold(0.0f).build() val labeler = ImageLabeling.getClient(options) }
Java
RemoteModelManager.getInstance().isModelDownloaded(remoteModel) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Boolean isDownloaded) { AutoMLImageLabelerOptions.Builder optionsBuilder; if (isDownloaded) { optionsBuilder = new AutoMLImageLabelerOptions.Builder(remoteModel); } else { optionsBuilder = new AutoMLImageLabelerOptions.Builder(localModel); } AutoMLImageLabelerOptions options = optionsBuilder .setConfidenceThreshold(0.0f) // Evaluate your model in the Firebase console // to determine an appropriate threshold. .build(); ImageLabeler labeler = ImageLabeling.getClient(options); } });
अगर आपके पास सिर्फ़ रिमोटली-होस्ट किया गया मॉडल है, तो आपको मॉडल से जुड़ी सुविधाओं को बंद कर देना चाहिए. उदाहरण के लिए, अपने यूज़र इंटरफ़ेस (यूआई) के कुछ हिस्से को धूसर करना या छिपाना. ऐसा तब तक करें, जब तक आपको यह पुष्टि न हो जाए कि मॉडल डाउनलोड हो गया है. इसके लिए, मॉडल मैनेजर के download() तरीके में लिसनर अटैच करें:
Kotlin
RemoteModelManager.getInstance().download(remoteModel, conditions) .addOnSuccessListener { // Download complete. Depending on your app, you could enable the ML // feature, or switch from the local model to the remote model, etc. }
Java
RemoteModelManager.getInstance().download(remoteModel, conditions) .addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(Void v) { // Download complete. Depending on your app, you could enable // the ML feature, or switch from the local model to the remote // model, etc. } });
2. इनपुट इमेज तैयार करना
इसके बाद, आपको जिस इमेज को लेबल करना है उसके लिए, अपनी इमेज से एक InputImage ऑब्जेक्ट बनाएं. इमेज लेबलर सबसे तेज़ी से तब काम करता है, जब Bitmap का इस्तेमाल किया जाता है. इसके अलावा, अगर camera2 API का इस्तेमाल किया जाता है, तो YUV_420_888 media.Image का इस्तेमाल किया जाता है. हमारा सुझाव है कि जब भी संभव हो, इनका इस्तेमाल करें.
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 ऑब्जेक्ट और रोटेशन डिग्री के साथ दिखाया जाता है.
3. इमेज लेबलर को चलाना
किसी इमेज में मौजूद ऑब्जेक्ट को लेबल करने के लिए,image ऑब्जेक्ट को ImageLabeler के process() तरीके में पास करें.
Kotlin
labeler.process(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
Java
labeler.process(image) .addOnSuccessListener(new OnSuccessListener<List<ImageLabel>>() { @Override public void onSuccess(List<ImageLabel> labels) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
4. लेबल किए गए ऑब्जेक्ट के बारे में जानकारी पाना
अगर इमेज लेबल करने की प्रोसेस पूरी हो जाती है, तो ImageLabel ऑब्जेक्ट की सूची, सफलता के बारे में बताने वाले लिसनर को पास की जाती है. हर ImageLabel ऑब्जेक्ट, इमेज में लेबल की गई किसी चीज़ को दिखाता है. आपको हर लेबल के टेक्स्ट का ब्यौरा, मैच का कॉन्फ़िडेंस स्कोर, और मैच का इंडेक्स मिल सकता है.
उदाहरण के लिए:
Kotlin
for (label in labels) { val text = label.text val confidence = label.confidence val index = label.index }
Java
for (ImageLabel label : labels) { String text = label.getText(); float confidence = label.getConfidence(); int index = label.getIndex(); }
रीयल-टाइम परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह
अगर आपको रीयल-टाइम ऐप्लिकेशन में इमेज को लेबल करना है, तो सबसे अच्छे फ़्रेमरेट पाने के लिए इन दिशा-निर्देशों का पालन करें:
- अगर
Cameraयाcamera2एपीआई का इस्तेमाल किया जाता है, तो इमेज लेबलर को किए जाने वाले कॉल को थ्रॉटल करें. अगर इमेज लेबलर के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध होता है, तो फ़्रेम को छोड़ दें. उदाहरण के लिए, क्विकस्टार्ट सैंपल के तौर पर मिले ऐप्लिकेशन मेंVisionProcessorBaseक्लास देखें. - अगर
CameraXएपीआई का इस्तेमाल किया जाता है, तो पक्का करें कि बैकप्रेशर रणनीति को इसकी डिफ़ॉल्ट वैल्यूImageAnalysis.STRATEGY_KEEP_ONLY_LATESTपर सेट किया गया हो. इससे यह पक्का होता है कि विश्लेषण के लिए, एक बार में सिर्फ़ एक इमेज भेजी जाएगी. अगर ऐनलाइज़र के व्यस्त होने पर ज़्यादा इमेज जनरेट होती हैं, तो उन्हें अपने-आप हटा दिया जाएगा. साथ ही, उन्हें डिलीवरी के लिए सूची में नहीं रखा जाएगा. ImageProxy.close() को कॉल करके, विश्लेषण की जा रही इमेज को बंद करने के बाद, अगली नई इमेज डिलीवर की जाएगी. - अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, इमेज लेबलर के आउटपुट का इस्तेमाल किया जाता है, तो पहले ML Kit से नतीजा पाएं. इसके बाद, इमेज को रेंडर करें और एक ही चरण में ओवरले करें. यह हर इनपुट फ़्रेम के लिए, डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर होता है. उदाहरण के लिए, क्विकस्टार्ट सैंपल ऐप्लिकेशन में
CameraSourcePreviewऔरGraphicOverlayक्लास देखें. - अगर Camera2 API का इस्तेमाल किया जा रहा है, तो इमेज को
ImageFormat.YUV_420_888फ़ॉर्मैट में कैप्चर करें. अगर पुराने Camera API का इस्तेमाल किया जा रहा है, तो इमेज कोImageFormat.NV21फ़ॉर्मैट में कैप्चर करें.