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 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 10 | #include "ml/handwriting_path.h" |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 11 | #include "ml/handwriting_proto_mojom_conversion.h" |
| 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) |
| 44 | : receiver_(this, std::move(receiver)) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 45 | auto* const hwr_library = ml::HandwritingLibrary::GetInstance(); |
| 46 | DCHECK(hwr_library->GetStatus() == ml::HandwritingLibrary::Status::kOk) |
| 47 | << "HandwritingRecognizerImpl should be created only if " |
| 48 | "HandwritingLibrary is initialized successfully."; |
| 49 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 50 | const auto model_path = GetModelPaths(std::move(spec)); |
| 51 | if (!model_path.has_value()) { |
| 52 | successfully_loaded_ = false; |
| 53 | return; |
| 54 | } |
| 55 | |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 56 | recognizer_ = hwr_library->CreateHandwritingRecognizer(); |
| 57 | |
| 58 | successfully_loaded_ = hwr_library->LoadHandwritingRecognizer( |
| 59 | recognizer_, chrome_knowledge::HandwritingRecognizerOptions(), |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 60 | model_path.value()); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | HandwritingRecognizerImpl::~HandwritingRecognizerImpl() { |
| 64 | ml::HandwritingLibrary::GetInstance()->DestroyHandwritingRecognizer( |
| 65 | recognizer_); |
| 66 | } |
| 67 | |
| 68 | void HandwritingRecognizerImpl::Recognize(HandwritingRecognitionQueryPtr query, |
| 69 | RecognizeCallback callback) { |
| 70 | chrome_knowledge::HandwritingRecognizerResult result_proto; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 71 | |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 72 | if (ml::HandwritingLibrary::GetInstance()->RecognizeHandwriting( |
| 73 | recognizer_, HandwritingRecognitionQueryToProto(std::move(query)), |
| 74 | &result_proto)) { |
| 75 | // Recognition succeeded, run callback on the result. |
| 76 | std::move(callback).Run(HandwritingRecognizerResultFromProto(result_proto)); |
| 77 | } else { |
| 78 | // Recognition failed, run callback on empty result and status = ERROR. |
| 79 | std::move(callback).Run(HandwritingRecognizerResult::New( |
| 80 | HandwritingRecognizerResult::Status::ERROR, |
| 81 | std::vector<HandwritingRecognizerCandidatePtr>())); |
| 82 | } |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace ml |