Android पर, AutoML से ट्रेनिंग वाले मॉडल की मदद से, इमेज को लेबल करना

ऑटोएमएल विज़न एज का उपयोग करके अपने स्वयं के मॉडल को प्रशिक्षित करने के बाद, आप छवियों को लेबल करने के लिए इसे अपने ऐप में उपयोग कर सकते हैं।

शुरू करने से पहले

  1. यदि आपने पहले से नहीं किया है, तो अपने एंड्रॉइड प्रोजेक्ट में फायरबेस जोड़ें
  2. अपने मॉड्यूल (ऐप-स्तर) ग्रैडल फ़ाइल (आमतौर पर app/build.gradle ) में एमएल किट एंड्रॉइड लाइब्रेरीज़ के लिए निर्भरताएं जोड़ें:
    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-automl:18.0.5'
    }
    

1. मॉडल लोड करें

एमएल किट आपके ऑटोएमएल-जनरेटेड मॉडल को डिवाइस पर चलाता है। हालाँकि, आप अपने मॉडल को फायरबेस से, स्थानीय स्टोरेज से, या दोनों से दूरस्थ रूप से लोड करने के लिए एमएल किट को कॉन्फ़िगर कर सकते हैं।

फायरबेस पर मॉडल को होस्ट करके, आप एक नया ऐप संस्करण जारी किए बिना मॉडल को अपडेट कर सकते हैं, और आप उपयोगकर्ताओं के विभिन्न समूहों को गतिशील रूप से विभिन्न मॉडल पेश करने के लिए रिमोट कॉन्फिग और ए/बी परीक्षण का उपयोग कर सकते हैं।

यदि आप मॉडल को केवल फायरबेस के साथ होस्ट करके प्रदान करना चुनते हैं, और इसे अपने ऐप के साथ बंडल नहीं करते हैं, तो आप अपने ऐप के प्रारंभिक डाउनलोड आकार को कम कर सकते हैं। हालाँकि, ध्यान रखें कि यदि मॉडल आपके ऐप के साथ बंडल नहीं किया गया है, तो मॉडल से संबंधित कोई भी कार्यक्षमता तब तक उपलब्ध नहीं होगी जब तक आपका ऐप पहली बार मॉडल डाउनलोड नहीं करता।

अपने मॉडल को अपने ऐप के साथ बंडल करके, आप यह सुनिश्चित कर सकते हैं कि आपके ऐप की एमएल सुविधाएं तब भी काम करती हैं जब फायरबेस-होस्टेड मॉडल उपलब्ध नहीं है।

फ़ायरबेस-होस्टेड मॉडल स्रोत कॉन्फ़िगर करें

दूरस्थ रूप से होस्ट किए गए मॉडल का उपयोग करने के लिए, एक FirebaseAutoMLRemoteModel ऑब्जेक्ट बनाएं, जिसमें मॉडल को प्रकाशित करते समय आपने जो नाम निर्दिष्ट किया था उसे निर्दिष्ट करें:

Java

// Specify the name you assigned in the Firebase console.
FirebaseAutoMLRemoteModel remoteModel =
    new FirebaseAutoMLRemoteModel.Builder("your_remote_model").build();

Kotlin+KTX

// Specify the name you assigned in the Firebase console.
val remoteModel = FirebaseAutoMLRemoteModel.Builder("your_remote_model").build()

फिर, उन शर्तों को निर्दिष्ट करते हुए मॉडल डाउनलोड कार्य शुरू करें जिनके तहत आप डाउनलोड करने की अनुमति देना चाहते हैं। यदि मॉडल डिवाइस पर नहीं है, या यदि मॉडल का एक नया संस्करण उपलब्ध है, तो कार्य फायरबेस से मॉडल को एसिंक्रोनस रूप से डाउनलोड करेगा:

Java

FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder()
        .requireWifi()
        .build();
FirebaseModelManager.getInstance().download(remoteModel, conditions)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                // Success.
            }
        });

Kotlin+KTX

