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

আপনি ছবিতে টেক্সট চিনতে Firebase ML ব্যবহার করতে পারেন। Firebase ML-এর উভয়ই একটি সাধারণ-উদ্দেশ্য API রয়েছে যা চিত্রগুলিতে পাঠ্য সনাক্ত করার জন্য উপযুক্ত, যেমন একটি রাস্তার চিহ্নের পাঠ্য এবং নথির পাঠ্য সনাক্ত করার জন্য অপ্টিমাইজ করা একটি 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গুলি সক্ষম করুন ক্লিক করুন৷

এখন আপনি চিত্রগুলিতে পাঠ্য সনাক্তকরণ শুরু করতে প্রস্তুত৷

ইনপুট ইমেজ নির্দেশিকা

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

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

  • খারাপ ইমেজ ফোকাস টেক্সট স্বীকৃতি সঠিকতা আঘাত করতে পারে. আপনি যদি গ্রহণযোগ্য ফলাফল না পান, তাহলে ব্যবহারকারীকে ছবিটি পুনরায় ক্যাপচার করতে বলার চেষ্টা করুন।


চিত্রে পাঠ্য চিনুন

একটি ছবিতে পাঠ্য সনাক্ত করতে, নীচে বর্ণিত হিসাবে পাঠ্য শনাক্তকারী চালান।

1. পাঠ্য শনাক্তকারী চালান

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

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

    • একটি 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. FirebaseVisionTextRecognizer এর একটি উদাহরণ পান।

    Kotlin+KTX

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

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

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 ব্যবহার করতে:

1. পাঠ্য শনাক্তকারী চালান

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

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

    • একটি 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. FirebaseVisionDocumentTextRecognizer এর একটি উদাহরণ পান:

    Kotlin+KTX

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

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

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();
            }
        }
    }
}

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