आप छवि में पहचाने गए ऑब्जेक्ट को लेबल करने के लिए Firebase ML का उपयोग कर सकते हैं। इस एपीआई की विशेषताओं के बारे में जानकारी के लिए अवलोकन देखें।
शुरू करने से पहले
- यदि आपने पहले से नहीं किया है, तो अपने Android प्रोजेक्ट में Firebase जोड़ें ।
- अपने मॉड्यूल (ऐप-लेवल) ग्रैडल फ़ाइल (आमतौर पर
<project>/<app-module>/build.gradle
) में, Firebase ML Vision Android लाइब्रेरी के लिए निर्भरता जोड़ें। हम लाइब्रेरी वर्ज़निंग को नियंत्रित करने के लिए Firebase Android BoM का उपयोग करने की सलाह देते हैं।dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:32.1.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 लाइब्रेरी संस्करण को उसकी निर्भरता रेखा में निर्दिष्ट करना होगा।
ध्यान दें कि यदि आप अपने ऐप में एकाधिक फायरबेस लाइब्रेरी का उपयोग करते हैं, तो हम लाइब्रेरी संस्करणों को प्रबंधित करने के लिए बीओएम का उपयोग करने की दृढ़ता से अनुशंसा करते हैं, जो सुनिश्चित करता है कि सभी संस्करण संगत हैं।
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' }
यदि आपने अपने प्रोजेक्ट के लिए क्लाउड-आधारित API को पहले से सक्षम नहीं किया है, तो अभी ऐसा करें:
- Firebase कंसोल का Firebase ML APIs पेज खोलें।
यदि आपने अपने प्रोजेक्ट को ब्लेज़ प्राइसिंग प्लान में पहले से अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड पर क्लिक करें। (आपको केवल तभी अपग्रेड करने के लिए कहा जाएगा यदि आपका प्रोजेक्ट ब्लेज़ योजना पर नहीं है।)
केवल ब्लेज़-स्तरीय परियोजनाएँ ही क्लाउड-आधारित API का उपयोग कर सकती हैं।
- यदि क्लाउड-आधारित API पहले से सक्षम नहीं हैं, तो क्लाउड-आधारित API सक्षम करें पर क्लिक करें।
अब आप छवियों को लेबल करने के लिए तैयार हैं।
1. इनपुट इमेज तैयार करें
अपनी इमेज से एकFirebaseVisionImage
ऑब्जेक्ट बनाएं। जब आप Bitmap
उपयोग करते हैं या यदि आप कैमरा 2 एपीआई का उपयोग करते हैं, तो जेपीईजी-प्रारूपित media.Image
, जब संभव हो तो छवि लेबलर सबसे तेज़ चलता है।media.Image
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, जैसे डिवाइस के कैमरे से इमेज कैप्चर करते समय,media.Image
ऑब्जेक्ट पास करें और इमेज का रोटेशनFirebaseVisionImage.fromMediaImage()
पर करें।यदि आप CameraX लाइब्रेरी का उपयोग करते हैं, तो
OnImageCapturedListener
औरImageAnalysis.Analyzer
कक्षाएं आपके लिए रोटेशन मान की गणना करती हैं, इसलिए आपकोFirebaseVisionImage.fromMediaImage()
कॉल करने से पहले रोटेशन को किसी एक Firebase ML केROTATION_
स्थिरांक में बदलने की आवश्यकता है:Kotlin+KTX
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+KTX
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+KTX
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
- फ़ाइल URI से
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, ऐप संदर्भ पास करें और URI कोFirebaseVisionImage.fromFilePath()
पर फ़ाइल करें। यह तब उपयोगी होता है जब आपACTION_GET_CONTENT
इंटेंट का इस्तेमाल करके उपयोगकर्ता को उनकी गैलरी ऐप्लिकेशन से इमेज चुनने के लिए कहते हैं.Kotlin+KTX
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+KTX
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+KTX
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+KTX
val image = FirebaseVisionImage.fromBitmap(bitmap)
Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Bitmap
ऑब्जेक्ट द्वारा दर्शाई गई छवि सीधी होनी चाहिए, बिना किसी अतिरिक्त रोटेशन की आवश्यकता के।
2. छवि लेबलर को कॉन्फ़िगर करें और चलाएं
किसी इमेज में ऑब्जेक्ट को लेबल करने के लिए,FirebaseVisionImage
ऑब्जेक्ट को FirebaseVisionImageLabeler
के processImage
मेथड में पास करें।सबसे पहले,
FirebaseVisionImageLabeler
का उदाहरण प्राप्त करें।Kotlin+KTX
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+KTX
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
ऑब्जेक्ट की एक सूची भेज दी जाएगी। प्रत्येक FirebaseVisionImageLabel
ऑब्जेक्ट छवि में लेबल की गई किसी चीज़ का प्रतिनिधित्व करता है। प्रत्येक लेबल के लिए, आप लेबल का टेक्स्ट विवरण, इसकी नॉलेज ग्राफ़ इकाई आईडी (यदि उपलब्ध हो) और मैच का कॉन्फिडेंस स्कोर प्राप्त कर सकते हैं। उदाहरण के लिए: Kotlin+KTX
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();
}
अगले कदम
- इससे पहले कि आप क्लाउड एपीआई का उपयोग करने वाले ऐप को प्रोडक्शन में तैनात करें, आपको अनधिकृत एपीआई एक्सेस के प्रभाव को रोकने और कम करने के लिए कुछ अतिरिक्त कदम उठाने चाहिए।