val conditions = FirebaseModelDownloadConditions.Builder()
    .requireWifi()
    .build()
FirebaseModelManager.getInstance().download(remoteModel, conditions)
    .addOnCompleteListener {
        // Success.
    }

कई ऐप्स अपने इनिशियलाइज़ेशन कोड में डाउनलोड कार्य शुरू करते हैं, लेकिन आप मॉडल का उपयोग करने से पहले किसी भी समय ऐसा कर सकते हैं।

स्थानीय मॉडल स्रोत कॉन्फ़िगर करें

मॉडल को अपने ऐप के साथ बंडल करने के लिए:

  1. फायरबेस कंसोल से डाउनलोड किए गए ज़िप संग्रह से मॉडल और उसके मेटाडेटा को निकालें। हम अनुशंसा करते हैं कि आप फ़ाइलों को वैसे ही उपयोग करें जैसे आपने उन्हें डाउनलोड किया था, बिना किसी संशोधन (फ़ाइल नाम सहित) के।
  2. अपने ऐप पैकेज में अपना मॉडल और उसकी मेटाडेटा फ़ाइलें शामिल करें:

    1. यदि आपके प्रोजेक्ट में एसेट फ़ोल्डर नहीं है, तो app/ फ़ोल्डर पर राइट-क्लिक करके एक बनाएं, फिर न्यू > फ़ोल्डर > एसेट फ़ोल्डर पर क्लिक करें।
    2. मॉडल फ़ाइलें रखने के लिए संपत्ति फ़ोल्डर के अंतर्गत एक उप-फ़ोल्डर बनाएं।
    3. फ़ाइलों model.tflite , dict.txt , और manifest.json को उप-फ़ोल्डर में कॉपी करें (तीनों फ़ाइलें एक ही फ़ोल्डर में होनी चाहिए)।
  3. यह सुनिश्चित करने के लिए कि ऐप बनाते समय ग्रैडल मॉडल फ़ाइल को संपीड़ित नहीं करता है, अपने ऐप की build.gradle फ़ाइल में निम्नलिखित जोड़ें:
    android {
        // ...
        aaptOptions {
            noCompress "tflite"
        }
    }
    
    मॉडल फ़ाइल को ऐप पैकेज में शामिल किया जाएगा और एक कच्ची संपत्ति के रूप में एमएल किट के लिए उपलब्ध होगा।
  4. मॉडल मेनिफेस्ट फ़ाइल का पथ निर्दिष्ट करते हुए एक FirebaseAutoMLLocalModel ऑब्जेक्ट बनाएं:

    Java

    FirebaseAutoMLLocalModel localModel = new FirebaseAutoMLLocalModel.Builder()
            .setAssetFilePath("manifest.json")
            .build();
    

    Kotlin+KTX

    val localModel = FirebaseAutoMLLocalModel.Builder()
            .setAssetFilePath("manifest.json")
            .build()
    

अपने मॉडल से एक छवि लेबलर बनाएं

अपने मॉडल स्रोतों को कॉन्फ़िगर करने के बाद, उनमें से किसी एक से FirebaseVisionImageLabeler ऑब्जेक्ट बनाएं।

यदि आपके पास केवल स्थानीय रूप से बंडल किया गया मॉडल है, तो बस अपने FirebaseAutoMLLocalModel ऑब्जेक्ट से एक लेबलर बनाएं और उस आत्मविश्वास स्कोर सीमा को कॉन्फ़िगर करें जिसकी आप आवश्यकता चाहते हैं ( अपने मॉडल का मूल्यांकन करें देखें):

Java

FirebaseVisionImageLabeler labeler;
try {
    FirebaseVisionOnDeviceAutoMLImageLabelerOptions options =
            new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
                    .setConfidenceThreshold(0.0f)  // Evaluate your model in the Firebase console
                                                   // to determine an appropriate value.
                    .build();
    labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options);
} catch (FirebaseMLException e) {
    // ...
}

Kotlin+KTX

val options = FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
    .setConfidenceThreshold(0)  // Evaluate your model in the Firebase console
                                // to determine an appropriate value.
    .build()
val labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options)

यदि आपके पास दूरस्थ रूप से होस्ट किया गया मॉडल है, तो आपको इसे चलाने से पहले यह जांचना होगा कि इसे डाउनलोड किया गया है। आप मॉडल प्रबंधक की isModelDownloaded() विधि का उपयोग करके मॉडल डाउनलोड कार्य की स्थिति की जांच कर सकते हैं।

हालाँकि आपको केवल लेबलर चलाने से पहले इसकी पुष्टि करनी होगी, यदि आपके पास दूरस्थ रूप से होस्ट किया गया मॉडल और स्थानीय रूप से बंडल किया गया मॉडल दोनों हैं, तो छवि लेबलर को इंस्टेंट करते समय यह जांच करना उचित हो सकता है: रिमोट मॉडल से एक लेबलर बनाएं यदि इसे डाउनलोड कर लिया गया है, अन्यथा स्थानीय मॉडल से।

Java

FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
        .addOnSuccessListener(new OnSuccessListener<Boolean>() {
            @Override
            public void onSuccess(Boolean isDownloaded) {
                FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder optionsBuilder;
                if (isDownloaded) {
                    optionsBuilder = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(remoteModel);
                } else {
                    optionsBuilder = new FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel);
                }
                FirebaseVisionOnDeviceAutoMLImageLabelerOptions options = optionsBuilder
                        .setConfidenceThreshold(0.0f)  // Evaluate your model in the Firebase console
                                                       // to determine an appropriate threshold.
                        .build();

                FirebaseVisionImageLabeler labeler;
                try {
                    labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options);
                } catch (FirebaseMLException e) {
                    // Error.
                }
            }
        });

Kotlin+KTX

FirebaseModelManager.getInstance().isModelDownloaded(remoteModel)
    .addOnSuccessListener { isDownloaded -> 
    val optionsBuilder =
        if (isDownloaded) {
            FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(remoteModel)
        } else {
            FirebaseVisionOnDeviceAutoMLImageLabelerOptions.Builder(localModel)
        }
    // Evaluate your model in the Firebase console to determine an appropriate threshold.
    val options = optionsBuilder.setConfidenceThreshold(0.0f).build()
    val labeler = FirebaseVision.getInstance().getOnDeviceAutoMLImageLabeler(options)
}

यदि आपके पास केवल दूरस्थ रूप से होस्ट किया गया मॉडल है, तो आपको मॉडल-संबंधित कार्यक्षमता को अक्षम करना चाहिए - उदाहरण के लिए, अपने यूआई के हिस्से को ग्रे-आउट या छुपाएं - जब तक कि आप पुष्टि न करें कि मॉडल डाउनलोड हो गया है। आप श्रोता को मॉडल प्रबंधक की download() विधि से जोड़कर ऐसा कर सकते हैं:

Java

FirebaseModelManager.getInstance().download(remoteModel, conditions)
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @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.
            }
        });

Kotlin+KTX

FirebaseModelManager.getInstance().download(remoteModel, conditions)
    .addOnCompleteListener {
        // Download complete. Depending on your app, you could enable the ML
        // feature, or switch from the local model to the remote model, etc.
    }

2. इनपुट छवि तैयार करें

फिर, प्रत्येक छवि के लिए जिसे आप लेबल करना चाहते हैं, इस अनुभाग में वर्णित विकल्पों में से एक का उपयोग करके एक FirebaseVisionImage ऑब्जेक्ट बनाएं और इसे FirebaseVisionImageLabeler (अगले अनुभाग में वर्णित) के एक उदाहरण में पास करें।

