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