mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-06-10 20:23:12 +08:00
Add unit test for OSD, update apiexample test (#1359)
* Update apiexample_test.cc * Add OSD test and logging function * Add images for OSD unittest
This commit is contained in:
parent
7972b13e3a
commit
5845e1a27d
BIN
testing/devatest-rotated-270.png
Normal file
BIN
testing/devatest-rotated-270.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 598 KiB |
BIN
testing/devatest.png
Normal file
BIN
testing/devatest.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 522 KiB |
BIN
testing/phototest-rotated-180.png
Normal file
BIN
testing/phototest-rotated-180.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.2 KiB |
BIN
testing/phototest-rotated-L.png
Normal file
BIN
testing/phototest-rotated-L.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
BIN
testing/phototest-rotated-R.png
Normal file
BIN
testing/phototest-rotated-R.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.7 KiB |
@ -30,8 +30,10 @@ AM_CPPFLAGS += -isystem $(top_srcdir)/googletest/googletest/include
|
|||||||
check_PROGRAMS = \
|
check_PROGRAMS = \
|
||||||
apiexample_test \
|
apiexample_test \
|
||||||
intsimdmatrix_test \
|
intsimdmatrix_test \
|
||||||
tesseracttests \
|
matrix_test \
|
||||||
matrix_test
|
osd_test \
|
||||||
|
tesseracttests
|
||||||
|
|
||||||
|
|
||||||
TESTS = $(check_PROGRAMS)
|
TESTS = $(check_PROGRAMS)
|
||||||
|
|
||||||
@ -47,6 +49,9 @@ intsimdmatrix_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
|
|||||||
matrix_test_SOURCES = matrix_test.cc
|
matrix_test_SOURCES = matrix_test.cc
|
||||||
matrix_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
|
matrix_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS)
|
||||||
|
|
||||||
|
osd_test_SOURCES = osd_test.cc
|
||||||
|
osd_test_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
|
||||||
|
|
||||||
tesseracttests_SOURCES = ../tests/tesseracttests.cpp
|
tesseracttests_SOURCES = ../tests/tesseracttests.cpp
|
||||||
tesseracttests_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
|
tesseracttests_LDADD = $(GTEST_LIBS) $(TESS_LIBS) $(LEPTONICA_LIBS)
|
||||||
|
|
||||||
@ -55,6 +60,7 @@ if T_WIN
|
|||||||
apiexample_test_LDADD += -lws2_32
|
apiexample_test_LDADD += -lws2_32
|
||||||
intsimdmatrix_test_LDADD += -lws2_32
|
intsimdmatrix_test_LDADD += -lws2_32
|
||||||
matrix_test_LDADD += -lws2_32
|
matrix_test_LDADD += -lws2_32
|
||||||
|
osd_test_LDADD += -lws2_32
|
||||||
tesseracttests_LDADD += -lws2_32
|
tesseracttests_LDADD += -lws2_32
|
||||||
|
|
||||||
AM_CPPFLAGS += -I$(top_srcdir)/vs2010/port
|
AM_CPPFLAGS += -I$(top_srcdir)/vs2010/port
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
// File: apiexample_test.cc
|
// File: apiexample_test.cc
|
||||||
// Description: Api Example for Tesseract.
|
// Description: Api Test for Tesseract using text fixtures and parameters.
|
||||||
// Author: ShreeDevi Kumar
|
// Author: ShreeDevi Kumar
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
@ -13,44 +13,69 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
#include "gtest/gtest.h"
|
|
||||||
|
// expects clone of tessdata_fast repo in ../../tessdata_fast
|
||||||
|
|
||||||
|
//#include "log.h"
|
||||||
|
#include "include_gunit.h"
|
||||||
#include "baseapi.h"
|
#include "baseapi.h"
|
||||||
#include "leptonica/allheaders.h"
|
#include "leptonica/allheaders.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <locale>
|
#include <locale>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
TEST(TesseractTest, ApiExample)
|
namespace {
|
||||||
{
|
|
||||||
|
class QuickTest : public testing::Test {
|
||||||
|
protected:
|
||||||
|
virtual void SetUp() {
|
||||||
|
start_time_ = time(NULL);
|
||||||
|
}
|
||||||
|
virtual void TearDown() {
|
||||||
|
const time_t end_time = time(NULL);
|
||||||
|
EXPECT_TRUE(end_time - start_time_ <=25) << "The test took too long - " << ::testing::PrintToString(end_time - start_time_);
|
||||||
|
}
|
||||||
|
time_t start_time_;
|
||||||
|
};
|
||||||
|
|
||||||
|
void OCRTester(const char* imgname, const char* groundtruth, const char* tessdatadir, const char* lang) {
|
||||||
|
//log.info() << tessdatadir << " for language: " << lang << std::endl;
|
||||||
char *outText;
|
char *outText;
|
||||||
std::locale loc("C"); // You can also use "" for the default system locale
|
std::locale loc("C"); // You can also use "" for the default system locale
|
||||||
std::ifstream file("../testing/phototest.txt");
|
std::ifstream file(groundtruth);
|
||||||
file.imbue(loc); // Use it for file input
|
file.imbue(loc); // Use it for file input
|
||||||
std::string gtText((std::istreambuf_iterator<char>(file)),
|
std::string gtText((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
|
|
||||||
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
|
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
|
||||||
// Initialize tesseract-ocr with English, without specifying tessdata path
|
ASSERT_FALSE(api->Init(tessdatadir, lang)) << "Could not initialize tesseract.";
|
||||||
ASSERT_FALSE(api->Init(nullptr, "eng")) << "Could not initialize tesseract.";
|
Pix *image = pixRead(imgname);
|
||||||
|
|
||||||
// Open input image with leptonica library
|
|
||||||
Pix *image = pixRead("../testing/phototest.tif");
|
|
||||||
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
||||||
api->SetImage(image);
|
api->SetImage(image);
|
||||||
// Get OCR result
|
|
||||||
outText = api->GetUTF8Text();
|
outText = api->GetUTF8Text();
|
||||||
|
EXPECT_EQ(gtText,outText) << "Phototest.tif OCR does not match ground truth for " << ::testing::PrintToString(lang);
|
||||||
ASSERT_EQ(gtText,outText) << "Phototest.tif with default values OCR does not match ground truth";
|
|
||||||
|
|
||||||
// Destroy used object and release memory
|
|
||||||
api->End();
|
api->End();
|
||||||
delete [] outText;
|
delete [] outText;
|
||||||
pixDestroy(&image);
|
pixDestroy(&image);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
class MatchGroundTruth : public QuickTest ,
|
||||||
|
public ::testing::WithParamInterface<const char*> {
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
TEST_P(MatchGroundTruth, FastPhototestOCR) {
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
OCRTester("../testing/phototest.tif" ,"../testing/phototest.txt" , "../../tessdata_fast", GetParam());
|
||||||
return RUN_ALL_TESTS();
|
}
|
||||||
}
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( EngLatinDevaArabLang, MatchGroundTruth,
|
||||||
|
::testing::Values("eng", "Latin", "Devanagari", "Arabic") );
|
||||||
|
|
||||||
|
class EuroText : public QuickTest {
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(EuroText, FastOCR) {
|
||||||
|
OCRTester("../testing/eurotext.tif" ,"../testing/eurotext.txt" , "../../tessdata_fast", "Latin");
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
28
unittest/log.h
Normal file
28
unittest/log.h
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
// File: log.h
|
||||||
|
// Description: Include for custom log message for unittest for tesseract.
|
||||||
|
// based on //https://stackoverflow.com/questions/16491675/how-to-send-custom-message-in-google-c-testing-framework
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
#ifndef TESSERACT_UNITTEST_LOG_H_
|
||||||
|
#define TESSERACT_UNITTEST_LOG_H_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static class LOG { public: LOG() {}
|
||||||
|
std::ostream& info() {
|
||||||
|
std::cout << "[ LOG MSG ] ";
|
||||||
|
return std::cout;
|
||||||
|
}
|
||||||
|
} log;
|
||||||
|
|
||||||
|
#endif // TESSERACT_UNITTEST_LOG_H_
|
110
unittest/osd_test.cc
Normal file
110
unittest/osd_test.cc
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
// File: osd_test.cc
|
||||||
|
// Description: OSD Tests for Tesseract.
|
||||||
|
// Author: ShreeDevi Kumar
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
//based on https://gist.github.com/amitdo/7c7a522004dd79b398340c9595b377e1
|
||||||
|
|
||||||
|
// expects clones of tessdata, tessdata_fast and tessdata_best repos
|
||||||
|
|
||||||
|
#include "log.h"
|
||||||
|
#include "include_gunit.h"
|
||||||
|
#include "baseapi.h"
|
||||||
|
#include "leptonica/allheaders.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
class TestClass : public testing::Test {
|
||||||
|
protected:
|
||||||
|
};
|
||||||
|
|
||||||
|
void OSDTester( int expected_deg, const char* imgname, const char* tessdatadir) {
|
||||||
|
log.info() << tessdatadir << " for image: " << imgname << std::endl;
|
||||||
|
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
|
||||||
|
ASSERT_FALSE(api->Init(tessdatadir, "osd")) << "Could not initialize tesseract.";
|
||||||
|
Pix *image = pixRead(imgname);
|
||||||
|
ASSERT_TRUE(image != nullptr) << "Failed to read test image.";
|
||||||
|
api->SetImage(image);
|
||||||
|
int orient_deg;
|
||||||
|
float orient_conf;
|
||||||
|
const char* script_name;
|
||||||
|
float script_conf;
|
||||||
|
bool detected = api->DetectOrientationScript(&orient_deg, &orient_conf, &script_name, &script_conf);
|
||||||
|
ASSERT_FALSE(!detected) << "Failed to detect OSD.";
|
||||||
|
printf("************ Orientation in degrees: %d, Orientation confidence: %.2f\n"
|
||||||
|
" Script: %s, Script confidence: %.2f\n",
|
||||||
|
orient_deg, orient_conf,
|
||||||
|
script_name, script_conf);
|
||||||
|
EXPECT_EQ(expected_deg, orient_deg);
|
||||||
|
api->End();
|
||||||
|
pixDestroy(&image);
|
||||||
|
}
|
||||||
|
|
||||||
|
class OSDTest : public TestClass ,
|
||||||
|
public ::testing::WithParamInterface<std::tuple<int, const char*, const char*>> {};
|
||||||
|
|
||||||
|
TEST_P(OSDTest, MatchOrientationDegrees) {
|
||||||
|
OSDTester(std::get<0>(GetParam()), std::get<1>(GetParam()), std::get<2>(GetParam()));
|
||||||
|
}
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataEngEuroHebrew, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(0),
|
||||||
|
::testing::Values("../testing/phototest.tif" , "../testing/eurotext.tif" , "../testing/hebrew.png"),
|
||||||
|
::testing::Values("../../tessdata")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataBestEngEuroHebrew, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(0),
|
||||||
|
::testing::Values("../testing/phototest.tif" , "../testing/eurotext.tif" , "../testing/hebrew.png"),
|
||||||
|
::testing::Values("../../tessdata_best")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataFastEngEuroHebrew, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(0),
|
||||||
|
::testing::Values("../testing/phototest.tif" , "../testing/eurotext.tif" , "../testing/hebrew.png"),
|
||||||
|
::testing::Values("../../tessdata_fast")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataFastRotated90, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(90),
|
||||||
|
::testing::Values("../testing/phototest-rotated-R.png"),
|
||||||
|
::testing::Values("../../tessdata_fast")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataFastRotated180, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(180),
|
||||||
|
::testing::Values("../testing/phototest-rotated-180.png"),
|
||||||
|
::testing::Values("../../tessdata_fast")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataFastRotated270, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(270),
|
||||||
|
::testing::Values("../testing/phototest-rotated-L.png"),
|
||||||
|
::testing::Values("../../tessdata_fast")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataFastDevaRotated270, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(270),
|
||||||
|
::testing::Values("../testing/devatest-rotated-270.png"),
|
||||||
|
::testing::Values("../../tessdata_fast")));
|
||||||
|
|
||||||
|
INSTANTIATE_TEST_CASE_P( TessdataFastDeva, OSDTest,
|
||||||
|
::testing::Combine(
|
||||||
|
::testing::Values(0),
|
||||||
|
::testing::Values("../testing/devatest.png"),
|
||||||
|
::testing::Values("../../tessdata_fast")));
|
||||||
|
|
||||||
|
} // namespace
|
Loading…
Reference in New Issue
Block a user