Skip to content

Commit

Permalink
OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
Burhan Two committed Jun 17, 2018
1 parent ea281de commit dc8143f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions C_API.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <leptonica/allheaders.h>
#include <tesseract/capi.h>

void die(const char *errstr) {
fputs(errstr, stderr);
exit(1);
}

int main(int argc, char *argv[]) {
TessBaseAPI *handle;
PIX *img;
char *text;

if((img = pixRead("russian-text.png")) == NULL)
die("Error reading image\n");

handle = TessBaseAPICreate();
if(TessBaseAPIInit3(handle, NULL, "rus") != 0)
die("Error initialising tesseract\n");

TessBaseAPISetImage2(handle, img);
if(TessBaseAPIRecognize(handle, NULL) != 0)
die("Error in Tesseract recognition\n");

if((text = TessBaseAPIGetUTF8Text(handle)) == NULL)
die("Error getting text\n");

fputs(text, stdout);

TessDeleteText(text);
TessBaseAPIEnd(handle);
TessBaseAPIDelete(handle);
pixDestroy(&img);

return 0;
}
28 changes: 28 additions & 0 deletions OCR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>

int main()
{
char *outText;

tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "rus")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}

// Open input image with leptonica library
Pix *image = pixRead("a.jpg");
api->SetImage(image);
// Get OCR result
outText = api->GetUTF8Text();
printf("OCR output:\n%s", outText);

// Destroy used object and release memory
api->End();
delete [] outText;
pixDestroy(&image);

return 0;
}

0 comments on commit dc8143f

Please sign in to comment.