charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ml/handwriting_recognizer_impl.h" |
| 6 | |
| 7 | #include <utility> |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 8 | #include <vector> |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 9 | |
| 10 | #include "ml/handwriting_proto_mojom_conversion.h" |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 11 | #include "ml/request_metrics.h" |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 12 | |
| 13 | namespace ml { |
| 14 | namespace { |
| 15 | |
| 16 | using ::chromeos::machine_learning::mojom::HandwritingRecognitionQueryPtr; |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 17 | using ::chromeos::machine_learning::mojom::HandwritingRecognizerCandidatePtr; |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 18 | using ::chromeos::machine_learning::mojom::HandwritingRecognizer; |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 19 | using ::chromeos::machine_learning::mojom::HandwritingRecognizerResult; |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 20 | using ::chromeos::machine_learning::mojom::HandwritingRecognizerSpecPtr; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 21 | |
| 22 | } // namespace |
| 23 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 24 | bool HandwritingRecognizerImpl::Create( |
| 25 | HandwritingRecognizerSpecPtr spec, |
| 26 | mojo::PendingReceiver<HandwritingRecognizer> receiver) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 27 | auto recognizer_impl = |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 28 | new HandwritingRecognizerImpl(std::move(spec), std::move(receiver)); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 29 | |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 30 | // Set the disconnection handler to strongly bind `recognizer_impl` to delete |
| 31 | // `recognizer_impl` when the connection is gone. |
| 32 | recognizer_impl->receiver_.set_disconnect_handler(base::Bind( |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 33 | [](const HandwritingRecognizerImpl* const recognizer_impl) { |
| 34 | delete recognizer_impl; |
| 35 | }, |
| 36 | base::Unretained(recognizer_impl))); |
| 37 | |
| 38 | return recognizer_impl->successfully_loaded_; |
| 39 | } |
| 40 | |
| 41 | HandwritingRecognizerImpl::HandwritingRecognizerImpl( |
Andrew Moylan | b481af7 | 2020-07-09 15:22:00 +1000 | [diff] [blame] | 42 | HandwritingRecognizerSpecPtr spec, |
| 43 | mojo::PendingReceiver<HandwritingRecognizer> receiver) |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 44 | : library_(ml::HandwritingLibrary::GetInstance()), |
| 45 | receiver_(this, std::move(receiver)) { |
| 46 | DCHECK(library_->GetStatus() == ml::HandwritingLibrary::Status::kOk) |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 47 | << "HandwritingRecognizerImpl should be created only if " |
| 48 | "HandwritingLibrary is initialized successfully."; |
| 49 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 50 | recognizer_ = library_->CreateHandwritingRecognizer(); |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 51 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 52 | successfully_loaded_ = |
| 53 | library_->LoadHandwritingRecognizer(recognizer_, std::move(spec)); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | HandwritingRecognizerImpl::~HandwritingRecognizerImpl() { |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 57 | library_->DestroyHandwritingRecognizer(recognizer_); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void HandwritingRecognizerImpl::Recognize(HandwritingRecognitionQueryPtr query, |
| 61 | RecognizeCallback callback) { |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 62 | RequestMetrics request_metrics("HandwritingModel", "Recognize"); |
| 63 | request_metrics.StartRecordingPerformanceMetrics(); |
| 64 | |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 65 | chrome_knowledge::HandwritingRecognizerResult result_proto; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 66 | |
Charles Zhao | c882eb0 | 2020-07-27 10:02:35 +1000 | [diff] [blame^] | 67 | if (library_->RecognizeHandwriting( |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 68 | recognizer_, HandwritingRecognitionQueryToProto(std::move(query)), |
| 69 | &result_proto)) { |
| 70 | // Recognition succeeded, run callback on the result. |
| 71 | std::move(callback).Run(HandwritingRecognizerResultFromProto(result_proto)); |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 72 | request_metrics.FinishRecordingPerformanceMetrics(); |
| 73 | request_metrics.RecordRequestEvent(HandwritingRecognizerResult::Status::OK); |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 74 | } else { |
| 75 | // Recognition failed, run callback on empty result and status = ERROR. |
| 76 | std::move(callback).Run(HandwritingRecognizerResult::New( |
| 77 | HandwritingRecognizerResult::Status::ERROR, |
| 78 | std::vector<HandwritingRecognizerCandidatePtr>())); |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 79 | request_metrics.RecordRequestEvent( |
| 80 | HandwritingRecognizerResult::Status::ERROR); |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 81 | } |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace ml |