Android-এ Firebase ML-এর সাহায্যে ইমেজে টেক্সট চিনুন

ছবিতে লেখা শনাক্ত করার জন্য আপনি Firebase ML ব্যবহার করতে পারেন। Firebase ML একটি সাধারণ-উদ্দেশ্য API রয়েছে যা ছবিতে লেখা শনাক্ত করার জন্য উপযুক্ত, যেমন রাস্তার চিহ্নের লেখা, এবং নথির লেখা শনাক্ত করার জন্য অপ্টিমাইজ করা একটি API।

শুরু করার আগে

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

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

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

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

এখন আপনি ছবিতে লেখা চিনতে শুরু করতে প্রস্তুত।

ছবির নির্দেশিকা ইনপুট করুন

  • Firebase ML সঠিকভাবে টেক্সট শনাক্ত করতে, ইনপুট ছবিতে এমন টেক্সট থাকতে হবে যা পর্যাপ্ত পিক্সেল ডেটা দ্বারা প্রতিনিধিত্ব করে। আদর্শভাবে, ল্যাটিন টেক্সটের জন্য, প্রতিটি অক্ষর কমপক্ষে 16x16 পিক্সেল হওয়া উচিত। চীনা, জাপানি এবং কোরিয়ান টেক্সটের জন্য, প্রতিটি অক্ষর 24x24 পিক্সেল হওয়া উচিত। সমস্ত ভাষার জন্য, 24x24 পিক্সেলের চেয়ে বড় অক্ষরের জন্য সাধারণত কোনও নির্ভুলতার সুবিধা নেই।

    উদাহরণস্বরূপ, একটি 640x480 চিত্র একটি ব্যবসায়িক কার্ড স্ক্যান করার জন্য ভাল কাজ করতে পারে যা ছবির সম্পূর্ণ প্রস্থ দখল করে। অক্ষর আকারের কাগজে মুদ্রিত একটি নথি স্ক্যান করার জন্য, একটি 720x1280 পিক্সেল চিত্রের প্রয়োজন হতে পারে।

  • ছবির দুর্বল ফোকাস টেক্সট শনাক্তকরণের নির্ভুলতার ক্ষতি করতে পারে। যদি আপনি গ্রহণযোগ্য ফলাফল না পান, তাহলে ব্যবহারকারীকে ছবিটি পুনরায় ক্যাপচার করতে বলুন।


ছবিতে লেখা চিনুন

একটি ছবিতে টেক্সট চিনতে, নীচে বর্ণিত পদ্ধতিতে টেক্সট রিকগনিজার চালান।

১. টেক্সট রিকগনিজার চালান

একটি ছবিতে টেক্সট চিনতে, Bitmap , media.Image , ByteBuffer , বাইট অ্যারে, অথবা ডিভাইসের একটি ফাইল থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন। তারপর, FirebaseVisionTextRecognizer এর processImage পদ্ধতিতে FirebaseVisionImage অবজেক্টটি পাস করুন।

  1. আপনার ছবি থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন।

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

      যদি আপনি 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);
    • একটি ফাইল URI থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করতে, অ্যাপের প্রসঙ্গটি পাস করুন এবং URI ফাইলটি 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. FirebaseVisionTextRecognizer এর একটি উদাহরণ পান।

    Kotlin

    val detector = FirebaseVision.getInstance().cloudTextRecognizer
    // Or, to change the default settings:
    // val detector = FirebaseVision.getInstance().getCloudTextRecognizer(options)
    // Or, to provide language hints to assist with language detection:
    // See https://cloud.google.com/vision/docs/languages for supported languages
    val options = FirebaseVisionCloudTextRecognizerOptions.Builder()
        .setLanguageHints(listOf("en", "hi"))
        .build()

    Java

    FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
            .getCloudTextRecognizer();
    // Or, to change the default settings:
    //   FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance()
    //          .getCloudTextRecognizer(options);
    // Or, to provide language hints to assist with language detection:
    // See https://cloud.google.com/vision/docs/languages for supported languages
    FirebaseVisionCloudTextRecognizerOptions options = new FirebaseVisionCloudTextRecognizerOptions.Builder()
            .setLanguageHints(Arrays.asList("en", "hi"))
            .build();
  3. অবশেষে, ছবিটি processImage পদ্ধতিতে পাস করুন:

    Kotlin

    val result = detector.processImage(image)
        .addOnSuccessListener { firebaseVisionText ->
            // Task completed successfully
            // ...
        }
        .addOnFailureListener { e ->
            // Task failed with an exception
            // ...
        }

    Java

    Task<FirebaseVisionText> result =
            detector.processImage(image)
                    .addOnSuccessListener(new OnSuccessListener<FirebaseVisionText>() {
                        @Override
                        public void onSuccess(FirebaseVisionText firebaseVisionText) {
                            // Task completed successfully
                            // ...
                        }
                    })
                    .addOnFailureListener(
                            new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    // Task failed with an exception
                                    // ...
                                }
                            });

