iOS पर ML Kit की मदद से इमेज में मौजूद टेक्स्ट की पहचान करना

आप इमेज या वीडियो में मौजूद टेक्स्ट के लिए एमएल किट का इस्तेमाल कर सकते हैं. जैसे, स्ट्रीट साइन का टेक्स्ट. इस सुविधा की मुख्य विशेषताएं ये हैं:

टेक्स्ट की पहचान करने वाला एपीआई
ब्यौराइमेज या वीडियो में लैटिन स्क्रिप्ट के लेख को पहचानें.
SDK टूल का नामGoogleMLKit/TextRecognition (version 2.2.0)
लागू करनाएसेट, बिल्ड टाइम पर आपके ऐप्लिकेशन से स्टैटिक तौर पर लिंक होते हैं.
ऐप्लिकेशन के साइज़ पर असरकरीब 20 एमबी
परफ़ॉर्मेंसज़्यादातर डिवाइसों पर रीयल-टाइम.

इसे आज़माएं

शुरू करने से पहले

  1. अपनी Podfile में ये ML किट पॉड शामिल करें:
    pod 'GoogleMLKit/TextRecognition','2.2.0'
    
  2. अपने प्रोजेक्ट के पॉड इंस्टॉल करने या अपडेट करने के बाद, अपने .xcworkspaceकोड का इस्तेमाल करके Xcode प्रोजेक्ट खोलें. ML किट, Xcode के 12.4 या इसके बाद के वर्शन पर काम करती है.

1. TextRecognizer का इंस्टेंस बनाएं

+textRecognizer पर कॉल करके TextRecognizer का इंस्टेंस बनाएं:
SwiftObjective-C
let textRecognizer = TextRecognizer.textRecognizer()
     
MLKTextRecognizer *textRecognizer = [MLKTextRecognizer textRecognizer];
     

2. इनपुट इमेज तैयार करें

TextRecognizer के process(_:completion:) तरीके में इमेज को UIImage या CMSampleBufferRef के तौर पर पास करें:

UIImage या CMSampleBuffer का इस्तेमाल करके, VisionImage ऑब्जेक्ट बनाएं.

अगर आप UIImage का इस्तेमाल करते हैं, तो यह तरीका अपनाएं:

  • UIImage के साथ एक VisionImage ऑब्जेक्ट बनाएं. .orientation का सही नाम डालना न भूलें.
    SwiftObjective-C
    let image = VisionImage(image: UIImage)
    visionImage
    .orientation = image.imageOrientation
    MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithImage:image];
    visionImage
    .orientation = image.imageOrientation;

अगर आप CMSampleBuffer का इस्तेमाल करते हैं, तो यह तरीका अपनाएं:

  • CMSampleBuffer में शामिल इमेज डेटा का ओरिएंटेशन बताएं.

    इमेज का ओरिएंटेशन पाने के लिए:

    SwiftObjective-C
    func imageOrientation(
      deviceOrientation
    : UIDeviceOrientation,
      cameraPosition
    : AVCaptureDevice.Position
    ) -> UIImage.Orientation {
     
    switch deviceOrientation {
     
    case .portrait:
       
    return cameraPosition == .front ? .leftMirrored : .right
     
    case .landscapeLeft:
       
    return cameraPosition == .front ? .downMirrored : .up
     
    case .portraitUpsideDown:
       
    return cameraPosition == .front ? .rightMirrored : .left
     
    case .landscapeRight:
       
    return cameraPosition == .front ? .upMirrored : .down
     
    case .faceDown, .faceUp, .unknown:
       
    return .up
     
    }
    }
         
    - (UIImageOrientation)
      imageOrientationFromDeviceOrientation
    :(UIDeviceOrientation)deviceOrientation
                             cameraPosition
    :(AVCaptureDevicePosition)cameraPosition {
     
    switch (deviceOrientation) {
       
    case UIDeviceOrientationPortrait:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationLeftMirrored
                                                               
    : UIImageOrientationRight;

       
    case UIDeviceOrientationLandscapeLeft:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationDownMirrored
                                                               
    : UIImageOrientationUp;
       
    case UIDeviceOrientationPortraitUpsideDown:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationRightMirrored
                                                               
    : UIImageOrientationLeft;
       
    case UIDeviceOrientationLandscapeRight:
         
    return cameraPosition == AVCaptureDevicePositionFront ? UIImageOrientationUpMirrored
                                                               
    : UIImageOrientationDown;
       
    case UIDeviceOrientationUnknown:
       
    case UIDeviceOrientationFaceUp:
       
    case UIDeviceOrientationFaceDown:
         
    return UIImageOrientationUp;
     
    }
    }
         
  • CMSampleBuffer ऑब्जेक्ट और ओरिएंटेशन का इस्तेमाल करके VisionImage ऑब्जेक्ट बनाएं:
    SwiftObjective-C
    let image = VisionImage(buffer: sampleBuffer)
    image
    .orientation = imageOrientation(
      deviceOrientation
    : UIDevice.current.orientation,
      cameraPosition
    : cameraPosition)
     MLKVisionImage *image = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
     image
    .orientation =
       
    [self imageOrientationFromDeviceOrientation:UIDevice.currentDevice.orientation
                                    cameraPosition
    :cameraPosition];

