การจดจำหมึกดิจิทัลของ ML Kit ช่วยให้คุณจดจำข้อความที่เขียนด้วยลายมือบนพื้นผิวดิจิทัลได้หลายร้อยภาษา รวมถึงจัดประเภทภาพสเก็ตช์ได้ด้วย
ลองเลย
- ลองใช้แอปตัวอย่างเพื่อดูตัวอย่างการใช้งาน API นี้
ก่อนเริ่มต้น
รวมไลบรารี ML Kit ต่อไปนี้ไว้ใน Podfile
pod 'GoogleMLKit/DigitalInkRecognition', '8.0.0'หลังจากติดตั้งหรืออัปเดต Pods ของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้
.xcworkspaceML 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 ซึ่งควรปรับเปลี่ยนตามความจำเป็นสำหรับแอปพลิเคชันของคุณ เราขอแนะนำให้ใช้ Roundcap เมื่อวาดส่วนของเส้นเพื่อให้ส่วนที่มีความยาวเป็นศูนย์วาดเป็นจุด (นึกถึงจุดบนตัวอักษร i ตัวพิมพ์เล็ก) ระบบจะเรียกใช้ฟังก์ชัน doRecognition() หลังจากเขียนแต่ละเส้นเสร็จ และจะกำหนดไว้ด้านล่าง
รับอินสแตนซ์ของ DigitalInkRecognizer
ในการจดจำ เราต้องส่งออบเจ็กต์ Ink ไปยังอินสแตนซ์ DigitalInkRecognizer หากต้องการรับอินสแตนซ์ DigitalInkRecognizer
ก่อนอื่นเราต้องดาวน์โหลดโมเดลเครื่องมือจดจำสำหรับภาษาที่ต้องการ และ
โหลดโมเดลลงใน RAM ซึ่งทำได้โดยใช้ข้อมูลโค้ดต่อไปนี้ ซึ่งวางไว้ในเมธอด 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."); }]; }
เคล็ดลับในการปรับปรุงความแม่นยำของการจดจำข้อความ
ความแม่นยำของการจดจำข้อความอาจแตกต่างกันไปในแต่ละภาษา ความแม่นยำยังขึ้นอยู่กับรูปแบบการเขียนด้วย แม้ว่า Digital Ink Recognition จะได้รับการฝึกให้จัดการกับรูปแบบการเขียนหลายประเภท แต่ผลลัพธ์อาจแตกต่างกันไปในผู้ใช้แต่ละราย
ต่อไปนี้เป็นวิธีปรับปรุงความแม่นยำของเครื่องมือจดจำข้อความ โปรดทราบว่าเทคนิคเหล่านี้ใช้ไม่ได้กับตัวแยกประเภทการวาดสำหรับอีโมจิ, AutoDraw และรูปร่าง
พื้นที่เขียน
แอปพลิเคชันจำนวนมากมีพื้นที่การเขียนที่กำหนดไว้อย่างชัดเจนสำหรับการป้อนข้อมูลของผู้ใช้ ความหมายของสัญลักษณ์จะขึ้นอยู่กับขนาดของสัญลักษณ์นั้นเมื่อเทียบกับขนาดของพื้นที่เขียนที่มีสัญลักษณ์นั้นอยู่ เช่น ความแตกต่างระหว่างตัวอักษร "o" หรือ "c" ตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่ และเครื่องหมายคอมมากับเครื่องหมายทับ
การบอกความกว้างและความสูงของพื้นที่เขียนให้โปรแกรมจดจำทราบจะช่วยเพิ่มความแม่นยำได้ อย่างไรก็ตาม เครื่องมือจดจำจะถือว่าพื้นที่เขียนมีข้อความเพียงบรรทัดเดียว หากพื้นที่การเขียนจริงมีขนาดใหญ่พอที่จะให้ผู้ใช้เขียนได้ 2 บรรทัดขึ้นไป คุณอาจได้ผลลัพธ์ที่ดีกว่าโดยการส่ง WritingArea ที่มีความสูงซึ่งเป็นค่าประมาณที่ดีที่สุดของความสูงของข้อความบรรทัดเดียว ออบเจ็กต์ WritingArea ที่คุณส่งไปยังเครื่องมือจดจำไม่จำเป็นต้องตรงกับพื้นที่การเขียนจริงบนหน้าจอทุกประการ การเปลี่ยนความสูงของ WritingArea ด้วยวิธีนี้ใช้ได้ดีในบางภาษามากกว่าภาษาอื่นๆ
เมื่อระบุพื้นที่เขียน ให้ระบุความกว้างและความสูงในหน่วยเดียวกับพิกัดเส้นขีด อาร์กิวเมนต์พิกัด x,y ไม่จำเป็นต้องมีหน่วย เนื่องจาก API จะปรับหน่วยทั้งหมดให้เป็นมาตรฐาน ดังนั้นสิ่งสำคัญเพียงอย่างเดียวคือขนาดและตำแหน่งสัมพัทธ์ของเส้นวาด คุณสามารถส่งพิกัดในสเกลใดก็ได้ที่เหมาะสมกับระบบของคุณ
บริบทก่อนหน้า
บริบทก่อนหน้าคือข้อความที่อยู่ก่อนหน้าเส้นขีดใน Ink ที่คุณพยายามจดจำโดยตรง คุณสามารถช่วยเครื่องมือจดจำได้โดยบอกเกี่ยวกับบริบทก่อนหน้า
ตัวอย่างเช่น ตัวอักษรเขียน "n" และ "u" มักจะถูกเข้าใจผิดว่าเป็นตัวเดียวกัน หากผู้ใช้ป้อนคำบางส่วนว่า "arg" แล้ว ผู้ใช้อาจเขียนต่อด้วยเส้นที่ระบบจดจำได้ว่าเป็น "ument" หรือ "nment" การระบุบริบทก่อนหน้า "arg" จะช่วยแก้ความกำกวมได้ เนื่องจากคำว่า "argument" มีแนวโน้มที่จะถูกต้องมากกว่า "argnment"
บริบทก่อนหน้ายังช่วยให้โปรแกรมจดจำระบุการเว้นวรรคระหว่างคำได้ด้วย คุณสามารถพิมพ์อักขระเว้นวรรคได้ แต่จะวาดไม่ได้ แล้วเครื่องมือจดจำจะระบุได้อย่างไรว่าคำหนึ่งสิ้นสุดลงและคำถัดไปเริ่มต้นขึ้นเมื่อใด หากผู้ใช้เขียนคำว่า "hello" ไปแล้วและเขียนคำว่า "world" ต่อ โดยไม่มีบริบทก่อนหน้า เครื่องมือจดจำจะแสดงผลสตริง "world" อย่างไรก็ตาม หากคุณระบุบริบทก่อนหน้าเป็น "hello" โมเดลจะแสดงผลสตริง " world" โดยมีเว้นวรรคนำหน้า เนื่องจาก "hello world" มีความหมายมากกว่า "helloword"
คุณควรระบุสตริงบริบทก่อนหน้าที่ยาวที่สุดเท่าที่จะเป็นไปได้ โดยมีความยาวไม่เกิน 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ออกและแทนที่ด้วยคำอื่น การแก้ไขอาจอยู่ตรงกลางประโยค แต่เส้นขีดสำหรับการแก้ไขจะอยู่ที่ส่วนท้ายของลำดับเส้นขีด
ในกรณีนี้ เราขอแนะนำให้ส่งคำที่เขียนใหม่ไปยัง API แยกต่างหาก และรวมผลลัพธ์กับการจดจำก่อนหน้าโดยใช้ตรรกะของคุณเอง
การจัดการกับรูปร่างที่กำกวม
มีบางกรณีที่ความหมายของรูปร่างที่ระบุให้กับเครื่องมือจดจำมีความกำกวม เช่น สี่เหลี่ยมผืนผ้าที่มีขอบมนมากอาจมองเป็นสี่เหลี่ยมผืนผ้าหรือวงรีก็ได้
กรณีที่ไม่ชัดเจนเหล่านี้สามารถจัดการได้โดยใช้คะแนนการจดจำเมื่อมีให้บริการ เฉพาะตัวแยกประเภทรูปร่างเท่านั้นที่ให้คะแนน หากโมเดลมีความมั่นใจมาก คะแนนของผลลัพธ์อันดับแรกจะดีกว่าผลลัพธ์อันดับ 2 มาก หากมีความไม่แน่นอน คะแนนของผลลัพธ์ 2 อันดับแรกจะใกล้เคียงกัน นอกจากนี้ โปรดทราบว่าตัวแยกประเภทรูปร่างจะตีความ Ink ทั้งหมดเป็นรูปร่างเดียว ตัวอย่างเช่น หาก Ink มีสี่เหลี่ยมผืนผ้าและวงรีอยู่ติดกัน เครื่องมือจดจำอาจแสดงผลเป็นรูปใดรูปหนึ่ง (หรือรูปอื่นที่แตกต่างไปโดยสิ้นเชิง) เนื่องจากตัวเลือกการจดจำรายการเดียวไม่สามารถแสดงรูปร่าง 2 รูปได้