从图像中识别Hindi 字符并将相应的边界框值和相应的印地语字符存储到一个文件中的示例程序。
/*
* Char_OCR.cpp
*
* Created on: Jun 23, 2016
* Author: pratik
*/
#include <opencv2/opencv.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
void dumpIntoFile(const char *ocrResult , ofstream &myfile1 ,int x1, int y1,
int x2, int y2, int &);
int main(int argc ,char **argv)
{
Pix *image = pixRead(argv[1]);
if (image == 0) {
cout << "Cannot load input file!\n";
}
tesseract::TessBaseAPI tess;
if (tess.Init("/usr/share/tesseract/tessdata", "hin")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
tess.SetImage(image);
tess.Recognize(0);
tesseract::ResultIterator *ri = tess.GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
cout << ri << endl;
ofstream myfile1("Word.txt");
myfile1 << "ID" << '\t' << "CORD_X" << '\t' << "CORD_Y" << '\t' <<
"CORD_W" << '\t' << "CORD_H" << '\t' << "STRING" << endl;
int i=1;
if(ri!=0)
{
do {
const char *word = ri->GetUTF8Text(level);
// cout << word << endl;
//float conf = ri->Confidence(level);
int x1, y1, x2, y2;
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
dumpIntoFile(word, myfile1, x1, y1, x2, y2, i);
delete []word;
} while (ri->Next(level));
delete []ri;
}
}
void dumpIntoFile(const char *ocrResult , ofstream &myfile1 ,int x1, int y1,
int x2, int y2,int &i)
{
int length = strlen(ocrResult);
myfile1 << i++ << '\t' << x1 << '\t' << y1 << '\t' <<
x2 << '\t' << y2 << '\t' ;
//cout << "in the string (" << length << ") ::";
for(int j = 0; j < length && ocrResult[j] != '\n'; j++)
{
myfile1 << ocrResult[j];
}
myfile1 << endl;
}