3. इमेज को प्रोसेस करें

इसके बाद, इमेज को process(_:completion:) वाले तरीके पर भेजें:

SwiftObjective-C
textRecognizer.process(visionImage) { result, error in
  guard error
== nil, let result = result else {
   
// Error handling
   
return
 
}
 
// Recognized text
}
[textRecognizer processImage:image
                  completion
:^(MLKText *_Nullable result,
                               
NSError *_Nullable error) {
 
if (error != nil || result == nil) {
   
// Error handling
   
return;
 
}
 
// Recognized text
}];

4. पहचाने गए टेक्स्ट के ब्लॉक से टेक्स्ट निकालें

टेक्स्ट की पहचान करने की कार्रवाई सफल होने पर, यह एक Text ऑब्जेक्ट दिखाता है. Text ऑब्जेक्ट में पूरा टेक्स्ट हो, जो इमेज में पहचाना गया हो और शून्य या ज़्यादा TextBlock ऑब्जेक्ट हो.

हर TextBlock टेक्स्ट के एक आयताकार ब्लॉक को दिखाता है, जिसमें शून्य या उससे ज़्यादा TextLine ऑब्जेक्ट शामिल होते हैं. हर TextLine ऑब्जेक्ट में शून्य या उससे ज़्यादा TextElement ऑब्जेक्ट होते हैं. इन ऑब्जेक्ट में तारीख और संख्या जैसी शब्द और शब्द जैसी इकाइयां शामिल होती हैं.

हर TextBlock, TextLine, और TextElement ऑब्जेक्ट के लिए, आपको इलाके में मौजूद टेक्स्ट और बाउंडिंग कोऑर्डिनेट के बारे में जानकारी मिल सकती है.

उदाहरण के लिए:

SwiftObjective-C
let resultText = result.text
for block in result.blocks {
    let blockText
= block.text
    let blockLanguages
= block.recognizedLanguages
    let blockCornerPoints
= block.cornerPoints
    let blockFrame
= block.frame
   
for line in block.lines {
        let lineText
= line.text
        let lineLanguages
= line.recognizedLanguages
        let lineCornerPoints
= line.cornerPoints
        let lineFrame
= line.frame
       
for element in line.elements {
            let elementText
= element.text
            let elementCornerPoints
= element.cornerPoints
            let elementFrame
= element.frame
       
}
   
}
}
NSString *resultText = result.text;
for (MLKTextBlock *block in result.blocks) {
 
NSString *blockText = block.text;
 
NSArray<MLKTextRecognizedLanguage *> *blockLanguages = block.recognizedLanguages;
 
NSArray<NSValue *> *blockCornerPoints = block.cornerPoints;
 
CGRect blockFrame = block.frame;
 
for (MLKTextLine *line in block.lines) {
   
NSString *lineText = line.text;
   
NSArray<MLKTextRecognizedLanguage *> *lineLanguages = line.recognizedLanguages;
   
NSArray<NSValue *> *lineCornerPoints = line.cornerPoints;
   
CGRect lineFrame = line.frame;
   
for (MLKTextElement *element in line.elements) {
     
NSString *elementText = element.text;
     
NSArray<NSValue *> *elementCornerPoints = element.cornerPoints;
     
CGRect elementFrame = element.frame;
   
}
 
}
}

