Honglin Yu | 0c4760a | 2020-04-18 20:53:31 +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 <fstream> |
| 6 | #include <string> |
| 7 | |
| 8 | #include <base/files/file_path.h> |
| 9 | #include <base/files/file_util.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | #include "chrome/knowledge/handwriting/validate.pb.h" |
| 13 | #include "ml/handwriting.h" |
| 14 | |
| 15 | namespace ml { |
| 16 | |
| 17 | TEST(HandwritingLibraryTest, CanLoadLibrary) { |
| 18 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
| 19 | #ifdef ML_SUPPORT_HANDWRITING |
| 20 | EXPECT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 21 | #else |
| 22 | EXPECT_EQ(instance->GetStatus(), |
| 23 | ml::HandwritingLibrary::Status::kLoadLibraryFailed); |
| 24 | #endif |
| 25 | } |
| 26 | |
| 27 | #ifdef ML_SUPPORT_HANDWRITING |
| 28 | TEST(HandwritingLibraryTest, ExampleRequest) { |
| 29 | auto* const instance = ml::HandwritingLibrary::GetInstance(); |
| 30 | ASSERT_EQ(instance->GetStatus(), ml::HandwritingLibrary::Status::kOk); |
| 31 | |
| 32 | HandwritingRecognizer const recognizer = |
| 33 | instance->CreateHandwritingRecognizer(); |
| 34 | chrome_knowledge::HandwritingRecognizerModelPaths paths; |
| 35 | paths.set_reco_model_path( |
| 36 | "/opt/google/chrome/ml_models/handwriting/latin_indy.tflite"); |
| 37 | paths.set_seg_model_path( |
| 38 | "/opt/google/chrome/ml_models/handwriting/latin_indy_seg.tflite"); |
| 39 | paths.set_conf_model_path( |
| 40 | "/opt/google/chrome/ml_models/handwriting/latin_indy_conf.tflite"); |
| 41 | paths.set_fst_lm_path( |
| 42 | "/opt/google/chrome/ml_models/handwriting/latin_indy.compact.fst"); |
| 43 | paths.set_recospec_path( |
| 44 | "/opt/google/chrome/ml_models/handwriting/latin_indy.pb"); |
| 45 | chrome_knowledge::HandwritingRecognizerOptions options; |
| 46 | ASSERT_TRUE(instance->LoadHandwritingRecognizer(recognizer, options, paths)); |
| 47 | |
| 48 | chrome_knowledge::HandwritingRecognizerLabeledRequests test_data; |
| 49 | std::string buf; |
| 50 | ASSERT_TRUE(base::ReadFileToString( |
| 51 | base::FilePath("/build/share/libhandwriting/correct_labeled_requests.pb"), |
| 52 | &buf)); |
| 53 | ASSERT_TRUE(test_data.ParseFromString(buf)); |
| 54 | ASSERT_GT(test_data.labeled_requests().size(), 0); |
| 55 | for (auto const& request : test_data.labeled_requests()) { |
| 56 | chrome_knowledge::HandwritingRecognizerResult result; |
| 57 | ASSERT_TRUE( |
| 58 | instance->RecognizeHandwriting(recognizer, request.request(), &result)); |
| 59 | ASSERT_GT(result.candidates().size(), 0); |
| 60 | EXPECT_EQ(result.candidates(0).text(), request.label()); |
| 61 | } |
| 62 | instance->DestroyHandwritingRecognizer(recognizer); |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | } // namespace ml |