2. স্বীকৃত টেক্সটের ব্লক থেকে টেক্সট বের করুন

যদি টেক্সট রিকগনিশন অপারেশন সফল হয়, তাহলে একটি FirebaseVisionText অবজেক্ট সাকসেস লিসেনারের কাছে পাঠানো হবে। একটি FirebaseVisionText অবজেক্টে ছবিতে স্বীকৃত সম্পূর্ণ টেক্সট এবং শূন্য বা তার বেশি TextBlock অবজেক্ট থাকে।

প্রতিটি TextBlock একটি আয়তক্ষেত্রাকার টেক্সট ব্লককে প্রতিনিধিত্ব করে, যাতে শূন্য বা তার বেশি Line অবজেক্ট থাকে। প্রতিটি Line অবজেক্টে শূন্য বা তার বেশি Element অবজেক্ট থাকে, যা শব্দ এবং শব্দের মতো সত্তা (তারিখ, সংখ্যা ইত্যাদি) উপস্থাপন করে।

প্রতিটি TextBlock , Line এবং Element অবজেক্টের জন্য, আপনি অঞ্চলের টেক্সট এবং অঞ্চলের সীমানা স্থানাঙ্কগুলি সনাক্ত করতে পারেন।

উদাহরণস্বরূপ:

Kotlin

val resultText = result.text
for (block in result.textBlocks) {
    val blockText = block.text
    val blockConfidence = block.confidence
    val blockLanguages = block.recognizedLanguages
    val blockCornerPoints = block.cornerPoints
    val blockFrame = block.boundingBox
    for (line in block.lines) {
        val lineText = line.text
        val lineConfidence = line.confidence
        val lineLanguages = line.recognizedLanguages
        val lineCornerPoints = line.cornerPoints
        val lineFrame = line.boundingBox
        for (element in line.elements) {
            val elementText = element.text
            val elementConfidence = element.confidence
            val elementLanguages = element.recognizedLanguages
            val elementCornerPoints = element.cornerPoints
            val elementFrame = element.boundingBox
        }
    }
}

Java

String resultText = result.getText();
for (FirebaseVisionText.TextBlock block: result.getTextBlocks()) {
    String blockText = block.getText();
    Float blockConfidence = block.getConfidence();
    List<RecognizedLanguage> blockLanguages = block.getRecognizedLanguages();
    Point[] blockCornerPoints = block.getCornerPoints();
    Rect blockFrame = block.getBoundingBox();
    for (FirebaseVisionText.Line line: block.getLines()) {
        String lineText = line.getText();
        Float lineConfidence = line.getConfidence();
        List<RecognizedLanguage> lineLanguages = line.getRecognizedLanguages();
        Point[] lineCornerPoints = line.getCornerPoints();
        Rect lineFrame = line.getBoundingBox();
        for (FirebaseVisionText.Element element: line.getElements()) {
            String elementText = element.getText();
            Float elementConfidence = element.getConfidence();
            List<RecognizedLanguage> elementLanguages = element.getRecognizedLanguages();
            Point[] elementCornerPoints = element.getCornerPoints();
            Rect elementFrame = element.getBoundingBox();
        }
    }
}

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


ডকুমেন্টের ছবিতে লেখা চিনুন

একটি নথির টেক্সট চিনতে, নীচে বর্ণিত পদ্ধতিতে ডকুমেন্ট টেক্সট রিকগনিজারটি কনফিগার করুন এবং চালান।

নিচে বর্ণিত ডকুমেন্ট টেক্সট রিকগনিশন API, এমন একটি ইন্টারফেস প্রদান করে যা ডকুমেন্টের ছবি নিয়ে কাজ করার জন্য আরও সুবিধাজনক হওয়ার উদ্দেশ্যে তৈরি। তবে, যদি আপনি FirebaseVisionTextRecognizer API দ্বারা প্রদত্ত ইন্টারফেস পছন্দ করেন, তাহলে আপনি ক্লাউড টেক্সট রিকগনিজারকে ঘন টেক্সট মডেল ব্যবহার করার জন্য কনফিগার করে ডকুমেন্ট স্ক্যান করার জন্য এটি ব্যবহার করতে পারেন।

ডকুমেন্ট টেক্সট রিকগনিশন API ব্যবহার করতে:

১. টেক্সট রিকগনিজার চালান

