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; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 18 | using ::chromeos::machine_learning::mojom::HandwritingRecognizerRequest; |
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 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 24 | bool HandwritingRecognizerImpl::Create(HandwritingRecognizerSpecPtr spec, |
| 25 | HandwritingRecognizerRequest request) { |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 26 | auto recognizer_impl = |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 27 | new HandwritingRecognizerImpl(std::move(spec), std::move(request)); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 28 | |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 29 | // Set the connection error handler to strongly bind `recognizer_impl` to |
| 30 | // delete `recognizer_impl` when the connection is gone. |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 31 | recognizer_impl->binding_.set_connection_error_handler(base::Bind( |
| 32 | [](const HandwritingRecognizerImpl* const recognizer_impl) { |
| 33 | delete recognizer_impl; |
| 34 | }, |
| 35 | base::Unretained(recognizer_impl))); |
| 36 | |
| 37 | return recognizer_impl->successfully_loaded_; |
| 38 | } |
| 39 | |
| 40 | HandwritingRecognizerImpl::HandwritingRecognizerImpl( |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 41 | HandwritingRecognizerSpecPtr spec, HandwritingRecognizerRequest request) |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 42 | : binding_(this, std::move(request)) { |
| 43 | auto* const hwr_library = ml::HandwritingLibrary::GetInstance(); |
| 44 | DCHECK(hwr_library->GetStatus() == ml::HandwritingLibrary::Status::kOk) |
| 45 | << "HandwritingRecognizerImpl should be created only if " |
| 46 | "HandwritingLibrary is initialized successfully."; |
| 47 | |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 48 | const auto model_path = GetModelPaths(std::move(spec)); |
| 49 | if (!model_path.has_value()) { |
| 50 | successfully_loaded_ = false; |
| 51 | return; |
| 52 | } |
| 53 | |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 54 | recognizer_ = hwr_library->CreateHandwritingRecognizer(); |
| 55 | |
| 56 | successfully_loaded_ = hwr_library->LoadHandwritingRecognizer( |
| 57 | recognizer_, chrome_knowledge::HandwritingRecognizerOptions(), |
charleszhao | 05c5a4a | 2020-06-09 16:49:54 +1000 | [diff] [blame] | 58 | model_path.value()); |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | HandwritingRecognizerImpl::~HandwritingRecognizerImpl() { |
| 62 | ml::HandwritingLibrary::GetInstance()->DestroyHandwritingRecognizer( |
| 63 | recognizer_); |
| 64 | } |
| 65 | |
| 66 | void HandwritingRecognizerImpl::Recognize(HandwritingRecognitionQueryPtr query, |
| 67 | RecognizeCallback callback) { |
| 68 | chrome_knowledge::HandwritingRecognizerResult result_proto; |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 69 | |
charleszhao | 492441e | 2020-05-26 16:17:07 +1000 | [diff] [blame] | 70 | if (ml::HandwritingLibrary::GetInstance()->RecognizeHandwriting( |
| 71 | recognizer_, HandwritingRecognitionQueryToProto(std::move(query)), |
| 72 | &result_proto)) { |
| 73 | // Recognition succeeded, run callback on the result. |
| 74 | std::move(callback).Run(HandwritingRecognizerResultFromProto(result_proto)); |
| 75 | } else { |
| 76 | // Recognition failed, run callback on empty result and status = ERROR. |
| 77 | std::move(callback).Run(HandwritingRecognizerResult::New( |
| 78 | HandwritingRecognizerResult::Status::ERROR, |
| 79 | std::vector<HandwritingRecognizerCandidatePtr>())); |
| 80 | } |
charleszhao | 17777f9 | 2020-04-23 12:53:11 +1000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | } // namespace ml |