การจดจำหมึกดิจิทัลของ ML Kit ช่วยให้คุณจดจำข้อความที่เขียนด้วยลายมือบนพื้นผิวดิจิทัลในหลายร้อยภาษา ทั้งยังจำแนกประเภทของภาพสเก็ตช์ได้ด้วย
ลองเลย
- ลองใช้แอปตัวอย่างเพื่อดูตัวอย่างการใช้งาน API นี้
ก่อนเริ่มต้น
ใส่ไลบรารี ML Kit ต่อไปนี้ใน Podfile
pod 'GoogleMLKit/DigitalInkRecognition', '3.2.0'
หลังจากติดตั้งหรืออัปเดตพ็อดของโปรเจ็กต์แล้ว ให้เปิดโปรเจ็กต์ Xcode โดยใช้
.xcworkspace
ของโปรเจ็กต์ Xcode เวอร์ชัน 13.2.1 ขึ้นไปรองรับ ML Kit
ขณะนี้คุณพร้อมที่จะเริ่มจดจำข้อความในวัตถุ 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 ซึ่งควรปรับเปลี่ยนตามความจำเป็นสำหรับแอปพลิเคชันของคุณ เราขอแนะนำให้ใช้จุดกลมเมื่อวาดส่วนของเส้นตรงเพื่อให้ส่วนที่ความยาวเป็น 0 ถูกวาดเป็นจุด (ลองนึกถึงจุดบนตัวอักษรพิมพ์เล็ก 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 จะได้รับการฝึกให้จัดการกับรูปแบบการเขียนหลากหลายประเภท แต่ผลลัพธ์อาจแตกต่างกันไปสำหรับผู้ใช้แต่ละคน
ต่อไปนี้คือวิธีปรับปรุงความแม่นยำของโปรแกรมจดจำข้อความ โปรดทราบว่าเทคนิคเหล่านี้ใช้ไม่ได้กับตัวแยกประเภทของการวาดภาพสำหรับอีโมจิ การวาดอัตโนมัติ และรูปร่าง
พื้นที่การเขียน
แอปพลิเคชันจำนวนมากมีการกำหนดพื้นที่เขียนไว้อย่างดีสำหรับการป้อนข้อมูลของผู้ใช้ ความหมายของสัญลักษณ์จะกำหนดโดยขนาดบางส่วนเทียบกับขนาดของพื้นที่เขียนที่มีสัญลักษณ์นั้นอยู่ เช่น ความแตกต่างระหว่างอักษรตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่ "o" หรือ "c" และคอมมากับเครื่องหมายทับ
การบอกเครื่องมือจดจำความกว้างและความสูงของพื้นที่เขียนจะช่วยเพิ่มความแม่นยำได้ แต่เครื่องมือจดจำจะถือว่าพื้นที่สำหรับเขียนข้อความมีเพียงบรรทัดเดียว หากพื้นที่เขียนเอกสารจริงมีขนาดใหญ่พอที่จะให้ผู้ใช้เขียนได้ 2 บรรทัดขึ้นไป คุณอาจได้รับผลลัพธ์ที่ดีขึ้นด้วยการส่งผ่าน WritingArea ด้วยความสูงที่ใกล้เคียงกับความสูงของข้อความบรรทัดเดียว ออบเจ็กต์ WritingArea ที่คุณส่งไปยังเครื่องมือจดจำไม่จำเป็นต้องสอดคล้องกับพื้นที่เขียนจริงบนหน้าจอ การเปลี่ยนความสูงของพื้นที่การเขียนด้วยวิธีนี้ จะดีกว่าในบางภาษา
เมื่อคุณระบุพื้นที่สำหรับเขียน ให้ระบุความกว้างและความสูงเป็นหน่วยเดียวกับพิกัดเส้นโครงร่าง อาร์กิวเมนต์พิกัด x,y ไม่มีข้อกำหนดหน่วย เพราะ API จะทำให้หน่วยทั้งหมดเป็นมาตรฐาน ดังนั้นสิ่งเดียวที่สำคัญคือขนาดและตำแหน่งของเส้นที่เกี่ยวข้อง คุณข้ามผ่านพิกัดในสเกลใดก็ได้ที่เหมาะสมสำหรับระบบของคุณ
ก่อนเริ่มบริบท
"ก่อนบริบท" คือข้อความที่อยู่หน้าเส้นใน Ink
ที่คุณพยายามจดจำทันที คุณช่วยเครื่องมือจดจำได้โดยการเล่าให้ฟังเกี่ยวกับก่อนเริ่มบริบท
ตัวอย่างเช่น ตัวอักษรคัดลายมือ "n" และ "u" มักทำให้เข้าใจผิดเป็นอักขระอื่น หากผู้ใช้ป้อนคํา "arg" ในบางส่วนของคําแล้ว ผู้ใช้อาจใช้เส้นที่จดจําได้ว่าเป็น "ument" หรือ "nment" การระบุ "arg" ก่อนบริบทจะช่วยแก้ความคลุมเครือ เนื่องจากคำว่า "argument" มักจะมากกว่า "argnment"
บริบทล่วงหน้ายังช่วยให้เครื่องมือจดจำระบุการแบ่งคำ ซึ่งก็คือการเว้นวรรคระหว่างคำได้ คุณสามารถพิมพ์เว้นวรรค แต่ไม่สามารถวาดอักขระได้ แล้วโปรแกรมจดจำจะทราบได้อย่างไรว่าคำหนึ่งสิ้นสุดเมื่อใดและคำถัดไปจะเริ่มต้นขึ้นเมื่อใด หากผู้ใช้เขียน "hello" ไว้แล้วและยังต่อด้วยคำว่า "world" โดยไม่มีบริบทก่อน เครื่องมือจดจำจะแสดงผลสตริง "world" อย่างไรก็ตาม หากคุณระบุคำว่า "hello" ก่อนบริบท โมเดลจะแสดงผลสตริง " world" พร้อมเว้นวรรคนำหน้า เนื่องจาก "helloworld" จะมีความหมายมากกว่า "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 อันดับแรกจะใกล้เคียงกัน นอกจากนี้ โปรดทราบว่าตัวแยกประเภทรูปร่างจะตีความ Ink
ทั้งหมดเป็นรูปร่างเดียว ตัวอย่างเช่น หาก Ink
มีรูปสี่เหลี่ยมผืนผ้าและมีวงรีอยู่ข้างๆ กัน ตัวจดจำอาจแสดงผลโดยให้อย่างใดอย่างหนึ่ง (หรือสิ่งที่แตกต่างโดยสิ้นเชิง) เป็นผลลัพธ์ เนื่องจากผู้สมัครจดจำรายการเดียวไม่สามารถแทนรูปร่าง 2 รูปได้