একটি ছবিতে টেক্সট চিনতে, Bitmap , media.Image , ByteBuffer , বাইট অ্যারে, অথবা ডিভাইসের একটি ফাইল থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন। তারপর, FirebaseVisionImage অবজেক্টটিকে FirebaseVisionDocumentTextRecognizer এর processImage পদ্ধতিতে পাস করুন।

  1. আপনার ছবি থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন।

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

      যদি আপনি 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);
    • একটি ফাইল URI থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করতে, অ্যাপের প্রসঙ্গটি পাস করুন এবং URI ফাইলটি 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. FirebaseVisionDocumentTextRecognizer এর একটি উদাহরণ পান:

    Kotlin

    val detector = FirebaseVision.getInstance()
        .cloudDocumentTextRecognizer
    // Or, to provide language hints to assist with language detection:
    // See https://cloud.google.com/vision/docs/languages for supported languages
    val options = FirebaseVisionCloudDocumentRecognizerOptions.Builder()
        .setLanguageHints(listOf("en", "hi"))
        .build()
    val detector = FirebaseVision.getInstance()
        .getCloudDocumentTextRecognizer(options)

    Java

    FirebaseVisionDocumentTextRecognizer detector = FirebaseVision.getInstance()
            .getCloudDocumentTextRecognizer();
    // Or, to provide language hints to assist with language detection:
    // See https://cloud.google.com/vision/docs/languages for supported languages
    FirebaseVisionCloudDocumentRecognizerOptions options =
            new FirebaseVisionCloudDocumentRecognizerOptions.Builder()
                    .setLanguageHints(Arrays.asList("en", "hi"))
                    .build();
    FirebaseVisionDocumentTextRecognizer detector = FirebaseVision.getInstance()
            .getCloudDocumentTextRecognizer(options);

  3. অবশেষে, ছবিটি processImage পদ্ধতিতে পাস করুন:

    Kotlin

    detector.processImage(myImage)
        .addOnSuccessListener { firebaseVisionDocumentText ->
            // Task completed successfully
            // ...
        }
        .addOnFailureListener { e ->
            // Task failed with an exception
            // ...
        }

    Java

    detector.processImage(myImage)
            .addOnSuccessListener(new OnSuccessListener<FirebaseVisionDocumentText>() {
                @Override
                public void onSuccess(FirebaseVisionDocumentText result) {
                    // Task completed successfully
                    // ...
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    // Task failed with an exception
                    // ...
                }
            });

2. স্বীকৃত টেক্সটের ব্লক থেকে টেক্সট বের করুন

যদি টেক্সট রিকগনিশন অপারেশন সফল হয়, তাহলে এটি একটি FirebaseVisionDocumentText অবজেক্ট ফেরত দেবে। একটি FirebaseVisionDocumentText অবজেক্টে ছবিতে স্বীকৃত সম্পূর্ণ টেক্সট এবং স্বীকৃত ডকুমেন্টের কাঠামো প্রতিফলিত করে এমন বস্তুর একটি শ্রেণিবিন্যাস থাকে:

প্রতিটি Block , Paragraph , Word এবং Symbol বস্তুর জন্য, আপনি অঞ্চলের পাঠ্য এবং অঞ্চলের সীমানা স্থানাঙ্কগুলি সনাক্ত করতে পারেন।

উদাহরণস্বরূপ:

Kotlin

val resultText = result.text
for (block in result.blocks) {
    val blockText = block.text
    val blockConfidence = block.confidence
    val blockRecognizedLanguages = block.recognizedLanguages
    val blockFrame = block.boundingBox
    for (paragraph in block.paragraphs) {
        val paragraphText = paragraph.text
        val paragraphConfidence = paragraph.confidence
        val paragraphRecognizedLanguages = paragraph.recognizedLanguages
        val paragraphFrame = paragraph.boundingBox
        for (word in paragraph.words) {
            val wordText = word.text
            val wordConfidence = word.confidence
            val wordRecognizedLanguages = word.recognizedLanguages
            val wordFrame = word.boundingBox
            for (symbol in word.symbols) {
                val symbolText = symbol.text
                val symbolConfidence = symbol.confidence
                val symbolRecognizedLanguages = symbol.recognizedLanguages
                val symbolFrame = symbol.boundingBox
            }
        }
    }
}

Java

String resultText = result.getText();
for (FirebaseVisionDocumentText.Block block: result.getBlocks()) {
    String blockText = block.getText();
    Float blockConfidence = block.getConfidence();
    List<RecognizedLanguage> blockRecognizedLanguages = block.getRecognizedLanguages();
    Rect blockFrame = block.getBoundingBox();
    for (FirebaseVisionDocumentText.Paragraph paragraph: block.getParagraphs()) {
        String paragraphText = paragraph.getText();
        Float paragraphConfidence = paragraph.getConfidence();
        List<RecognizedLanguage> paragraphRecognizedLanguages = paragraph.getRecognizedLanguages();
        Rect paragraphFrame = paragraph.getBoundingBox();
        for (FirebaseVisionDocumentText.Word word: paragraph.getWords()) {
            String wordText = word.getText();
            Float wordConfidence = word.getConfidence();
            List<RecognizedLanguage> wordRecognizedLanguages = word.getRecognizedLanguages();
            Rect wordFrame = word.getBoundingBox();
            for (FirebaseVisionDocumentText.Symbol symbol: word.getSymbols()) {
                String symbolText = symbol.getText();
                Float symbolConfidence = symbol.getConfidence();
                List<RecognizedLanguage> symbolRecognizedLanguages = symbol.getRecognizedLanguages();
                Rect symbolFrame = symbol.getBoundingBox();
            }
        }
    }
}

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