ML Kit की डिजिटल इंक पहचान के साथ, सैकड़ों भाषाओं में डिजिटल प्लैटफ़ॉर्म पर हाथ से लिखे हुए टेक्स्ट को पहचान सकते हैं. साथ ही, स्केच की कैटगरी तय कर सकते हैं.
इसे आज़माएं
- इस एपीआई के इस्तेमाल के उदाहरण देखने के लिए, सैंपल ऐप्लिकेशन का इस्तेमाल करें.
शुरू करने से पहले
अपने Podfile में इन एमएल किट लाइब्रेरी को शामिल करें:
pod 'GoogleMLKit/DigitalInkRecognition', '3.2.0'
अपने प्रोजेक्ट के पॉड इंस्टॉल या अपडेट करने के बाद,
.xcworkspace
का इस्तेमाल करके अपना Xcode प्रोजेक्ट खोलें. एमएल किट, 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" के बीच कॉमा और फ़ॉरवर्ड स्लैश के बीच का अंतर.
पहचानकर्ता को लेखन क्षेत्र की चौड़ाई और ऊंचाई बताने से वह ज़्यादा सटीक हो सकता है. लेकिन, लेकिन पहचानकर्ता यह मानता है कि लेखन क्षेत्र में टेक्स्ट की केवल एक पंक्ति है. अगर फ़िज़िकल राइटिंग एरिया इतना बड़ा है कि उपयोगकर्ता को दो या दो से ज़्यादा लाइनें लिखने की अनुमति मिलती है, तो लेखन क्षेत्र में पास की एक ऊंचाई पास करके आपको बेहतर नतीजे मिल सकते हैं. यह एक ही लाइन का टेक्स्ट है. पहचानकर्ता को पास किए जाने वाले Writearea ऑब्जेक्ट को, स्क्रीन पर लेख के बिल्कुल सही हिस्से के जैसा नहीं होना चाहिए. इस तरह से लेखन क्षेत्र की ऊंचाई बदलने से कुछ भाषाओं का अनुभव अन्य भाषाओं के मुकाबले बेहतर होता है.
जब आप लेखन क्षेत्र को तय करते हैं, तो इसकी चौड़ाई और ऊंचाई को स्ट्रोक निर्देशांकों के समान इकाइयों में बताएं. x,y कोऑर्डिनेट आर्ग्युमेंट के लिए किसी यूनिट की ज़रूरत नहीं होती. एपीआई, सभी यूनिट को नॉर्मलाइज़ करता है. इस वजह से, स्ट्रोक का रिलेटिव साइज़ और पोज़िशन मायने रखती है. आप अपने सिस्टम के मुताबिक, किसी भी पैमाने पर निर्देशांकों को पास करने के लिए स्वतंत्र हैं.
कॉन्टेक्स्ट से पहले
प्री-कॉन्टेक्स्ट वह टेक्स्ट है जिसे Ink
में, स्ट्रोक के ठीक पहले लिखा जाता है. इवेंट से पहले, कॉन्टेक्स्ट पहचानने के लिए आइडेंटिफ़ायर की मदद करें.
उदाहरण के लिए, अक्सर "n" और "u" वाले कर्सिव अक्षरों को, गलती से एक-दूसरे के तौर पर समझ लिया जाता है. अगर उपयोगकर्ता ने पहले ही कुछ हद तक "आर्ग्युमेंट" शब्द डाल दिया है, तो हो सकता है कि उनके पास स्ट्रोक हो. इसकी पहचान, "न्यूमेंट" या "नेम" के तौर पर हो सकती है. "आर्ग्युमेंट" शब्द का इस्तेमाल करने से, "आर्ग्युमेंट" शब्द का इस्तेमाल होने पर, "तर्क" शब्द की दुविधा खत्म हो जाती है.
पहले से कॉन्टेक्स्ट से, शब्दों के बीच की खाली जगह का पता लगाने में भी मदद मिलती है. आपके पास स्पेस का वर्ण लिखने का विकल्प है. हालांकि, आपके पास एक वर्ण लिखने का विकल्प नहीं है. ऐसे में, कोई आइडेंटिफ़ायर कैसे तय कर सकता है कि एक शब्द कब खत्म होगा और अगला शब्द कब शुरू होगा? अगर उपयोगकर्ता ने पहले ही "हैलो" लिखा है और "बताए गए" शब्द के साथ जारी रखा है, तो कॉन्टेक्स्ट के बिना, आइडेंटिफ़ायर "दुनिया" स्ट्रिंग दिखाता है. हालांकि, अगर आपने प्री-कॉन्टेक्स्ट "हैलो" तय किया है, तो मॉडल "वर्ल्ड" स्ट्रिंग दिखाएगा, क्योंकि इसे इस्तेमाल करने में ज़्यादा स्पेस होगा, क्योंकि "हैलो की दुनिया" ज़्यादा सही नहीं है.
आपको, कॉन्टेक्स्ट के अलावा, स्पेस के साथ 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
में एक आयत और एक-दूसरे के बगल में एलिप्स है, तो आइडेंटिफ़ायर की वजह से एक या दूसरे (या पूरी तरह से अलग)
दिख सकता है. ऐसा इसलिए होगा, क्योंकि एक बार में एक ही पहचान रखने वाला उम्मीदवार दो आकार नहीं दिखा सकता.