ছবিতে স্বীকৃত বস্তুগুলিকে লেবেল করার জন্য আপনি Firebase ML ব্যবহার করতে পারেন। এই API এর বৈশিষ্ট্যগুলি সম্পর্কে তথ্যের জন্য ওভারভিউ দেখুন।
শুরু করার আগে
- যদি আপনি ইতিমধ্যেই না করে থাকেন, তাহলে আপনার অ্যান্ড্রয়েড প্রজেক্টে Firebase যোগ করুন ।
- আপনার মডিউল (অ্যাপ-লেভেল) গ্র্যাডেল ফাইলে (সাধারণত
<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' }
যদি আপনি ইতিমধ্যেই আপনার প্রকল্পের জন্য ক্লাউড-ভিত্তিক API সক্রিয় না করে থাকেন, তাহলে এখনই তা করুন:
- Firebase কনসোলে Firebase ML API পৃষ্ঠাটি খুলুন।
যদি আপনি ইতিমধ্যেই আপনার প্রকল্পটিকে পে-অ্যাজ-ইউ-গো ব্লেজ প্রাইসিং প্ল্যানে আপগ্রেড না করে থাকেন, তাহলে আপগ্রেডে ক্লিক করে তা করুন। (আপনার প্রকল্পটি ব্লেজ প্রাইসিং প্ল্যানে না থাকলেই আপনাকে আপগ্রেড করতে বলা হবে।)
শুধুমাত্র ব্লেজ প্রাইসিং প্ল্যানের প্রকল্পগুলিই ক্লাউড-ভিত্তিক API ব্যবহার করতে পারবে।
- যদি ক্লাউড-ভিত্তিক API গুলি ইতিমধ্যেই সক্ষম না থাকে, তাহলে ক্লাউড-ভিত্তিক API গুলি সক্ষম করুন এ ক্লিক করুন।
এখন আপনি ছবি লেবেল করার জন্য প্রস্তুত।
১. ইনপুট ইমেজ প্রস্তুত করুন
আপনার ছবি থেকে একটিFirebaseVisionImage অবজেক্ট তৈরি করুন। যখন আপনি একটি Bitmap ব্যবহার করেন অথবা যদি আপনি camera2 API ব্যবহার করেন, তাহলে একটি JPEG-ফর্ম্যাটেড media.Image ব্যবহার করেন, যা সম্ভব হলে সুপারিশ করা হয়, তখন ছবির লেবেলারটি সবচেয়ে দ্রুত চলে।একটি
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অবজেক্ট দ্বারা উপস্থাপিত ছবিটি অবশ্যই খাড়া হতে হবে, অতিরিক্ত ঘূর্ণনের প্রয়োজন হবে না।
২. ইমেজ লেবেলার কনফিগার করুন এবং চালান
একটি ছবিতে অবজেক্ট লেবেল করতে,FirebaseVisionImage অবজেক্টটিকে FirebaseVisionImageLabeler এর processImage পদ্ধতিতে পাস করুন।প্রথমে,
FirebaseVisionImageLabelerএর একটি উদাহরণ নিন।Kotlin
val labeler = FirebaseVision.getInstance().getCloudImageLabeler() // Or, to set the minimum confidence required: // val options = FirebaseVisionCloudImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build() // val labeler = FirebaseVision.getInstance().getCloudImageLabeler(options)Java
FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() .getCloudImageLabeler(); // Or, to set the minimum confidence required: // FirebaseVisionCloudImageLabelerOptions options = // new FirebaseVisionCloudImageLabelerOptions.Builder() // .setConfidenceThreshold(0.7f) // .build(); // FirebaseVisionImageLabeler labeler = FirebaseVision.getInstance() // .getCloudImageLabeler(options);তারপর, ছবিটি
processImage()পদ্ধতিতে পাস করুন:Kotlin
labeler.processImage(image) .addOnSuccessListener { labels -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }Java
labeler.processImage(image) .addOnSuccessListener(new OnSuccessListener<List<FirebaseVisionImageLabel>>() { @Override public void onSuccess(List<FirebaseVisionImageLabel> labels) { // Task completed successfully // ... } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { // Task failed with an exception // ... } });
৩. লেবেলযুক্ত বস্তু সম্পর্কে তথ্য পান
যদি ইমেজ লেবেলিং অপারেশন সফল হয়, তাহলেFirebaseVisionImageLabel অবজেক্টের একটি তালিকা সাকসেস লিসেনারের কাছে পাঠানো হবে। প্রতিটি FirebaseVisionImageLabel অবজেক্ট এমন কিছু উপস্থাপন করে যা ছবিতে লেবেল করা ছিল। প্রতিটি লেবেলের জন্য, আপনি লেবেলের টেক্সট বিবরণ, এর নলেজ গ্রাফ এন্টিটি আইডি (যদি পাওয়া যায়) এবং ম্যাচের কনফিডেন্স স্কোর পেতে পারেন। উদাহরণস্বরূপ: Kotlin
for (label in labels) {
val text = label.text
val entityId = label.entityId
val confidence = label.confidence
}
Java
for (FirebaseVisionImageLabel label: labels) {
String text = label.getText();
String entityId = label.getEntityId();
float confidence = label.getConfidence();
}
পরবর্তী পদক্ষেপ
- ক্লাউড এপিআই ব্যবহার করে এমন কোনও অ্যাপ তৈরিতে স্থাপন করার আগে, অননুমোদিত এপিআই অ্যাক্সেসের প্রভাব প্রতিরোধ এবং প্রশমিত করার জন্য আপনার কিছু অতিরিক্ত পদক্ষেপ নেওয়া উচিত।