इनपुट इमेज के दिशा-निर्देश

  • मशीन लर्निंग (एमएल किट) टेक्स्ट की सही पहचान कर सके, इसके लिए इनपुट इमेज में ऐसा टेक्स्ट होना चाहिए जिसे पिक्सल डेटा की मदद से दिखाया जाए. आम तौर पर, हर वर्ण कम से कम 16x16 पिक्सल का होना चाहिए. आम तौर पर, 24x24 पिक्सल से बड़े वर्णों वाले वर्णों के लिए सटीक होने का कोई फ़ायदा नहीं होता है.

    उदाहरण के लिए, 640x480 साइज़ वाली इमेज से, कारोबार का वह कार्ड स्कैन किया जा सकता है जो इमेज की पूरी चौड़ाई में दिखता है. अक्षरों वाले साइज़ के पेपर पर प्रिंट किए गए दस्तावेज़ को स्कैन करने के लिए, 720x1280 पिक्सल की इमेज की ज़रूरत हो सकती है.

  • इमेज फ़ोकस सही तरीके से काम नहीं कर रहा है. इसका असर टेक्स्ट पहचानने की सुविधा पर पड़ सकता है. अगर आपको अच्छे नतीजे नहीं मिल रहे हैं, तो उपयोगकर्ता से इमेज को फिर से देखने की कोशिश करें.

  • अगर रीयल-टाइम ऐप्लिकेशन में टेक्स्ट की पहचान की जा रही है, तो आपको इनपुट इमेज के सभी डाइमेंशन को ध्यान में रखना चाहिए. छोटी इमेज तेज़ी से प्रोसेस की जा सकती हैं. इंतज़ार का समय कम करने के लिए, पक्का करें कि टेक्स्ट की इमेज ज़रूरत के मुताबिक ज़्यादा से ज़्यादा जगह पर हो. साथ ही, यह भी ध्यान रखें कि ऊपर बताए गए रिज़ॉल्यूशन के हिसाब से ही इमेज का रिज़ॉल्यूशन कम हो. ज़्यादा जानकारी के लिए, परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह देखें.

परफ़ॉर्मेंस को बेहतर बनाने के लिए सलाह

  • वीडियो फ़्रेम प्रोसेस करने के लिए, डिटेक्टर के results(in:) सिंक्रोनस एपीआई का इस्तेमाल करें. दिए गए वीडियो फ़्रेम से सिंक्रोनस रूप से परिणाम पाने के लिए AVCaptureVideoDataOutputSampleBufferDelegate के captureOutput(_, didOutput:from:) फ़ंक्शन से इस विधि को कॉल करें. AVCaptureVideoDataOutput की alwaysDiscardsLateVideoFrames को true के तौर पर रखें, ताकि डिटेक्टर को कॉल थ्रॉटल किए जा सकें. अगर डिटेक्टर चलने के दौरान नया वीडियो फ़्रेम उपलब्ध हो जाता है, तो उसे छोड़ दिया जाएगा.
  • अगर आप इनपुट इमेज पर ग्राफ़िक ओवरले करने के लिए डिटेक्टर के आउटपुट का इस्तेमाल करते हैं, तो पहले ML किट से नतीजा पाएं. इसके बाद, इमेज और ओवरले को एक ही चरण में रेंडर करें. ऐसा करके, आप हर प्रोसेस किए गए इनपुट फ़्रेम के लिए डिसप्ले को सिर्फ़ एक बार रेंडर करते हैं. उदाहरण के लिए, ML किट क्विकस्टार्ट सैंपल में updatepreviewOverlayViewWithLastFrame देखें.
  • कम रिज़ॉल्यूशन में इमेज कैप्चर करें. हालांकि, इस एपीआई के इमेज डाइमेंशन की ज़रूरी शर्तों को भी ध्यान में रखें.