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