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