ML Kit की डिजिटल इंक की पहचान करने की सुविधा की मदद से, डिजिटल प्लैटफ़ॉर्म पर हाथ से लिखे गए टेक्स्ट को सैकड़ों भाषाओं में पहचाना जा सकता है. साथ ही, स्केच को भी कैटगरी में बांटा जा सकता है.
इसे आज़माएं
- इस एपीआई के इस्तेमाल का उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन का इस्तेमाल करें.
शुरू करने से पहले
अपने Podfile में, ML Kit की ये लाइब्रेरी शामिल करें:
pod 'GoogleMLKit/DigitalInkRecognition', '8.0.0'अपने प्रोजेक्ट के पॉड इंस्टॉल या अपडेट करने के बाद, अपने Xcode प्रोजेक्ट को
.xcworkspaceका इस्तेमाल करके खोलें. ML Kit, Xcode के 13.2.1 या इसके बाद के वर्शन पर काम करता है.
अब Ink ऑब्जेक्ट में मौजूद टेक्स्ट को पहचाना जा सकता है.
Ink ऑब्जेक्ट बनाना
Ink ऑब्जेक्ट बनाने का मुख्य तरीका, उसे टच स्क्रीन पर ड्रा करना है. iOS पर,
आप UIImageView का इस्तेमाल कर सकते हैं. इसके साथ,
टच इवेंट
हैंडलर
का इस्तेमाल करके, स्क्रीन पर स्ट्रोक ड्रा किए जा सकते हैं. साथ ही, स्ट्रोक के पॉइंट भी सेव किए जा सकते हैं, ताकि Ink ऑब्जेक्ट बनाया जा सके. यहां दिए गए कोड स्निपेट में, इस सामान्य पैटर्न को दिखाया गया है. ज़्यादा जानकारी के लिए, क्विकस्टार्ट
ऐप्लिकेशन देखें. इसमें टच इवेंट हैंडलिंग, स्क्रीन ड्रॉइंग,
और स्ट्रोक डेटा मैनेजमेंट को अलग-अलग दिखाया गया है.
Swift
@IBOutlet weak var mainImageView: UIImageView! var kMillisecondsPerTimeInterval = 1000.0 var lastPoint = CGPoint.zero private var strokes: [Stroke] = [] private var points: [StrokePoint] = [] func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) { UIGraphicsBeginImageContext(view.frame.size) guard let context = UIGraphicsGetCurrentContext() else { return } mainImageView.image?.draw(in: view.bounds) context.move(to: fromPoint) context.addLine(to: toPoint) context.setLineCap(.round) context.setBlendMode(.normal) context.setLineWidth(10.0) context.setStrokeColor(UIColor.white.cgColor) context.strokePath() mainImageView.image = UIGraphicsGetImageFromCurrentImageContext() mainImageView.alpha = 1.0 UIGraphicsEndImageContext() } override func touchesBegan(_ touches: Set, with event: UIEvent?) { guard let touch = touches.first else { return } lastPoint = touch.location(in: mainImageView) let t = touch.timestamp points = [StrokePoint.init(x: Float(lastPoint.x), y: Float(lastPoint.y), t: Int(t * kMillisecondsPerTimeInterval))] drawLine(from:lastPoint, to:lastPoint) } override func touchesMoved(_ touches: Set , with event: UIEvent?) { guard let touch = touches.first else { return } let currentPoint = touch.location(in: mainImageView) let t = touch.timestamp points.append(StrokePoint.init(x: Float(currentPoint.x), y: Float(currentPoint.y), t: Int(t * kMillisecondsPerTimeInterval))) drawLine(from: lastPoint, to: currentPoint) lastPoint = currentPoint } override func touchesEnded(_ touches: Set , with event: UIEvent?) { guard let touch = touches.first else { return } let currentPoint = touch.location(in: mainImageView) let t = touch.timestamp points.append(StrokePoint.init(x: Float(currentPoint.x), y: Float(currentPoint.y), t: Int(t * kMillisecondsPerTimeInterval))) drawLine(from: lastPoint, to: currentPoint) lastPoint = currentPoint strokes.append(Stroke.init(points: points)) self.points = [] doRecognition() }
Objective-C
// Interface @property (weak, nonatomic) IBOutlet UIImageView *mainImageView; @property(nonatomic) CGPoint lastPoint; @property(nonatomic) NSMutableArray*strokes; @property(nonatomic) NSMutableArray *points; // Implementations static const double kMillisecondsPerTimeInterval = 1000.0; - (void)drawLineFrom:(CGPoint)fromPoint to:(CGPoint)toPoint { UIGraphicsBeginImageContext(self.mainImageView.frame.size); [self.mainImageView.image drawInRect:CGRectMake(0, 0, self.mainImageView.frame.size.width, self.mainImageView.frame.size.height)]; CGContextMoveToPoint(UIGraphicsGetCurrentContext(), fromPoint.x, fromPoint.y); CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), toPoint.x, toPoint.y); CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0); CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1, 1, 1, 1); CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal); CGContextStrokePath(UIGraphicsGetCurrentContext()); CGContextFlush(UIGraphicsGetCurrentContext()); self.mainImageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } - (void)touchesBegan:(NSSet *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; self.lastPoint = [touch locationInView:self.mainImageView]; NSTimeInterval time = [touch timestamp]; self.points = [NSMutableArray array]; [self.points addObject:[[MLKStrokePoint alloc] initWithX:self.lastPoint.x y:self.lastPoint.y t:time * kMillisecondsPerTimeInterval]]; [self drawLineFrom:self.lastPoint to:self.lastPoint]; } - (void)touchesMoved:(NSSet *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.mainImageView]; NSTimeInterval time = [touch timestamp]; [self.points addObject:[[MLKStrokePoint alloc] initWithX:currentPoint.x y:currentPoint.y t:time * kMillisecondsPerTimeInterval]]; [self drawLineFrom:self.lastPoint to:currentPoint]; self.lastPoint = currentPoint; } - (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint currentPoint = [touch locationInView:self.mainImageView]; NSTimeInterval time = [touch timestamp]; [self.points addObject:[[MLKStrokePoint alloc] initWithX:currentPoint.x y:currentPoint.y t:time * kMillisecondsPerTimeInterval]]; [self drawLineFrom:self.lastPoint to:currentPoint]; self.lastPoint = currentPoint; if (self.strokes == nil) { self.strokes = [NSMutableArray array]; } [self.strokes addObject:[[MLKStroke alloc] initWithPoints:self.points]]; self.points = nil; [self doRecognition]; }
ध्यान दें कि कोड स्निपेट में,
UIImageView में स्ट्रोक ड्रा करने के लिए, एक सैंपल फ़ंक्शन शामिल है.
इसे अपने ऐप्लिकेशन के हिसाब से बदला जा सकता है. हमारा सुझाव है कि लाइन सेगमेंट ड्रा करते समय, राउंडकैप का इस्तेमाल करें, ताकि शून्य लंबाई वाले सेगमेंट को डॉट के तौर पर ड्रा किया जा सके. जैसे, लोअरकेस में "i" के ऊपर मौजूद डॉट. doRecognition() फ़ंक्शन, हर स्ट्रोक के लिखे जाने के बाद कॉल किया जाता है. इसके बारे में यहां बताया गया है.
DigitalInkRecognizer का इंस्टेंस पाना
पहचान करने के लिए, हमें Ink ऑब्जेक्ट को
DigitalInkRecognizer इंस्टेंस में पास करना होगा. DigitalInkRecognizer इंस्टेंस पाने के लिए, हमें सबसे पहले अपनी पसंद की भाषा के लिए, पहचान करने वाले मॉडल को डाउनलोड करना होगा. इसके बाद, मॉडल को रैम में लोड करना होगा. ऐसा करने के लिए, यहां दिया गया कोड स्निपेट इस्तेमाल किया जा सकता है. इसे आसान बनाने के लिए, viewDidLoad() तरीके में रखा गया है. साथ ही, इसमें हार्डकोड की गई भाषा का नाम इस्तेमाल किया गया है. उपयोगकर्ता को उपलब्ध भाषाओं की सूची दिखाने और चुनी गई भाषा को डाउनलोड करने का तरीका जानने के लिए, क्विकस्टार्ट
ऐप्लिकेशन देखें.
Swift
override func viewDidLoad() { super.viewDidLoad() let languageTag = "en-US" let identifier = DigitalInkRecognitionModelIdentifier(forLanguageTag: languageTag) if identifier == nil { // no model was found or the language tag couldn't be parsed, handle error. } let model = DigitalInkRecognitionModel.init(modelIdentifier: identifier!) let modelManager = ModelManager.modelManager() let conditions = ModelDownloadConditions.init(allowsCellularAccess: true, allowsBackgroundDownloading: true) modelManager.download(model, conditions: conditions) // Get a recognizer for the language let options: DigitalInkRecognizerOptions = DigitalInkRecognizerOptions.init(model: model) recognizer = DigitalInkRecognizer.digitalInkRecognizer(options: options) }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; NSString *languagetag = @"en-US"; MLKDigitalInkRecognitionModelIdentifier *identifier = [MLKDigitalInkRecognitionModelIdentifier modelIdentifierForLanguageTag:languagetag]; if (identifier == nil) { // no model was found or the language tag couldn't be parsed, handle error. } MLKDigitalInkRecognitionModel *model = [[MLKDigitalInkRecognitionModel alloc] initWithModelIdentifier:identifier]; MLKModelManager *modelManager = [MLKModelManager modelManager]; [modelManager downloadModel:model conditions:[[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES allowsBackgroundDownloading:YES]]; MLKDigitalInkRecognizerOptions *options = [[MLKDigitalInkRecognizerOptions alloc] initWithModel:model]; self.recognizer = [MLKDigitalInkRecognizer digitalInkRecognizerWithOptions:options]; }
क्विकस्टार्ट ऐप्लिकेशन में, अतिरिक्त कोड शामिल होता है. इससे यह पता चलता है कि एक साथ कई डाउनलोड को कैसे मैनेज किया जाए. साथ ही, पूरा होने की सूचनाओं को मैनेज करके, यह कैसे पता लगाया जाए कि कौनसे डाउनलोड पूरे हुए हैं.
Ink ऑब्जेक्ट की पहचान करना
अब हम doRecognition() फ़ंक्शन पर आते हैं. इसे आसान बनाने के लिए, touchesEnded() से कॉल किया जाता है. अन्य ऐप्लिकेशन में, टाइम आउट होने के बाद या उपयोगकर्ता के किसी बटन को दबाकर पहचान करने की सुविधा को ट्रिगर करने पर ही, पहचान करने की सुविधा को चालू किया जा सकता है.
Swift
func doRecognition() { let ink = Ink.init(strokes: strokes) recognizer.recognize( ink: ink, completion: { [unowned self] (result: DigitalInkRecognitionResult?, error: Error?) in var alertTitle = "" var alertText = "" if let result = result, let candidate = result.candidates.first { alertTitle = "I recognized this:" alertText = candidate.text } else { alertTitle = "I hit an error:" alertText = error!.localizedDescription } let alert = UIAlertController(title: alertTitle, message: alertText, preferredStyle: UIAlertController.Style.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)) self.present(alert, animated: true, completion: nil) } ) }
Objective-C
- (void)doRecognition { MLKInk *ink = [[MLKInk alloc] initWithStrokes:self.strokes]; __weak typeof(self) weakSelf = self; [self.recognizer recognizeInk:ink completion:^(MLKDigitalInkRecognitionResult *_Nullable result, NSError *_Nullable error) { typeof(weakSelf) strongSelf = weakSelf; if (strongSelf == nil) { return; } NSString *alertTitle = nil; NSString *alertText = nil; if (result.candidates.count > 0) { alertTitle = @"I recognized this:"; alertText = result.candidates[0].text; } else { alertTitle = @"I hit an error:"; alertText = [error localizedDescription]; } UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle message:alertText preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; [strongSelf presentViewController:alert animated:YES completion:nil]; }]; }
मॉडल डाउनलोड को मैनेज करना
हम पहले ही देख चुके हैं कि पहचान करने वाला मॉडल कैसे डाउनलोड किया जाता है. यहां दिए गए कोड स्निपेट से, यह पता चलता है कि कोई मॉडल पहले से डाउनलोड किया गया है या नहीं. साथ ही, स्टोरेज की जगह खाली करने के लिए, किसी मॉडल को कैसे मिटाया जाए.
यह देखना कि कोई मॉडल पहले से डाउनलोड किया गया है या नहीं
Swift
let model : DigitalInkRecognitionModel = ... let modelManager = ModelManager.modelManager() modelManager.isModelDownloaded(model)
Objective-C
MLKDigitalInkRecognitionModel *model = ...; MLKModelManager *modelManager = [MLKModelManager modelManager]; [modelManager isModelDownloaded:model];
डाउनलोड किए गए मॉडल को मिटाना
Swift
let model : DigitalInkRecognitionModel = ... let modelManager = ModelManager.modelManager() if modelManager.isModelDownloaded(model) { modelManager.deleteDownloadedModel( model!, completion: { error in if error != nil { // Handle error return } NSLog(@"Model deleted."); }) }
Objective-C
MLKDigitalInkRecognitionModel *model = ...; MLKModelManager *modelManager = [MLKModelManager modelManager]; if ([self.modelManager isModelDownloaded:model]) { [self.modelManager deleteDownloadedModel:model completion:^(NSError *_Nullable error) { if (error) { // Handle error. return; } NSLog(@"Model deleted."); }]; }
टेक्स्ट की पहचान करने की सुविधा को ज़्यादा सटीक बनाने के लिए सलाह
टेक्स्ट की पहचान करने की सुविधा की सटीकता, अलग-अलग भाषाओं के हिसाब से अलग-अलग हो सकती है. सटीकता, लिखने की स्टाइल पर भी निर्भर करती है. डिजिटल इंक की पहचान करने की सुविधा को, लिखने की कई स्टाइल को हैंडल करने के लिए ट्रेन किया गया है. हालांकि, नतीजे हर उपयोगकर्ता के हिसाब से अलग-अलग हो सकते हैं.
टेक्स्ट की पहचान करने की सुविधा की सटीकता को बेहतर बनाने के कुछ तरीके यहां दिए गए हैं. ध्यान दें कि ये तकनीकें, इमोजी, ऑटोड्रॉ, और आकार के लिए, ड्रॉइंग क्लासिफ़ायर पर लागू नहीं होती हैं.
लिखने की जगह
कई ऐप्लिकेशन में, उपयोगकर्ता के इनपुट के लिए, लिखने की जगह तय होती है. किसी सिंबल का मतलब, आंशिक तौर पर उस जगह के साइज़ के हिसाब से तय होता है जहां वह लिखा गया है. उदाहरण के लिए, लोअर या अपर केस में "o" या "c" के बीच का अंतर. साथ ही, कॉमा और फ़ॉरवर्ड स्लैश के बीच का अंतर.
पहचान करने की सुविधा को, लिखने की जगह की चौड़ाई और लंबाई बताने से, सटीकता बेहतर हो सकती है. हालांकि, पहचान करने की सुविधा यह मानती है कि लिखने की जगह में, टेक्स्ट की सिर्फ़ एक लाइन होती है. अगर लिखने की जगह इतनी बड़ी है कि उपयोगकर्ता दो या उससे ज़्यादा लाइनें लिख सकता है, तो WritingArea को पास करके बेहतर नतीजे मिल सकते हैं. इसमें, टेक्स्ट की एक लाइन की लंबाई का अनुमान लगाया जाता है. पहचान करने की सुविधा को पास किया गया WritingArea ऑब्जेक्ट, स्क्रीन पर मौजूद लिखने की जगह से पूरी तरह मेल नहीं खाता है. इस तरह से WritingArea की लंबाई में बदलाव करने से, कुछ भाषाओं में बेहतर नतीजे मिलते हैं.
लिखने की जगह तय करते समय, उसकी चौड़ाई और लंबाई को स्ट्रोक के कोऑर्डिनेट वाली इकाइयों में ही तय करें. x,y कोऑर्डिनेट वाले आर्ग्युमेंट के लिए, किसी इकाई की ज़रूरत नहीं होती. एपीआई सभी इकाइयों को सामान्य करता है. इसलिए, सिर्फ़ स्ट्रोक का साइज़ और पोज़िशन मायने रखती है. आपके पास कोऑर्डिनेट को किसी भी स्केल में पास करने का विकल्प होता है.
प्री-कॉन्टेक्स्ट
प्री-कॉन्टेक्स्ट वह टेक्स्ट होता है जो पहचाने जाने वाले Ink में स्ट्रोक से ठीक पहले लिखा जाता है. पहचान करने की सुविधा को, प्री-कॉन्टेक्स्ट के बारे में बताकर मदद की जा सकती है.
उदाहरण के लिए, कर्सिव में लिखे गए "n" और "u" को अक्सर एक जैसा समझ लिया जाता है. अगर उपयोगकर्ता ने "arg" शब्द का हिस्सा पहले ही डाल दिया है, तो वह ऐसे स्ट्रोक डाल सकता है जिन्हें "ument" या "nment" के तौर पर पहचाना जा सकता है. प्री-कॉन्टेक्स्ट "arg" तय करने से, इस समस्या को हल किया जा सकता है, क्योंकि "argnment" के मुकाबले "argument" शब्द ज़्यादा सही है.
प्री-कॉन्टेक्स्ट की मदद से, पहचान करने की सुविधा, शब्दों के बीच मौजूद स्पेस को भी पहचान सकती है. स्पेस कैरेक्टर टाइप किया जा सकता है, लेकिन उसे ड्रा नहीं किया जा सकता. ऐसे में, पहचान करने की सुविधा यह कैसे तय कर सकती है कि कोई शब्द कब खत्म होता है और दूसरा कब शुरू होता है? अगर उपयोगकर्ता ने "hello" पहले ही लिख दिया है और "world" शब्द लिखता है, तो प्री-कॉन्टेक्स्ट के बिना, पहचान करने की सुविधा "world" स्ट्रिंग दिखाती है. हालांकि, अगर "hello" प्री-कॉन्टेक्स्ट तय किया जाता है, तो मॉडल " world" स्ट्रिंग दिखाएगा. इसमें, "helloword" के मुकाबले "hello world" ज़्यादा सही है.
आपको ज़्यादा से ज़्यादा 20 वर्णों वाली प्री-कॉन्टेक्स्ट स्ट्रिंग देनी चाहिए. इसमें स्पेस भी शामिल हैं. अगर स्ट्रिंग लंबी है, तो पहचान करने की सुविधा सिर्फ़ आखिरी 20 वर्णों का इस्तेमाल करती है.
यहां दिए गए कोड सैंपल में, लिखने की जगह तय करने और प्री-कॉन्टेक्स्ट तय करने के लिए, RecognitionContext ऑब्जेक्ट का इस्तेमाल करने का तरीका बताया गया है.
Swift
let ink: Ink = ...; let recognizer: DigitalInkRecognizer = ...; let preContext: String = ...; let writingArea = WritingArea.init(width: ..., height: ...); let context: DigitalInkRecognitionContext.init( preContext: preContext, writingArea: writingArea); recognizer.recognizeHandwriting( from: ink, context: context, completion: { (result: DigitalInkRecognitionResult?, error: Error?) in if let result = result, let candidate = result.candidates.first { NSLog("Recognized \(candidate.text)") } else { NSLog("Recognition error \(error)") } })
Objective-C
MLKInk *ink = ...; MLKDigitalInkRecognizer *recognizer = ...; NSString *preContext = ...; MLKWritingArea *writingArea = [MLKWritingArea initWithWidth:... height:...]; MLKDigitalInkRecognitionContext *context = [MLKDigitalInkRecognitionContext initWithPreContext:preContext writingArea:writingArea]; [recognizer recognizeHandwritingFromInk:ink context:context completion:^(MLKDigitalInkRecognitionResult *_Nullable result, NSError *_Nullable error) { NSLog(@"Recognition result %@", result.candidates[0].text); }];
स्ट्रोक का ऑर्डर
स्ट्रोक के ऑर्डर के हिसाब से, पहचान करने की सुविधा की सटीकता अलग-अलग हो सकती है. पहचान करने की सुविधा, स्ट्रोक को उस क्रम में पहचानती है जिस क्रम में लोग आम तौर पर लिखते हैं. जैसे, अंग्रेज़ी के लिए बाएं से दाएं. अगर इस पैटर्न से अलग कोई क्रम इस्तेमाल किया जाता है, तो नतीजे कम सटीक होते हैं. जैसे, अंग्रेज़ी का कोई वाक्य आखिरी शब्द से शुरू करना.
एक और उदाहरण तब मिलता है, जब Ink के बीच में मौजूद किसी शब्द को हटाकर उसकी जगह कोई दूसरा शब्द लिखा जाता है. संशोधन शायद वाक्य के बीच में होता है, लेकिन संशोधन के लिए स्ट्रोक, स्ट्रोक सीक्वेंस के आखिर में होते हैं.
ऐसे में, हमारा सुझाव है कि एपीआई को नया लिखा गया शब्द अलग से भेजा जाए. साथ ही, अपने लॉजिक का इस्तेमाल करके, नतीजे को पहले की पहचान के साथ मर्ज किया जाए.
अस्पष्ट आकार से निपटना
ऐसे मामले होते हैं जिनमें पहचान करने की सुविधा को दिया गया आकार अस्पष्ट होता है. उदाहरण के लिए, बहुत गोल किनारों वाले आयत को आयत या एलिप्स के तौर पर देखा जा सकता है.
ऐसे मामलों को, पहचान के स्कोर का इस्तेमाल करके हैंडल किया जा सकता है. हालांकि, ये स्कोर सिर्फ़ आकार के क्लासिफ़ायर देते हैं. सिर्फ़ आकार के क्लासिफ़ायर स्कोर देते हैं. अगर मॉडल को पूरा भरोसा है, तो सबसे अच्छे नतीजे का स्कोर, दूसरे सबसे अच्छे नतीजे के स्कोर से काफ़ी बेहतर होगा. अगर अनिश्चितता है, तो पहले दो नतीजों के स्कोर आस-पास होंगे. इसके अलावा, ध्यान रखें कि आकार के क्लासिफ़ायर, पूरे Ink को एक आकार के तौर पर समझते हैं. उदाहरण के लिए, अगर Ink में एक आयत और एक एलिप्स एक-दूसरे के बगल में हैं, तो पहचान करने की सुविधा, नतीजे के तौर पर इनमें से कोई एक या पूरी तरह से अलग आकार दिखा सकती है. ऐसा इसलिए, क्योंकि पहचान का एक उम्मीदवार, दो आकार नहीं दिखा सकता.