आप media.Image ऑब्जेक्ट, डिवाइस पर एक फ़ाइल, एक बाइट सरणी, या एक Bitmap ऑब्जेक्ट से एक FirebaseVisionImage ऑब्जेक्ट बना सकते हैं:

  • media.Image ऑब्जेक्ट से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए, जैसे किसी डिवाइस के कैमरे से छवि कैप्चर करते समय, media.Image ऑब्जेक्ट और छवि के रोटेशन को FirebaseVisionImage.fromMediaImage() पर पास करें।

    यदि आप कैमराएक्स लाइब्रेरी का उपयोग करते हैं, तो OnImageCapturedListener और ImageAnalysis.Analyzer कक्षाएं आपके लिए रोटेशन मान की गणना करती हैं, इसलिए आपको FirebaseVisionImage.fromMediaImage() कॉल करने से पहले रोटेशन को एमएल किट के 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+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 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+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
    }

    फिर, media.Image ऑब्जेक्ट और रोटेशन मान को FirebaseVisionImage.fromMediaImage() पर पास करें:

    Java

    FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);

    Kotlin+KTX

    val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
  • फ़ाइल URI से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए, ऐप संदर्भ पास करें और URI को FirebaseVisionImage.fromFilePath() पर फ़ाइल करें। यह तब उपयोगी होता है जब आप उपयोगकर्ता को उनके गैलरी ऐप से एक छवि चुनने के लिए संकेत देने के लिए ACTION_GET_CONTENT इरादे का उपयोग करते हैं।

    Java

    FirebaseVisionImage image;
    try {
        image = FirebaseVisionImage.fromFilePath(context, uri);
    } catch (IOException e) {
        e.printStackTrace();
    }

    Kotlin+KTX

    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+KTX

    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+KTX

    val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata)
    // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
  • Bitmap ऑब्जेक्ट से FirebaseVisionImage ऑब्जेक्ट बनाने के लिए:

    Java

    FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);

    Kotlin+KTX

    val image = FirebaseVisionImage.fromBitmap(bitmap)
    Bitmap ऑब्जेक्ट द्वारा दर्शाई गई छवि सीधी होनी चाहिए, जिसमें किसी अतिरिक्त घुमाव की आवश्यकता नहीं है।

3. छवि लेबलर चलाएँ

किसी छवि में ऑब्जेक्ट को लेबल करने के लिए, FirebaseVisionImage ऑब्जेक्ट को FirebaseVisionImageLabeler की 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+KTX

labeler.processImage(image)
        .addOnSuccessListener { labels ->
            // Task completed successfully
            // ...
        }
        .addOnFailureListener { e ->
            // Task failed with an exception
            // ...
        }

यदि छवि लेबलिंग सफल होती है, तो FirebaseVisionImageLabel ऑब्जेक्ट की एक सरणी सफल श्रोता को भेज दी जाएगी। प्रत्येक ऑब्जेक्ट से, आप छवि में पहचाने गए फीचर के बारे में जानकारी प्राप्त कर सकते हैं।

उदाहरण के लिए:

Java

for (FirebaseVisionImageLabel label: labels) {
    String text = label.getText();
    float confidence = label.getConfidence();
}

Kotlin+KTX

for (label in labels) {
    val text = label.text
    val confidence = label.confidence
}

वास्तविक समय के प्रदर्शन को बेहतर बनाने के लिए युक्तियाँ

  • थ्रॉटल डिटेक्टर को कॉल करता है। यदि डिटेक्टर के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो फ़्रेम को छोड़ दें।
  • यदि आप इनपुट छवि पर ग्राफिक्स को ओवरले करने के लिए डिटेक्टर के आउटपुट का उपयोग कर रहे हैं, तो पहले एमएल किट से परिणाम प्राप्त करें, फिर छवि को प्रस्तुत करें और एक ही चरण में ओवरले करें। ऐसा करने से, आप प्रत्येक इनपुट फ़्रेम के लिए केवल एक बार डिस्प्ले सतह पर रेंडर करते हैं।
  • यदि आप कैमरा2 एपीआई का उपयोग करते हैं, तो ImageFormat.YUV_420_888 प्रारूप में छवियां कैप्चर करें।

    यदि आप पुराने कैमरा एपीआई का उपयोग करते हैं, तो ImageFormat.NV21 प्रारूप में छवियां कैप्चर करें।