1
1
// /////////////////////////////////////////////////////////////////////
2
- // File: apiexample.cpp
2
+ // File: apiexample_test.cc
3
3
// Description: Api Example for Tesseract.
4
4
// Author: ShreeDevi Kumar
5
5
//
23
23
24
24
TEST (TesseractTest, ApiExample)
25
25
{
26
- const char * imagefile = " ../testing/phototest.tif" ;
27
- const char * groundtruth = " testfiles/phototest.txt" ;
28
26
char *outText;
29
- std::locale loc (" en_US.UTF-8" );
30
- std::ifstream file (groundtruth );
31
- file.imbue (loc);
27
+ std::locale loc (" en_US.UTF-8" ); // You can also use "" for the default system locale
28
+ std::ifstream file (" testfiles/phototest.txt " );
29
+ file.imbue (loc); // Use it for file input
32
30
std::string gtText ((std::istreambuf_iterator<char >(file)),
33
31
std::istreambuf_iterator<char >());
32
+
34
33
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI ();
34
+ // Initialize tesseract-ocr with English, without specifying tessdata path
35
35
if (api->Init (NULL , " eng" )) {
36
36
fprintf (stderr, " Could not initialize tesseract.\n " );
37
37
exit (1 );
38
38
}
39
- Pix *image = pixRead (imagefile);
39
+
40
+ // Open input image with leptonica library
41
+ Pix *image = pixRead (" ../testing/phototest.tif" );
40
42
api->SetImage (image);
41
- api-> SetPageSegMode (tesseract::PSM_AUTO_OSD);
43
+ // Get OCR result
42
44
outText = api->GetUTF8Text ();
43
- ASSERT_EQ (gtText,outText) << " Phototest.tif with default values OCR does not match ground truth" ;
45
+
46
+ ASSERT_EQ (gtText,outText) << " Phototest.tif with default values OCR does not match ground truth" ;
47
+
48
+ // Destroy used object and release memory
44
49
api->End ();
45
50
delete [] outText;
46
51
pixDestroy (&image);
52
+
47
53
}
48
54
49
55
int main (int argc, char **argv) {
50
56
::testing::InitGoogleTest (&argc, argv);
51
57
return RUN_ALL_TESTS ();
52
- }
58
+ }
0 commit comments