ML Kit का इस्तेमाल करके, किसी इमेज में पहचाने गए ऑब्जेक्ट को लेबल किया जा सकता है. इसके लिए, डिवाइस पर मौजूद मॉडल या क्लाउड मॉडल में से किसी एक का इस्तेमाल किया जा सकता है. हर तरीके के फ़ायदों के बारे में जानने के लिए, खास जानकारी देखें.
शुरू करने से पहले
- अगर आपने पहले से ऐसा नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.
- अपने मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल (आम तौर पर
app/build.gradle) में, ML Kit की Android लाइब्रेरी के लिए डिपेंडेंसी जोड़ें:apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-vision:24.0.3' implementation 'com.google.firebase:firebase-ml-vision-image-label-model:20.0.1' }
-
ज़रूरी नहीं, लेकिन सुझाया गया: अगर डिवाइस पर मौजूद एपीआई का इस्तेमाल किया जाता है, तो अपने ऐप्लिकेशन को इस तरह कॉन्फ़िगर करें कि Play Store से ऐप्लिकेशन इंस्टॉल होने के बाद, डिवाइस पर एमएल मॉडल अपने-आप डाउनलोड हो जाए.
इसके लिए, अपने ऐप्लिकेशन की
AndroidManifest.xmlफ़ाइल में यह एलान जोड़ें: अगर आपने इंस्टॉल के समय मॉडल डाउनलोड करने की सुविधा चालू नहीं की है, तो डिवाइस पर डिटेक्टर को पहली बार चलाने पर मॉडल डाउनलोड हो जाएगा. डाउनलोड पूरा होने से पहले किए गए अनुरोधों का कोई नतीजा नहीं मिलेगा.<application ...> ... <meta-data android:name="com.google.firebase.ml.vision.DEPENDENCIES" android:value="label" /> <!-- To use multiple models: android:value="label,model2,model3" --> </application>
-
अगर आपको क्लाउड-आधारित मॉडल का इस्तेमाल करना है और आपने अपने प्रोजेक्ट के लिए, क्लाउड-आधारित एपीआई को पहले से चालू नहीं किया है, तो अब ऐसा करें:
- Firebase कंसोल का ML Kit APIs पेज खोलें.
-
अगर आपने अब तक अपने प्रोजेक्ट को Blaze के प्लान पर अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें पर क्लिक करें. (आपको अपग्रेड करने के लिए तब ही कहा जाएगा, जब आपका प्रोजेक्ट Blaze प्लान पर नहीं होगा.)
सिर्फ़ Blaze-लेवल के प्रोजेक्ट, क्लाउड-आधारित एपीआई का इस्तेमाल कर सकते हैं.
- अगर क्लाउड-आधारित एपीआई पहले से चालू नहीं हैं, तो क्लाउड-आधारित एपीआई चालू करें पर क्लिक करें.
अगर आपको सिर्फ़ डिवाइस पर मौजूद मॉडल का इस्तेमाल करना है, तो इस चरण को छोड़ा जा सकता है.
अब आपके पास डिवाइस पर मौजूद मॉडल या क्लाउड-आधारित मॉडल का इस्तेमाल करके, इमेज को लेबल करने का विकल्प है.
1. इनपुट इमेज तैयार करना
अपनी इमेज सेFirebaseVisionImage ऑब्जेक्ट बनाएं.
इमेज लेबलर सबसे तेज़ी से तब काम करता है, जब Bitmap का इस्तेमाल किया जाता है. अगर camera2 API का इस्तेमाल किया जाता है, तो JPEG फ़ॉर्मैट में media.Image का इस्तेमाल करें. हमारा सुझाव है कि जहां भी हो सके वहां इनका इस्तेमाल करें.
-
media.Imageऑब्जेक्ट सेFirebaseVisionImageऑब्जेक्ट बनाने के लिए,media.Imageऑब्जेक्ट और इमेज के रोटेशन कोFirebaseVisionImage.fromMediaImage()में पास करें. जैसे, डिवाइस के कैमरे से इमेज कैप्चर करते समय.अगर CameraX लाइब्रेरी का इस्तेमाल किया जाता है, तो
OnImageCapturedListenerऔरImageAnalysis.Analyzerक्लास आपके लिए रोटेशन वैल्यू का हिसाब लगाती हैं. इसलिए,FirebaseVisionImage.fromMediaImage()को कॉल करने से पहले, आपको रोटेशन को ML Kit केROTATION_कॉन्स्टेंट में बदलना होगा:Java
private class YourAnalyzer implements ImageAnalysis.Analyzer { private int degreesToFirebaseRotation(int degrees) { switch (degrees) { case 0: return FirebaseVisionImageMetadata.ROTATION_0; case 90: return FirebaseVisionImageMetadata.ROTATION_90; case 180: return FirebaseVisionImageMetadata.ROTATION_180; case 270: return FirebaseVisionImageMetadata.ROTATION_270; default: throw new IllegalArgumentException( "Rotation must be 0, 90, 180, or 270."); } } @Override public void analyze(ImageProxy imageProxy, int degrees) { if (imageProxy == null || imageProxy.getImage() == null) { return; } Image mediaImage = imageProxy.getImage(); int rotation = degreesToFirebaseRotation(degrees); FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation); // Pass image to an ML Kit Vision API // ... } }
Kotlin
private class YourImageAnalyzer : ImageAnalysis.Analyzer { private fun degreesToFirebaseRotation(degrees: Int): Int = when(degrees) { 0 -> FirebaseVisionImageMetadata.ROTATION_0 90 -> FirebaseVisionImageMetadata.ROTATION_90 180 -> FirebaseVisionImageMetadata.ROTATION_180 270 -> FirebaseVisionImageMetadata.ROTATION_270 else -> throw Exception("Rotation must be 0, 90, 180, or 270.") } override fun analyze(imageProxy: ImageProxy?, degrees: Int) { val mediaImage = imageProxy?.image val imageRotation = degreesToFirebaseRotation(degrees) if (mediaImage != null) { val image = FirebaseVisionImage.fromMediaImage(mediaImage, imageRotation) // Pass image to an ML Kit Vision API // ... } } }
अगर आपने ऐसी कैमरा लाइब्रेरी का इस्तेमाल नहीं किया है जो इमेज के रोटेशन की जानकारी देती है, तो डिवाइस के रोटेशन और डिवाइस में मौजूद कैमरा सेंसर के ओरिएंटेशन से इसका हिसाब लगाया जा सकता है:
Java
private static final SparseIntArray ORIENTATIONS = new SparseIntArray(); static { ORIENTATIONS.append(Surface.ROTATION_0, 90); ORIENTATIONS.append(Surface.ROTATION_90, 0); ORIENTATIONS.append(Surface.ROTATION_180, 270); ORIENTATIONS.append(Surface.ROTATION_270, 180); } /** * 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, Context context) 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); // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE); int sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION); rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360; // Return the corresponding FirebaseVisionImageMetadata rotation value. int result; switch (rotationCompensation) { case 0: result = FirebaseVisionImageMetadata.ROTATION_0; break; case 90: result = FirebaseVisionImageMetadata.ROTATION_90; break; case 180: result = FirebaseVisionImageMetadata.ROTATION_180; break; case 270: result = FirebaseVisionImageMetadata.ROTATION_270; break; default: result = FirebaseVisionImageMetadata.ROTATION_0; Log.e(TAG, "Bad rotation value: " + rotationCompensation); } return result; }
Kotlin
private val ORIENTATIONS = SparseIntArray() init { ORIENTATIONS.append(Surface.ROTATION_0, 90) ORIENTATIONS.append(Surface.ROTATION_90, 0) ORIENTATIONS.append(Surface.ROTATION_180, 270) ORIENTATIONS.append(Surface.ROTATION_270, 180) } /** * 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, context: Context): 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) // On most devices, the sensor orientation is 90 degrees, but for some // devices it is 270 degrees. For devices with a sensor orientation of // 270, rotate the image an additional 180 ((270 + 270) % 360) degrees. val cameraManager = context.getSystemService(CAMERA_SERVICE) as CameraManager val sensorOrientation = cameraManager .getCameraCharacteristics(cameraId) .get(CameraCharacteristics.SENSOR_ORIENTATION)!! rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360 // Return the corresponding FirebaseVisionImageMetadata rotation value. val result: Int when (rotationCompensation) { 0 -> result = FirebaseVisionImageMetadata.ROTATION_0 90 -> result = FirebaseVisionImageMetadata.ROTATION_90 180 -> result = FirebaseVisionImageMetadata.ROTATION_180 270 -> result = FirebaseVisionImageMetadata.ROTATION_270 else -> { result = FirebaseVisionImageMetadata.ROTATION_0 Log.e(TAG, "Bad rotation value: $rotationCompensation") } } return result }
इसके बाद,
media.Imageऑब्जेक्ट औरFirebaseVisionImage.fromMediaImage()में रोटेशन की वैल्यू पास करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
Kotlin
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
- फ़ाइल यूआरआई से
FirebaseVisionImageऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन कॉन्टेक्स्ट और फ़ाइल यूआरआई कोFirebaseVisionImage.fromFilePath()में पास करें. यह तब काम आता है, जब उपयोगकर्ता को अपने गैलरी ऐप्लिकेशन से कोई इमेज चुनने के लिए कहने के लिए,ACTION_GET_CONTENTइंटेंट का इस्तेमाल किया जाता है.Java
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Kotlin
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
ByteBufferया बाइट कलेक्शन सेFirebaseVisionImageऑब्जेक्ट बनाने के लिए, सबसे पहलेmedia.Imageइनपुट के लिए ऊपर बताए गए तरीके से इमेज के रोटेशन का हिसाब लगाएं.इसके बाद, एक
FirebaseVisionImageMetadataऑब्जेक्ट बनाएं, जिसमें इमेज की ऊंचाई, चौड़ाई, रंग कोडिंग फ़ॉर्मैट, और घुमाव की जानकारी हो:Java
FirebaseVisionImageMetadata metadata = new FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build();
Kotlin
val metadata = FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build()
FirebaseVisionImageऑब्जेक्ट बनाने के लिए, बफ़र या कलेक्शन और मेटाडेटा ऑब्जेक्ट का इस्तेमाल करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
Kotlin
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
Bitmapऑब्जेक्ट सेFirebaseVisionImageऑब्जेक्ट बनाने के लिए:Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Kotlin
val image = FirebaseVisionImage.fromBitmap(bitmap)
Bitmapऑब्जेक्ट से दिखाई गई इमेज, सीधी होनी चाहिए. इसे किसी और दिशा में घुमाने की ज़रूरत नहीं है.
2. इमेज लेबलर को कॉन्फ़िगर और चलाना
किसी इमेज में ऑब्जेक्ट लेबल करने के लिए,FirebaseVisionImage ऑब्जेक्ट को FirebaseVisionImageLabeler के processImage तरीके में पास करें.
सबसे पहले,
FirebaseVisionImageLabelerका इंस्टेंस पाएं.अगर आपको डिवाइस पर मौजूद इमेज लेबलर का इस्तेमाल करना है, तो:
Java
FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() .getOnDeviceImageLabeler(); // Or, to set the minimum confidence required: // FirebaseVisionOnDeviceImageLabelerOptions options = // new FirebaseVisionOnDeviceImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build(); // FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() // .getOnDeviceImageLabeler(options);Kotlin
val labeler = FirebaseVision.getInstance().getOnDeviceImageLabeler() // Or, to set the minimum confidence required: // val options = FirebaseVisionOnDeviceImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build() // val labeler = FirebaseVision.getInstance().getOnDeviceImageLabeler(options)अगर आपको क्लाउड इमेज लेबलर का इस्तेमाल करना है, तो:
Java
FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() .getCloudImageLabeler(); // Or, to set the minimum confidence required: // FirebaseVisionCloudImageLabelerOptions options = // new FirebaseVisionCloudImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build(); // FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() // .getCloudImageLabeler(options);Kotlin
val labeler = FirebaseVision.getInstance().getCloudImageLabeler() // Or, to set the minimum confidence required: // val options = FirebaseVisionCloudImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build() // val labeler = FirebaseVision.getInstance().getCloudImageLabeler(options)इसके बाद, इमेज को
processImage()तरीके में पास करें:Java
labeler.processImage(image) .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() { @Override public void onSuccess(List<FirebaseVisionImageLabel> labels) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });Kotlin
labeler.processImage(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
3. लेबल किए गए ऑब्जेक्ट के बारे में जानकारी पाना
अगर इमेज लेबल करने की कार्रवाई पूरी हो जाती है, तोFirebaseVisionImageLabel ऑब्जेक्ट की सूची, ऑब्जेक्ट के लेबल होने की सूचना सुनने वाले फ़ंक्शन को भेज दी जाएगी. हर FirebaseVisionImageLabel ऑब्जेक्ट, इमेज में लेबल की गई किसी चीज़ को दिखाता है. हर लेबल के लिए, आपको लेबल का टेक्स्ट ब्यौरा, उसका नॉलेज ग्राफ़ इकाई आईडी (अगर उपलब्ध हो), और मैच के कॉन्फ़िडेंस स्कोर की जानकारी मिल सकती है. उदाहरण के लिए:
Java
for (FirebaseVisionImageLabel label: labels) {
String text = label.getText();
String entityId = label.getEntityId();
float confidence = label.getConfidence();
}
Kotlin
for (label in labels) {
val text = label.text
val entityId = label.entityId
val confidence = label.confidence
}
रीयल-टाइम परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह
अगर आपको रीयल-टाइम ऐप्लिकेशन में इमेज लेबल करनी हैं, तो सबसे अच्छा फ़्रेमरेट पाने के लिए, इन दिशा-निर्देशों का पालन करें:
- इमेज लेबलर को कॉल को कम करें. अगर इमेज लेबलर की सुविधा चालू होने के दौरान, वीडियो का कोई नया फ़्रेम उपलब्ध हो जाता है, तो फ़्रेम को छोड़ दें.
- अगर इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए, इमेज लेबलर के आउटपुट का इस्तेमाल किया जा रहा है, तो पहले ML Kit से नतीजा पाएं. इसके बाद, एक ही चरण में इमेज और ओवरले को रेंडर करें. ऐसा करने पर, हर इनपुट फ़्रेम के लिए डिसप्ले प्लैटफ़ॉर्म पर सिर्फ़ एक बार रेंडर किया जाता है.
-
अगर Camera2 API का इस्तेमाल किया जा रहा है, तो इमेज को
ImageFormat.YUV_420_888फ़ॉर्मैट में कैप्चर करें.अगर पुराने Camera API का इस्तेमाल किया जा रहा है, तो इमेज को
ImageFormat.NV21फ़ॉर्मैट में कैप्चर करें.
अगले चरण
- Cloud API का इस्तेमाल करने वाले ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, आपको कुछ और कदम उठाने चाहिए. इससे बिना अनुमति के एपीआई को ऐक्सेस करने से होने वाले असर को रोका और कम किया जा सकता है.