आप छवियों में टेक्स्ट को पहचानने के लिए एमएल किट का उपयोग कर सकते हैं। एमएल किट में छवियों में पाठ को पहचानने के लिए उपयुक्त एक सामान्य प्रयोजन एपीआई है, जैसे सड़क चिह्न का पाठ, और दस्तावेजों के पाठ को पहचानने के लिए अनुकूलित एपीआई। सामान्य प्रयोजन एपीआई में ऑन-डिवाइस और क्लाउड-आधारित दोनों मॉडल हैं। दस्तावेज़ पाठ पहचान केवल क्लाउड-आधारित मॉडल के रूप में उपलब्ध है। क्लाउड और ऑन-डिवाइस मॉडल की तुलना के लिए अवलोकन देखें।
शुरू करने से पहले
- यदि आपने पहले से नहीं किया है, तो अपने एंड्रॉइड प्रोजेक्ट में फायरबेस जोड़ें ।
- अपने मॉड्यूल (ऐप-स्तर) ग्रैडल फ़ाइल (आमतौर पर
app/build.gradle
) में एमएल किट एंड्रॉइड लाइब्रेरीज़ के लिए निर्भरताएं जोड़ें:apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' dependencies { // ... implementation 'com.google.firebase:firebase-ml-vision:24.0.3' }
- वैकल्पिक लेकिन अनुशंसित : यदि आप ऑन-डिवाइस एपीआई का उपयोग करते हैं, तो प्ले स्टोर से आपका ऐप इंस्टॉल होने के बाद डिवाइस पर एमएल मॉडल को स्वचालित रूप से डाउनलोड करने के लिए अपने ऐप को कॉन्फ़िगर करें।
ऐसा करने के लिए, अपने ऐप की
AndroidManifest.xml
फ़ाइल में निम्नलिखित घोषणा जोड़ें:<application ...> ... <meta-data android:name="com.google.firebase.ml.vision.DEPENDENCIES" android:value="ocr" /> <!-- To use multiple models: android:value="ocr,model2,model3" --> </application>
यदि आप इंस्टॉल-टाइम मॉडल डाउनलोड सक्षम नहीं करते हैं, तो पहली बार ऑन-डिवाइस डिटेक्टर चलाने पर मॉडल डाउनलोड हो जाएगा। डाउनलोड पूरा होने से पहले आपके द्वारा किए गए अनुरोधों का कोई परिणाम नहीं मिलेगा। यदि आप क्लाउड-आधारित मॉडल का उपयोग करना चाहते हैं, और आपने पहले से ही अपने प्रोजेक्ट के लिए क्लाउड-आधारित एपीआई सक्षम नहीं किया है, तो अभी ऐसा करें:
- फायरबेस कंसोल का एमएल किट एपीआई पेज खोलें।
यदि आपने पहले से ही अपने प्रोजेक्ट को ब्लेज़ मूल्य निर्धारण योजना में अपग्रेड नहीं किया है, तो ऐसा करने के लिए अपग्रेड पर क्लिक करें। (आपको केवल तभी अपग्रेड करने के लिए प्रेरित किया जाएगा यदि आपका प्रोजेक्ट ब्लेज़ योजना पर नहीं है।)
केवल ब्लेज़-स्तरीय प्रोजेक्ट ही क्लाउड-आधारित एपीआई का उपयोग कर सकते हैं।
- यदि क्लाउड-आधारित एपीआई पहले से सक्षम नहीं हैं, तो क्लाउड-आधारित एपीआई सक्षम करें पर क्लिक करें।
यदि आप केवल ऑन-डिवाइस मॉडल का उपयोग करना चाहते हैं, तो आप इस चरण को छोड़ सकते हैं।
अब आप छवियों में टेक्स्ट को पहचानना शुरू करने के लिए तैयार हैं।
इनपुट छवि दिशानिर्देश
एमएल किट में टेक्स्ट को सटीक रूप से पहचानने के लिए, इनपुट छवियों में वह टेक्स्ट होना चाहिए जो पर्याप्त पिक्सेल डेटा द्वारा दर्शाया गया हो। आदर्श रूप से, लैटिन पाठ के लिए, प्रत्येक अक्षर कम से कम 16x16 पिक्सेल का होना चाहिए। चीनी, जापानी और कोरियाई पाठ (केवल क्लाउड-आधारित एपीआई द्वारा समर्थित) के लिए, प्रत्येक वर्ण 24x24 पिक्सेल होना चाहिए। सभी भाषाओं के लिए, 24x24 पिक्सेल से बड़े वर्णों के लिए आमतौर पर कोई सटीकता लाभ नहीं होता है।
इसलिए, उदाहरण के लिए, एक 640x480 छवि एक व्यवसाय कार्ड को स्कैन करने के लिए अच्छी तरह से काम कर सकती है जो छवि की पूरी चौड़ाई घेरती है। अक्षर के आकार के कागज़ पर मुद्रित दस्तावेज़ को स्कैन करने के लिए, 720x1280 पिक्सेल छवि की आवश्यकता हो सकती है।
खराब छवि फोकस पाठ पहचान सटीकता को नुकसान पहुंचा सकता है। यदि आपको स्वीकार्य परिणाम नहीं मिल रहे हैं, तो उपयोगकर्ता से छवि पुनः प्राप्त करने के लिए कहें।
यदि आप वास्तविक समय एप्लिकेशन में टेक्स्ट को पहचान रहे हैं, तो आप इनपुट छवियों के समग्र आयामों पर भी विचार करना चाहेंगे। छोटी छवियों को तेजी से संसाधित किया जा सकता है, इसलिए विलंबता को कम करने के लिए, कम रिज़ॉल्यूशन पर छवियों को कैप्चर करें (उपरोक्त सटीकता आवश्यकताओं को ध्यान में रखते हुए) और सुनिश्चित करें कि पाठ जितना संभव हो उतना छवि पर कब्जा कर ले। वास्तविक समय के प्रदर्शन को बेहतर बनाने के लिए युक्तियाँ भी देखें।
छवियों में पाठ को पहचानें
ऑन-डिवाइस या क्लाउड-आधारित मॉडल का उपयोग करके किसी छवि में टेक्स्ट को पहचानने के लिए, नीचे बताए अनुसार टेक्स्ट पहचानकर्ता चलाएँ।
1. पाठ पहचानकर्ता चलाएँ
किसी छवि में टेक्स्ट को पहचानने के लिए,Bitmap
, media.Image
, ByteBuffer
, बाइट सरणी, या डिवाइस पर एक फ़ाइल से एक FirebaseVisionImage
ऑब्जेक्ट बनाएं। फिर, FirebaseVisionImage
ऑब्जेक्ट को FirebaseVisionTextRecognizer
की processImage
विधि में पास करें।अपनी छवि से एक
FirebaseVisionImage
ऑब्जेक्ट बनाएं।media.Image
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, जैसे किसी डिवाइस के कैमरे से छवि कैप्चर करते समय,media.Image
ऑब्जेक्ट और छवि के रोटेशन कोFirebaseVisionImage.fromMediaImage()
पर पास करें।यदि आप कैमराएक्स लाइब्रेरी का उपयोग करते हैं, तो
OnImageCapturedListener
औरImageAnalysis.Analyzer
कक्षाएं आपके लिए रोटेशन मान की गणना करती हैं, इसलिए आपकोFirebaseVisionImage.fromMediaImage()
कॉल करने से पहले रोटेशन को एमएल किट केROTATION_
स्थिरांक में से एक में परिवर्तित करने की आवश्यकता है: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 Kit Vision API // ... } }
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 Kit Vision API // ... } } }
यदि आप ऐसी कैमरा लाइब्रेरी का उपयोग नहीं करते हैं जो आपको छवि का रोटेशन देती है, तो आप डिवाइस के रोटेशन और डिवाइस में कैमरा सेंसर के ओरिएंटेशन से इसकी गणना कर सकते हैं:
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; }
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 }
फिर,
media.Image
ऑब्जेक्ट और रोटेशन मान कोFirebaseVisionImage.fromMediaImage()
पर पास करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
Kotlin+KTX
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
- फ़ाइल URI से
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, ऐप संदर्भ पास करें और URI कोFirebaseVisionImage.fromFilePath()
पर फ़ाइल करें। यह तब उपयोगी होता है जब आप उपयोगकर्ता को उनके गैलरी ऐप से एक छवि चुनने के लिए संकेत देने के लिएACTION_GET_CONTENT
इरादे का उपयोग करते हैं।Java
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Kotlin+KTX
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
-
ByteBuffer
या बाइट सरणी सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, पहलेmedia.Image
इनपुट के लिए ऊपर बताए अनुसार छवि रोटेशन की गणना करें।फिर, एक
FirebaseVisionImageMetadata
ऑब्जेक्ट बनाएं जिसमें छवि की ऊंचाई, चौड़ाई, रंग एन्कोडिंग प्रारूप और रोटेशन शामिल हो: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();
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()
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए बफ़र या सरणी और मेटाडेटा ऑब्जेक्ट का उपयोग करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
Kotlin+KTX
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
-
Bitmap
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए:Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Kotlin+KTX
val image = FirebaseVisionImage.fromBitmap(bitmap)
Bitmap
ऑब्जेक्ट द्वारा दर्शाई गई छवि सीधी होनी चाहिए, जिसमें किसी अतिरिक्त घुमाव की आवश्यकता नहीं है।
FirebaseVisionTextRecognizer
का एक उदाहरण प्राप्त करें।ऑन-डिवाइस मॉडल का उपयोग करने के लिए:
Java
FirebaseVisionTextRecognizer detector = FirebaseVision.getInstance() .getOnDeviceTextRecognizer();
Kotlin+KTX
val detector = FirebaseVision.getInstance() .onDeviceTextRecognizer
क्लाउड-आधारित मॉडल का उपयोग करने के लिए:
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();
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()
अंत में, छवि को
processImage
विधि में पास करें: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 // ... } });
Kotlin+KTX
val result = detector.processImage(image) .addOnSuccessListener { firebaseVisionText -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
2. मान्यता प्राप्त पाठ के ब्लॉक से पाठ निकालें
यदि टेक्स्ट पहचान ऑपरेशन सफल होता है, तो एकFirebaseVisionText
ऑब्जेक्ट सफल श्रोता को भेज दिया जाएगा। FirebaseVisionText
ऑब्जेक्ट में छवि में पहचाना गया पूरा टेक्स्ट और शून्य या अधिक TextBlock
ऑब्जेक्ट होते हैं। प्रत्येक TextBlock
टेक्स्ट के एक आयताकार ब्लॉक का प्रतिनिधित्व करता है, जिसमें शून्य या अधिक Line
ऑब्जेक्ट होते हैं। प्रत्येक Line
ऑब्जेक्ट में शून्य या अधिक Element
ऑब्जेक्ट होते हैं, जो शब्दों और शब्द-जैसी संस्थाओं (तिथियां, संख्याएं, और इसी तरह) का प्रतिनिधित्व करते हैं।
प्रत्येक TextBlock
, Line
और Element
ऑब्जेक्ट के लिए, आप क्षेत्र में पहचाने गए टेक्स्ट और क्षेत्र के बाउंडिंग निर्देशांक प्राप्त कर सकते हैं।
उदाहरण के लिए:
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(); } } }
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 } } }
वास्तविक समय के प्रदर्शन को बेहतर बनाने के लिए युक्तियाँ
यदि आप रीयल-टाइम एप्लिकेशन में टेक्स्ट को पहचानने के लिए ऑन-डिवाइस मॉडल का उपयोग करना चाहते हैं, तो सर्वोत्तम फ़्रेमरेट प्राप्त करने के लिए इन दिशानिर्देशों का पालन करें:
- टेक्स्ट पहचानकर्ता को थ्रॉटल कॉल। यदि टेक्स्ट पहचानकर्ता के चलने के दौरान कोई नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो फ़्रेम को छोड़ दें।
- यदि आप इनपुट छवि पर ग्राफिक्स को ओवरले करने के लिए टेक्स्ट पहचानकर्ता के आउटपुट का उपयोग कर रहे हैं, तो पहले एमएल किट से परिणाम प्राप्त करें, फिर छवि को प्रस्तुत करें और एक ही चरण में ओवरले करें। ऐसा करने से, आप प्रत्येक इनपुट फ़्रेम के लिए केवल एक बार डिस्प्ले सतह पर रेंडर करते हैं।
यदि आप कैमरा2 एपीआई का उपयोग करते हैं, तो
ImageFormat.YUV_420_888
प्रारूप में छवियां कैप्चर करें।यदि आप पुराने कैमरा एपीआई का उपयोग करते हैं, तो
ImageFormat.NV21
प्रारूप में छवियां कैप्चर करें।- कम रिज़ॉल्यूशन पर छवियाँ कैप्चर करने पर विचार करें। हालाँकि, इस एपीआई की छवि आयाम आवश्यकताओं को भी ध्यान में रखें।
अगले कदम
- इससे पहले कि आप क्लाउड एपीआई का उपयोग करने वाले किसी ऐप का उत्पादन शुरू करें, आपको अनधिकृत एपीआई एक्सेस के प्रभाव को रोकने और कम करने के लिए कुछ अतिरिक्त कदम उठाने चाहिए।
दस्तावेज़ों की छवियों में टेक्स्ट को पहचानें
किसी दस्तावेज़ के टेक्स्ट को पहचानने के लिए, नीचे बताए अनुसार क्लाउड-आधारित दस्तावेज़ टेक्स्ट पहचानकर्ता को कॉन्फ़िगर करें और चलाएं।
नीचे वर्णित दस्तावेज़ पाठ पहचान एपीआई, एक इंटरफ़ेस प्रदान करता है जिसका उद्देश्य दस्तावेज़ों की छवियों के साथ काम करने के लिए अधिक सुविधाजनक होना है। हालाँकि, यदि आप FirebaseVisionTextRecognizer
API द्वारा प्रदान किए गए इंटरफ़ेस को पसंद करते हैं, तो आप घने टेक्स्ट मॉडल का उपयोग करने के लिए क्लाउड टेक्स्ट पहचानकर्ता को कॉन्फ़िगर करके दस्तावेज़ों को स्कैन करने के लिए इसका उपयोग कर सकते हैं।
दस्तावेज़ टेक्स्ट पहचान एपीआई का उपयोग करने के लिए:
1. पाठ पहचानकर्ता चलाएँ
किसी छवि में टेक्स्ट को पहचानने के लिए,Bitmap
, media.Image
, ByteBuffer
, बाइट सरणी, या डिवाइस पर एक फ़ाइल से एक FirebaseVisionImage
ऑब्जेक्ट बनाएं। फिर, FirebaseVisionImage
ऑब्जेक्ट को FirebaseVisionDocumentTextRecognizer
की processImage
विधि में पास करें।अपनी छवि से एक
FirebaseVisionImage
ऑब्जेक्ट बनाएं।media.Image
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, जैसे किसी डिवाइस के कैमरे से छवि कैप्चर करते समय,media.Image
ऑब्जेक्ट और छवि के रोटेशन कोFirebaseVisionImage.fromMediaImage()
पर पास करें।यदि आप कैमराएक्स लाइब्रेरी का उपयोग करते हैं, तो
OnImageCapturedListener
औरImageAnalysis.Analyzer
कक्षाएं आपके लिए रोटेशन मान की गणना करती हैं, इसलिए आपकोFirebaseVisionImage.fromMediaImage()
कॉल करने से पहले रोटेशन को एमएल किट केROTATION_
स्थिरांक में से एक में परिवर्तित करने की आवश्यकता है: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 Kit Vision API // ... } }
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 Kit Vision API // ... } } }
यदि आप ऐसी कैमरा लाइब्रेरी का उपयोग नहीं करते हैं जो आपको छवि का रोटेशन देती है, तो आप डिवाइस के रोटेशन और डिवाइस में कैमरा सेंसर के ओरिएंटेशन से इसकी गणना कर सकते हैं:
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; }
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 }
फिर,
media.Image
ऑब्जेक्ट और रोटेशन मान कोFirebaseVisionImage.fromMediaImage()
पर पास करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation);
Kotlin+KTX
val image = FirebaseVisionImage.fromMediaImage(mediaImage, rotation)
- फ़ाइल URI से
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, ऐप संदर्भ पास करें और URI कोFirebaseVisionImage.fromFilePath()
पर फ़ाइल करें। यह तब उपयोगी होता है जब आप उपयोगकर्ता को उनके गैलरी ऐप से एक छवि चुनने के लिए संकेत देने के लिएACTION_GET_CONTENT
इरादे का उपयोग करते हैं।Java
FirebaseVisionImage image; try { image = FirebaseVisionImage.fromFilePath(context, uri); } catch (IOException e) { e.printStackTrace(); }
Kotlin+KTX
val image: FirebaseVisionImage try { image = FirebaseVisionImage.fromFilePath(context, uri) } catch (e: IOException) { e.printStackTrace() }
-
ByteBuffer
या बाइट सरणी सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए, पहलेmedia.Image
इनपुट के लिए ऊपर बताए अनुसार छवि रोटेशन की गणना करें।फिर, एक
FirebaseVisionImageMetadata
ऑब्जेक्ट बनाएं जिसमें छवि की ऊंचाई, चौड़ाई, रंग एन्कोडिंग प्रारूप और रोटेशन शामिल हो: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();
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()
FirebaseVisionImage
ऑब्जेक्ट बनाने के लिए बफ़र या सरणी और मेटाडेटा ऑब्जेक्ट का उपयोग करें:Java
FirebaseVisionImage image = FirebaseVisionImage.fromByteBuffer(buffer, metadata); // Or: FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(byteArray, metadata);
Kotlin+KTX
val image = FirebaseVisionImage.fromByteBuffer(buffer, metadata) // Or: val image = FirebaseVisionImage.fromByteArray(byteArray, metadata)
-
Bitmap
ऑब्जेक्ट सेFirebaseVisionImage
ऑब्जेक्ट बनाने के लिए:Java
FirebaseVisionImage image = FirebaseVisionImage.fromBitmap(bitmap);
Kotlin+KTX
val image = FirebaseVisionImage.fromBitmap(bitmap)
Bitmap
ऑब्जेक्ट द्वारा दर्शाई गई छवि सीधी होनी चाहिए, जिसमें किसी अतिरिक्त घुमाव की आवश्यकता नहीं है।
FirebaseVisionDocumentTextRecognizer
का एक उदाहरण प्राप्त करें: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);
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)
अंत में, छवि को
processImage
विधि में पास करें: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 // ... } });
Kotlin+KTX
detector.processImage(myImage) .addOnSuccessListener { firebaseVisionDocumentText -> // Task completed successfully // ... } .addOnFailureListener { e -> // Task failed with an exception // ... }
2. मान्यता प्राप्त पाठ के ब्लॉक से पाठ निकालें
यदि टेक्स्ट पहचान ऑपरेशन सफल होता है, तो यह एक FirebaseVisionDocumentText
ऑब्जेक्ट लौटाएगा। FirebaseVisionDocumentText
ऑब्जेक्ट में छवि में पहचाना गया पूरा टेक्स्ट और ऑब्जेक्ट का एक पदानुक्रम होता है जो मान्यता प्राप्त दस्तावेज़ की संरचना को दर्शाता है:
-
FirebaseVisionDocumentText.Block
-
FirebaseVisionDocumentText.Paragraph
-
FirebaseVisionDocumentText.Word
-
FirebaseVisionDocumentText.Symbol
प्रत्येक Block
, Paragraph
, Word
और Symbol
ऑब्जेक्ट के लिए, आप क्षेत्र में पहचाने गए टेक्स्ट और क्षेत्र के बाउंडिंग निर्देशांक प्राप्त कर सकते हैं।
उदाहरण के लिए:
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(); } } } }
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 } } } }
अगले कदम
- इससे पहले कि आप क्लाउड एपीआई का उपयोग करने वाले किसी ऐप का उत्पादन शुरू करें, आपको अनधिकृत एपीआई एक्सेस के प्रभाव को रोकने और कम करने के लिए कुछ अतिरिक्त कदम उठाने चाहिए।