Android पर, AutoML से ट्रेन किए गए मॉडल की मदद से इमेज लेबल करना
AutoML Vision Edge का इस्तेमाल करके अपने मॉडल को ट्रेन करने के बाद, इमेज को लेबल करने के लिए, अपने ऐप्लिकेशन में इसका इस्तेमाल किया जा सकता है. AutoML Vision Edge से ट्रेन किए गए मॉडल को इंटिग्रेट करने के दो तरीके हैं: मॉडल को अपने ऐप्लिकेशन के ऐसेट फ़ोल्डर में डालकर बंडल किया जा सकता है या Firebase से डाइनैमिक तौर पर डाउनलोड किया जा सकता है.मॉडल को बंडल करने के विकल्प | |
---|---|
आपके ऐप्लिकेशन में बंडल किया गया |
|
Firebase की मदद से होस्ट किया गया |
|
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन आज़माएं.
शुरू करने से पहले
1. प्रोजेक्ट-लेवल कीbuild.gradle
फ़ाइल में, buildscript
और allprojects
, दोनों सेक्शन में Google की मेवन रिपॉज़िटरी को शामिल करना न भूलें.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/
फ़ोल्डर पर राइट क्लिक करें. इसके बाद, नया > फ़ोल्डर > एसेट फ़ोल्डर पर क्लिक करें.b. मॉडल फ़ाइलों को शामिल करने के लिए, एसेट फ़ोल्डर में एक सब-फ़ोल्डर बनाएं.
c. फ़ाइलों
model.tflite
, dict.txt
, और
manifest.json
को सब-फ़ोल्डर में कॉपी करें. यह ज़रूरी है कि तीनों फ़ाइलें एक ही फ़ोल्डर में हों.3. अपने ऐप्लिकेशन की
build.gradle
फ़ाइल में यह जानकारी जोड़ें, ताकि यह पक्का किया जा सके कि ऐप्लिकेशन बनाते समय, Gradle मॉडल फ़ाइल को कंप्रेस न करे:
android { // ... aaptOptions { noCompress "tflite" } }
ध्यान दें: Android Gradle प्लग इन के 4.1 वर्शन से, .tflite को डिफ़ॉल्ट रूप से noCompress सूची में जोड़ दिया जाएगा. साथ ही, ऊपर बताए गए तरीके का इस्तेमाल करने की ज़रूरत नहीं होगी.
4. मॉडल मेनिफ़ेस्ट फ़ाइल के पाथ की जानकारी देकर,
LocalModel
ऑब्जेक्ट बनाएं:
val localModel = AutoMLImageLabelerLocalModel.Builder() .setAssetFilePath("manifest.json") // or .setAbsoluteFilePath(absolute file path to manifest file) .build()
AutoMLImageLabelerLocalModel localModel = new AutoMLImageLabelerLocalModel.Builder() .setAssetFilePath("manifest.json") // or .setAbsoluteFilePath(absolute file path to manifest file) .build();
Firebase पर होस्ट किए गए मॉडल सोर्स को कॉन्फ़िगर करना
रिमोट तौर पर होस्ट किए गए मॉडल का इस्तेमाल करने के लिए, RemoteModel
ऑब्जेक्ट बनाएं. साथ ही, उस नाम की जानकारी दें जो आपने मॉडल को पब्लिश करते समय असाइन किया था:
// Specify the name you assigned in the Firebase console. val remoteModel = AutoMLImageLabelerRemoteModel.Builder("your_model_name").build()
// Specify the name you assigned in the Firebase console. AutoMLImageLabelerRemoteModel remoteModel = new AutoMLImageLabelerRemoteModel.Builder("your_model_name").build();
इसके बाद, मॉडल डाउनलोड करने का टास्क शुरू करें. साथ ही, उन शर्तों के बारे में बताएं जिनके तहत आपको डाउनलोड करने की अनुमति देनी है. अगर मॉडल डिवाइस पर मौजूद नहीं है या मॉडल का नया वर्शन उपलब्ध है, तो टास्क, Firebase से मॉडल को असिंक्रोनस तरीके से डाउनलोड करेगा:
val downloadConditions = DownloadConditions.Builder() .requireWifi() .build() RemoteModelManager.getInstance().download(remoteModel, downloadConditions) .addOnSuccessListener { // Success. }
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
ऑब्जेक्ट से लेबलर बनाएं और भरोसे के उस स्कोर के थ्रेशोल्ड को कॉन्फ़िगर करें जिसकी आपको ज़रूरत है (अपने मॉडल का आकलन करें देखें):
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)
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()
तरीके का इस्तेमाल करके, मॉडल डाउनलोड करने के टास्क की स्थिति देखी जा सकती है.
लेबलर को चलाने से पहले ही इसकी पुष्टि कर ली जा सकती है. हालांकि, अगर आपके पास रिमोट से होस्ट किया गया मॉडल और स्थानीय तौर पर बंडल किया गया मॉडल, दोनों मौजूद हैं, तो इमेज लेबलर को इंस्टैंशिएट करते समय यह जांच करना सही रहेगा: अगर रिमोट मॉडल डाउनलोड किया गया है, तो उससे लेबलर बनाएं. अगर नहीं, तो स्थानीय मॉडल से बनाएं.
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) }
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()
तरीके में एक लिसनर अटैच करें:
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. }
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
क्लास आपके लिए रोटेशन वैल्यू का हिसाब लगाती हैं.
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 // ... } } }
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 // ... } } }
अगर आपने ऐसी कैमरा लाइब्रेरी का इस्तेमाल नहीं किया है जो इमेज के घूमने की डिग्री बताती है, तो डिवाइस के घूमने की डिग्री और डिवाइस में कैमरे के सेंसर के ओरिएंटेशन से इसका हिसाब लगाया जा सकता है:
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 }
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()
में घुमाव की डिग्री की वैल्यू पास करें:
val image = InputImage.fromMediaImage(mediaImage, rotation)
InputImage image = InputImage.fromMediaImage(mediaImage, rotation);
फ़ाइल के यूआरआई का इस्तेमाल करना
फ़ाइल यूआरआई से InputImage
ऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन कॉन्टेक्स्ट और फ़ाइल यूआरआई को InputImage.fromFilePath()
में पास करें. यह तब काम आता है, जब उपयोगकर्ता को अपने गैलरी ऐप्लिकेशन से कोई इमेज चुनने के लिए कहने के लिए, ACTION_GET_CONTENT
इंटेंट का इस्तेमाल किया जाता है.
val image: InputImage try { image = InputImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
InputImage image;
try {
image = InputImage.fromFilePath(context, uri);
} catch (IOException e) {
e.printStackTrace();
}
ByteBuffer
या ByteArray
का इस्तेमाल करना
ByteBuffer
या ByteArray
से InputImage
आइटम बनाने के लिए, सबसे पहले इमेज के घूमने की डिग्री का हिसाब लगाएं. यह हिसाब लगाने का तरीका, media.Image
इनपुट के लिए पहले बताया गया है.
इसके बाद, बफ़र या ऐरे के साथ InputImage
ऑब्जेक्ट बनाएं. साथ ही, इमेज की
ऊंचाई, चौड़ाई, कलर कोडिंग फ़ॉर्मैट, और घुमाव की डिग्री भी डालें:
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 )
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
ऑब्जेक्ट बनाने के लिए, यह एलान करें:
val image = InputImage.fromBitmap(bitmap, 0)
InputImage image = InputImage.fromBitmap(bitmap, rotationDegree);
इमेज को घुमाने के डिग्री के साथ Bitmap
ऑब्जेक्ट से दिखाया जाता है.
3. इमेज लेबलर को चलाना
किसी इमेज में ऑब्जेक्ट लेबल करने के लिए,image
ऑब्जेक्ट को ImageLabeler
के process()
तरीके में पास करें.
labeler.process(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
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
ऑब्जेक्ट, इमेज में लेबल किए गए किसी ऑब्जेक्ट को दिखाता है. आपको हर लेबल का टेक्स्ट ब्यौरा, मैच के कॉन्फ़िडेंस स्कोर, और मैच का इंडेक्स दिख सकता है.
उदाहरण के लिए:
for (label in labels) { val text = label.text val confidence = label.confidence val index = label.index }
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
फ़ॉर्मैट में कैप्चर करें.