इमेज में पहचाने गए ऑब्जेक्ट को लेबल करने के लिए, Firebase ML का इस्तेमाल किया जा सकता है. इस एपीआई की सुविधाओं के बारे में जानने के लिए, खास जानकारी देखें.
शुरू करने से पहले
- अगर आपने पहले से ऐसा नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें.
-
अपनी मॉड्यूल (ऐप्लिकेशन-लेवल) की Gradle फ़ाइल में, Firebase ML Android के लिए Vision लाइब्रेरी की डिपेंडेंसी जोड़ें. यह फ़ाइल आम तौर पर
<project>/<app-module>/build.gradle.kts
या<project>/<app-module>/build.gradle
होती है. हमारा सुझाव है कि लाइब्रेरी के वर्शन को कंट्रोल करने के लिए, Firebase Android BoM का इस्तेमाल करें.dependencies { // Import the BoM for the Firebase platform implementation(platform("com.google.firebase:firebase-bom:34.0.0")) // Add the dependency for the Firebase ML Vision library // When using the BoM, you don't specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-ml-vision' }
Firebase Android BoM का इस्तेमाल करने पर, आपका ऐप्लिकेशन हमेशा Firebase Android लाइब्रेरी के साथ काम करने वाले वर्शन का इस्तेमाल करेगा.
(वैकल्पिक) BoM का इस्तेमाल किए बिना, Firebase लाइब्रेरी की डिपेंडेंसी जोड़ें
अगर आपको Firebase BoM का इस्तेमाल नहीं करना है, तो आपको डिपेंडेंसी लाइन में Firebase की हर लाइब्रेरी के वर्शन की जानकारी देनी होगी.
ध्यान दें कि अगर आपके ऐप्लिकेशन में Firebase की एक से ज़्यादा लाइब्रेरी का इस्तेमाल किया जाता है, तो हमारा सुझाव है कि लाइब्रेरी के वर्शन मैनेज करने के लिए BoM का इस्तेमाल करें. इससे यह पक्का किया जा सकता है कि सभी वर्शन एक-दूसरे के साथ काम करते हों.
dependencies { // Add the dependency for the Firebase ML Vision library // When NOT using the BoM, you must specify versions in Firebase library dependencies implementation 'com.google.firebase:firebase-ml-vision:24.1.0' }
-
अगर आपने अब तक अपने प्रोजेक्ट के लिए, क्लाउड-आधारित एपीआई चालू नहीं किए हैं, तो अभी ऐसा करें:
- Firebase कंसोल में, Firebase ML एपीआई पेज खोलें.
-
अगर आपने अपने प्रोजेक्ट को इस्तेमाल के हिसाब से शुल्क चुकाने वाले Blaze प्लान पर अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड करें पर क्लिक करें. (आपको अपग्रेड करने के लिए सिर्फ़ तब कहा जाएगा, जब आपका प्रोजेक्ट Blaze प्लान पर नहीं होगा.)
सिर्फ़ ब्लेज़ प्लान वाले प्रोजेक्ट, क्लाउड पर आधारित एपीआई का इस्तेमाल कर सकते हैं.
- अगर क्लाउड पर आधारित एपीआई पहले से चालू नहीं हैं, तो क्लाउड पर आधारित एपीआई चालू करें पर क्लिक करें.
अब इमेज को लेबल किया जा सकता है.
1. इनपुट इमेज तैयार करना
अपनी इमेज सेFirebaseVisionImage
ऑब्जेक्ट बनाएं.
इमेज लेबलर सबसे तेज़ी से तब काम करता है, जब Bitmap
का इस्तेमाल किया जाता है. अगर camera2 API का इस्तेमाल किया जाता है, तो JPEG फ़ॉर्मैट वाले media.Image
का इस्तेमाल करें. हमारा सुझाव है कि जब भी हो सके, इनका इस्तेमाल करें.
-
किसी
media.Image
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए,FirebaseVisionImage.fromMediaImage()
कोmedia.Image
ऑब्जेक्ट और इमेज का रोटेशन पास करें. ऐसा तब किया जाता है, जब किसी डिवाइस के कैमरे से इमेज कैप्चर की जाती है.अगर CameraX लाइब्रेरी का इस्तेमाल किया जाता है, तो
OnImageCapturedListener
औरImageAnalysis.Analyzer
क्लास, रोटेशन वैल्यू का हिसाब लगाती हैं. इसलिए, आपकोFirebaseVisionImage.fromMediaImage()
को कॉल करने से पहले, रोटेशन को Firebase ML केROTATION_
कॉन्स्टेंट में से किसी एक में बदलना होगा: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 Vision API // ... } } }
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 Vision API // ... } }
अगर ऐसी कैमरा लाइब्रेरी का इस्तेमाल नहीं किया जा रहा है जो आपको इमेज के रोटेशन की जानकारी देती है, तो डिवाइस के रोटेशन और डिवाइस में मौजूद कैमरा सेंसर के ओरिएंटेशन से, इमेज के रोटेशन का हिसाब लगाया जा सकता है:
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 }
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; }
इसके बाद,
media.Image
ऑब्जेक्ट और रोटेशन वैल्यू कोFirebaseVisionImage.fromMediaImage()
में पास करें:Kotlin
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
- फ़ाइल यूआरआई से
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, ऐप्लिकेशन के कॉन्टेक्स्ट और फ़ाइल यूआरआई कोFirebaseVisionImage.fromFilePath()
में पास करें. यह तब काम आता है, जब आपको उपयोगकर्ता को उसकी गैलरी ऐप्लिकेशन से कोई इमेज चुनने के लिए प्रॉम्प्ट करना हो. इसके लिए,ACTION_GET_CONTENT
इंटेंट का इस्तेमाल करें.Kotlin
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
Java
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
ByteBuffer
या बाइट ऐरे सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, सबसे पहले इमेज के रोटेशन का हिसाब लगाएं. इसके लिए,media.Image
इनपुट के लिए ऊपर बताया गया तरीका अपनाएं.इसके बाद, एक
FirebaseVisionImageMetadata
ऑब्जेक्ट बनाएं. इसमें इमेज की ऊंचाई, चौड़ाई, कलर एन्कोडिंग फ़ॉर्मैट, और रोटेशन की जानकारी शामिल करें:Kotlin
val metadata = FirebaseVisionImageMetadata.Builder() .setWidth(480) // 480x360 is typically sufficient for .setHeight(360) // image recognition .setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21) .setRotation(rotation) .build()
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();
बफ़र या ऐरे और मेटाडेटा ऑब्जेक्ट का इस्तेमाल करके,
FirebaseVisionImage
ऑब्जेक्ट बनाएं:Kotlin
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
- किसी
Bitmap
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए:Kotlin
val image = FirebaseVisionImage.fromBitmap(bitmap)
Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Bitmap
ऑब्जेक्ट से दिखाई गई इमेज सीधी होनी चाहिए. इसे घुमाने की ज़रूरत नहीं होनी चाहिए.
2. इमेज लेबलर को कॉन्फ़िगर करना और चलाना
किसी इमेज में मौजूद ऑब्जेक्ट को लेबल करने के लिए,FirebaseVisionImage
ऑब्जेक्ट को FirebaseVisionImageLabeler
के processImage
तरीके से पास करें.
सबसे पहले,
FirebaseVisionImageLabeler
का कोई इंस्टेंस पाएं.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)
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);
इसके बाद, इमेज को
processImage()
तरीके से पास करें:Kotlin
labeler.processImage(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
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 // ... } });
3. लेबल किए गए ऑब्जेक्ट के बारे में जानकारी पाना
अगर इमेज लेबल करने की प्रोसेस पूरी हो जाती है, तोFirebaseVisionImageLabel
ऑब्जेक्ट की सूची को success listener को पास किया जाएगा. हर FirebaseVisionImageLabel
ऑब्जेक्ट, इमेज में लेबल की गई किसी चीज़ को दिखाता है. हर लेबल के लिए, आपको लेबल के टेक्स्ट का ब्यौरा, उसका नॉलेज ग्राफ़ एंटिटी आईडी (अगर उपलब्ध हो), और मैच होने का कॉन्फ़िडेंस स्कोर मिल सकता है. उदाहरण के लिए:
Kotlin
for (label in labels) {
val text = label.text
val entityId = label.entityId
val confidence = label.confidence
}
Java
for (FirebaseVisionImageLabel label: labels) {
String text = label.getText();
String entityId = label.getEntityId();
float confidence = label.getConfidence();
}
अगले चरण
- Cloud API का इस्तेमाल करने वाले किसी ऐप्लिकेशन को प्रोडक्शन में डिप्लॉय करने से पहले, आपको कुछ और कदम उठाने चाहिए. इससे एपीआई को बिना अनुमति के ऐक्सेस करने से रोका जा सकेगा और इसके असर को कम किया जा सकेगा.