আপনি ছবির মধ্যে থাকা লেখা শনাক্ত করতে Firebase ML ব্যবহার করতে পারেন। Firebase ML দুটি এপিআই রয়েছে: একটি সাধারণ এপিআই যা রাস্তার সাইনবোর্ডের লেখার মতো ছবির মধ্যে থাকা লেখা শনাক্ত করার জন্য উপযুক্ত, এবং আরেকটি এপিআই যা ডকুমেন্টের লেখা শনাক্ত করার জন্য বিশেষভাবে তৈরি।
শুরু করার আগে
- যদি আগে থেকে না করে থাকেন, তাহলে আপনার অ্যান্ড্রয়েড প্রজেক্টে ফায়ারবেস যোগ করুন ।
- আপনার মডিউল (অ্যাপ-লেভেল) গ্রেডল ফাইলে (সাধারণত
<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:34.11.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 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' }
আপনি যদি আপনার প্রোজেক্টের জন্য এখনও ক্লাউড-ভিত্তিক এপিআই চালু না করে থাকেন, তবে এখনই তা করুন:
- Firebase কনসোলে Firebase ML APIs পৃষ্ঠাটি খুলুন।
আপনি যদি এখনও আপনার প্রজেক্টটি পে-অ্যাজ-ইউ-গো ব্লেজ প্রাইসিং প্ল্যানে আপগ্রেড না করে থাকেন, তাহলে তা করার জন্য 'আপগ্রেড' বাটনে ক্লিক করুন। (শুধুমাত্র যদি আপনার প্রজেক্টটি ব্লেজ প্রাইসিং প্ল্যানে না থাকে, তবেই আপনাকে আপগ্রেড করার জন্য অনুরোধ করা হবে।)
শুধুমাত্র ব্লেজ প্রাইসিং প্ল্যানে থাকা প্রজেক্টগুলোই ক্লাউড-ভিত্তিক এপিআই ব্যবহার করতে পারে।
- যদি ক্লাউড-ভিত্তিক এপিআই আগে থেকে সক্রিয় করা না থাকে, তাহলে ‘ক্লাউড-ভিত্তিক এপিআই সক্রিয় করুন’ এ ক্লিক করুন।
এখন আপনি ছবির মধ্যে থাকা লেখা শনাক্ত করা শুরু করতে প্রস্তুত।
ইনপুট ছবির নির্দেশিকা
Firebase ML পক্ষে টেক্সট সঠিকভাবে শনাক্ত করার জন্য, ইনপুট ইমেজগুলিতে এমন টেক্সট থাকতে হবে যা পর্যাপ্ত পিক্সেল ডেটা দ্বারা উপস্থাপিত। আদর্শগতভাবে, ল্যাটিন টেক্সটের জন্য প্রতিটি অক্ষর কমপক্ষে ১৬x১৬ পিক্সেল হওয়া উচিত। চীনা, জাপানি এবং কোরিয়ান টেক্সটের জন্য প্রতিটি অক্ষর ২৪x২৪ পিক্সেল হওয়া উচিত। সব ভাষার ক্ষেত্রেই, অক্ষরগুলো ২৪x২৪ পিক্সেলের চেয়ে বড় হলে নির্ভুলতার দিক থেকে সাধারণত কোনো সুবিধা হয় না।
সুতরাং, উদাহরণস্বরূপ, একটি বিজনেস কার্ড স্ক্যান করার জন্য একটি 640x480 সাইজের ছবি বেশ কার্যকর হতে পারে, যদি কার্ডটি ছবির সম্পূর্ণ প্রস্থ জুড়ে থাকে। লেটার-সাইজের কাগজে প্রিন্ট করা কোনো ডকুমেন্ট স্ক্যান করার জন্য 720x1280 পিক্সেলের একটি ছবির প্রয়োজন হতে পারে।
ছবির ফোকাস ঠিক না থাকলে টেক্সট শনাক্তকরণের নির্ভুলতা কমে যেতে পারে। যদি আপনি গ্রহণযোগ্য ফলাফল না পান, তবে ব্যবহারকারীকে ছবিটি পুনরায় তুলতে বলুন।
ছবিতে থাকা লেখা শনাক্ত করুন
কোনো ছবিতে থাকা লেখা শনাক্ত করতে, নিচে বর্ণিত পদ্ধতি অনুযায়ী টেক্সট রিকগনাইজারটি চালান।
১. টেক্সট রিকগনাইজারটি চালান।
কোনো ইমেজের মধ্যে থাকা টেক্সট শনাক্ত করতে, ডিভাইসেরBitmap , media.Image , ByteBuffer , বাইট অ্যারে বা কোনো ফাইল থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন। এরপর, FirebaseVisionTextRecognizer এর processImage মেথডে FirebaseVisionImage অবজেক্টটি পাস করুন।আপনার ইমেজ থেকে একটি
FirebaseVisionImageঅবজেক্ট তৈরি করুন।একটি
media.Imageঅবজেক্ট থেকে একটিFirebaseVisionImageঅবজেক্ট তৈরি করতে, যেমন ডিভাইসের ক্যামেরা থেকে ছবি তোলার সময়,FirebaseVisionImage.fromMediaImage()ফাংশনেmedia.Imageঅবজেক্টটি এবং ছবিটির রোটেশন পাস করুন।আপনি যদি 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 // ... } }
যদি আপনি এমন কোনো ক্যামেরা লাইব্রেরি ব্যবহার না করেন যা আপনাকে ছবির ঘূর্ণন (rotation) দেয়, তাহলে আপনি ডিভাইসের ঘূর্ণন এবং ডিভাইসের ক্যামেরা সেন্সরের অভিমুখ (orientation) থেকে এটি গণনা করতে পারেন:
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অবজেক্ট তৈরি করতে,FirebaseVisionImage.fromFilePath()-এ অ্যাপ কনটেক্সট এবং ফাইল URI পাস করুন। এটি তখন কাজে আসে যখন আপনি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অবজেক্ট দ্বারা উপস্থাপিত চিত্রটি অবশ্যই খাড়া হতে হবে, এর জন্য কোনো অতিরিক্ত ঘূর্ণনের প্রয়োজন নেই।
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();
অবশেষে, ছবিটি
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 // ... } });
২. শনাক্তকৃত টেক্সটের ব্লকগুলো থেকে টেক্সট বের করুন।
টেক্সট শনাক্তকরণ প্রক্রিয়া সফল হলে, একটি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 এপিআই দ্বারা প্রদত্ত ইন্টারফেসটি পছন্দ করেন, তাহলে ক্লাউড টেক্সট রিকগনাইজারকে ডেন্স টেক্সট মডেল ব্যবহার করার জন্য কনফিগার করে ডকুমেন্ট স্ক্যান করার জন্য এর পরিবর্তে সেটি ব্যবহার করতে পারেন।
ডকুমেন্ট টেক্সট রিকগনিশন এপিআই ব্যবহার করতে:
১. টেক্সট রিকগনাইজারটি চালান।
কোনো ইমেজের মধ্যে থাকা টেক্সট শনাক্ত করতে, ডিভাইসেরBitmap , media.Image , ByteBuffer , বাইট অ্যারে বা কোনো ফাইল থেকে একটি FirebaseVisionImage অবজেক্ট তৈরি করুন। এরপর, FirebaseVisionImage অবজেক্টটি FirebaseVisionDocumentTextRecognizer এর processImage মেথডে পাস করুন।আপনার ইমেজ থেকে একটি
FirebaseVisionImageঅবজেক্ট তৈরি করুন।একটি
media.Imageঅবজেক্ট থেকে একটিFirebaseVisionImageঅবজেক্ট তৈরি করতে, যেমন ডিভাইসের ক্যামেরা থেকে ছবি তোলার সময়,FirebaseVisionImage.fromMediaImage()ফাংশনেmedia.Imageঅবজেক্টটি এবং ছবিটির রোটেশন পাস করুন।আপনি যদি 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 // ... } }
যদি আপনি এমন কোনো ক্যামেরা লাইব্রেরি ব্যবহার না করেন যা আপনাকে ছবির ঘূর্ণন (rotation) দেয়, তাহলে আপনি ডিভাইসের ঘূর্ণন এবং ডিভাইসের ক্যামেরা সেন্সরের অভিমুখ (orientation) থেকে এটি গণনা করতে পারেন:
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অবজেক্ট তৈরি করতে,FirebaseVisionImage.fromFilePath()-এ অ্যাপ কনটেক্সট এবং ফাইল URI পাস করুন। এটি তখন কাজে আসে যখন আপনি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অবজেক্ট দ্বারা উপস্থাপিত চিত্রটি অবশ্যই খাড়া হতে হবে, এর জন্য কোনো অতিরিক্ত ঘূর্ণনের প্রয়োজন নেই।
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);
অবশেষে, ছবিটি
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 // ... } });
২. শনাক্তকৃত টেক্সটের ব্লকগুলো থেকে টেক্সট বের করুন।
টেক্সট শনাক্তকরণ প্রক্রিয়াটি সফল হলে, এটি একটি FirebaseVisionDocumentText অবজেক্ট রিটার্ন করবে। একটি FirebaseVisionDocumentText অবজেক্টে ছবিতে শনাক্ত করা সম্পূর্ণ টেক্সট এবং শনাক্তকৃত ডকুমেন্টের কাঠামো প্রতিফলিত করে এমন অবজেক্টের একটি স্তরক্রম থাকে:
-
FirebaseVisionDocumentText.Block -
FirebaseVisionDocumentText.Paragraph -
FirebaseVisionDocumentText.Word -
FirebaseVisionDocumentText.Symbol
প্রতিটি 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(); } } } }
পরবর্তী পদক্ষেপ
- ক্লাউড এপিআই ব্যবহার করে এমন কোনো অ্যাপ প্রোডাকশনে ডেপ্লয় করার আগে, অননুমোদিত এপিআই অ্যাক্সেসের প্রভাব প্রতিরোধ ও প্রশমিত করার জন্য আপনার কিছু অতিরিক্ত পদক্ষেপ নেওয়া উচিত।