Android-এ Firebase ML সহ লেবেল চিত্রগুলি

আপনি একটি ছবিতে স্বীকৃত বস্তুকে লেবেল করতে Firebase ML ব্যবহার করতে পারেন। এই API এর বৈশিষ্ট্য সম্পর্কে তথ্যের জন্য ওভারভিউ দেখুন।

তুমি শুরু করার আগে

  1. যদি আপনি ইতিমধ্যেই না করে থাকেন তাহলে আপনার Android প্রকল্পে Firebase যোগ করুন
  2. আপনার মডিউলে (অ্যাপ-লেভেল) গ্রেডল ফাইল (সাধারণত <project>/<app-module>/build.gradle.kts বা <project>/<app-module>/build.gradle ), Firebase ML-এর জন্য নির্ভরতা যোগ করুন অ্যান্ড্রয়েডের জন্য ভিশন লাইব্রেরি। আমরা লাইব্রেরি সংস্করণ নিয়ন্ত্রণ করতে Firebase Android BoM ব্যবহার করার পরামর্শ দিই।
    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.8.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 লাইব্রেরি সংস্করণ তার নির্ভরতা লাইনে উল্লেখ করতে হবে।

    মনে রাখবেন যে আপনি যদি আপনার অ্যাপে একাধিক ফায়ারবেস লাইব্রেরি ব্যবহার করেন, আমরা দৃঢ়ভাবে লাইব্রেরি সংস্করণগুলি পরিচালনা করতে 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'
    }
    
    একটি কোটলিন-নির্দিষ্ট লাইব্রেরি মডিউল খুঁজছেন? অক্টোবর 2023 থেকে শুরু হচ্ছে (Firebase BoM 32.5.0) , Kotlin এবং Java ডেভেলপাররা প্রধান লাইব্রেরি মডিউলের উপর নির্ভর করতে পারে (বিস্তারিত জানতে, এই উদ্যোগ সম্পর্কে প্রায়শই জিজ্ঞাসিত প্রশ্ন দেখুন)।
  3. আপনি যদি ইতিমধ্যে আপনার প্রকল্পের জন্য ক্লাউড-ভিত্তিক API সক্ষম না করে থাকেন তবে এখনই তা করুন:

    1. Firebase কনসোলের Firebase ML APIs পৃষ্ঠা খুলুন।
    2. আপনি যদি ইতিমধ্যেই আপনার প্রোজেক্টকে ব্লেজ প্রাইসিং প্ল্যানে আপগ্রেড না করে থাকেন, তা করতে আপগ্রেড এ ক্লিক করুন। (যদি আপনার প্রকল্পটি ব্লেজ প্ল্যানে না থাকে তবেই আপনাকে আপগ্রেড করার জন্য অনুরোধ করা হবে।)

      শুধুমাত্র ব্লেজ-স্তরের প্রকল্পগুলি ক্লাউড-ভিত্তিক API ব্যবহার করতে পারে।

    3. যদি ক্লাউড-ভিত্তিক APIগুলি ইতিমধ্যে সক্ষম না থাকে, তাহলে ক্লাউড-ভিত্তিক APIগুলি সক্ষম করুন ক্লিক করুন৷

এখন আপনি ইমেজ লেবেল প্রস্তুত.

1. ইনপুট ইমেজ প্রস্তুত করুন

আপনার ছবি থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন। যখন আপনি একটি Bitmap ব্যবহার করেন বা, যদি আপনি ক্যামেরা2 API, একটি JPEG-ফরম্যাটেড media.Image ব্যবহার করেন তখন ইমেজ লেবেলারটি দ্রুত চলে।

  • একটি media.Image থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করতে। ইমেজ অবজেক্ট, যেমন একটি ডিভাইসের ক্যামেরা থেকে একটি ইমেজ ক্যাপচার করার সময়, media.Image অবজেক্ট এবং ছবির রোটেশন FirebaseVisionImage.fromMediaImage() এ।

    আপনি যদি CameraX লাইব্রেরি ব্যবহার করেন, OnImageCapturedListener এবং ImageAnalysis.Analyzer ক্লাসগুলি আপনার জন্য ঘূর্ণন মান গণনা করে, তাই আপনাকে ROTATION_ FirebaseVisionImage.fromMediaImage()

    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 পদ্ধতিতে পাস করুন।

  1. প্রথমে, 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);
    

  2. তারপরে, ছবিটিকে 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();
}

পরবর্তী পদক্ষেপ