#include <string>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <sys/time.h>
using namespace std;
using namespace cv;
inline void trim(string &s)
{
int index = 0;
if (!s.empty())
{
while((index = s.find(' ',index)) != string::npos)
{
s.erase(index, 1);
}
}
}
int main(int argc, char* argv[])
{
string outText, price;
string imPath = argv[1];
Mat imgGray, result;
int flag = 0;
struct timeval beg,end;
// Create Tesseract object
tesseract::TessBaseAPI *ocr = new tesseract::TessBaseAPI();
// Initialize tesseract to use English (eng) and the LSTM OCR engine.
ocr->Init("/home/moon/program/tmp/tessdata", "chi_sim", tesseract::OEM_LSTM_ONLY);
ocr->SetVariable("tessedit_write_images", "true");
// Set Page segmentation mode to PSM_AUTO (3)
ocr->SetPageSegMode(tesseract::PSM_AUTO);
// Open input image using OpenCV
Mat im = cv::imread(imPath, IMREAD_COLOR);
Rect rect_one(0, im.rows * 100 / 192, im.cols, im.rows * 138 / 1920);
Rect rect_two(0, im.rows * 130 / 192, im.cols, im.rows * 138 / 1920);
cout << "rows:" << im.rows << " cols:" << im.cols << endl;
gettimeofday(&beg, NULL);
Mat im_one = im(rect_one);
Mat im_two = im(rect_two);
Mat im_real(im_one.rows + im_two.rows, im_one.cols, im_one.type());
im_one.rowRange(0, im_one.rows).copyTo(im_real.rowRange(0, im_one.rows));
im_two.rowRange(0, im_two.rows).copyTo(im_real.rowRange(im_one.rows, im_real.rows));
ocr->SetImage(im_real.data, im_real.cols, im_real.rows, 3, im_real.step);
outText = string(ocr->GetUTF8Text());
trim(outText);
// print recognized text
cout << outText << endl; // Destroy used object and release memory ocr->End();
int position = outText.find("需要车型");
if (position != outText.npos)
{
flag++;
position = outText.find("已支付羊");
if (position != outText.npos)
{
flag++;
price = outText.substr(position + 12);
}
}
if (flag == 0)
{
cout << "no order coming" << endl;
}
else if (flag == 1)
{
cout << "order is coming" << endl;
}
else if (flag == 2)
{
cout << "order price is: " << price << endl;
}
gettimeofday(&end, NULL);
double time = end.tv_sec - beg.tv_sec + (double)(end.tv_usec - beg.tv_usec)/1000000.0;
cout << "recognize time is: " << time << endl;
return EXIT_SUCCESS;
}
Rect rect_one(0, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
Rect rect_two(0, im.cols * 1100 / 1080, im.cols / 4, im.cols * 228 / 1080);
Rect rect_three(im.cols * 3 / 4, im.cols * 1100 / 1080, im.cols / 4, im.cols * 228 / 1080);
Mat im_one = im(rect_one);
Mat im_two = im(rect_two);
Mat im_three = im(rect_three);
Mat im_real(im_one.rows + im_two.rows + im_three.rows, im_one.cols, im_one.type());
im_one.rowRange(0, im_one.rows).copyTo(im_real.rowRange(0, im_one.rows));
im_two.rowRange(0, im_two.rows).copyTo(im_real.rowRange(im_one.rows, im_one.rows + im_two.rows));
im_three.rowRange(0, im_three.rows).copyTo(im_real.rowRange(im_one.rows + im_two.rows, im_real.rows));
imwrite("/data/local/tmp/aaa.PNG", im_real);
string outText = imageToString(im_real.data, im_real.cols, im_real.rows, 3, im_real.step);
void transToBinary(Mat src, Mat* dst, int* time, int thresh)
{
Mat gray;
// ostringstream oss;
// ostringstream oss1;
cvtColor(src, gray, CV_BGR2GRAY);
// oss << "gray_" << *time<< ".jpg";
// cout << oss.str();
// imwrite(oss.str(), gray);
threshold(gray, *dst, thresh, 255, THRESH_BINARY);
// oss1 << "binary_" << *time << ".jpg";
// imwrite(oss1.str(), *dst);
// *time = *time + 1;
}
gettimeofday(&beg, NULL);
Rect rect_one(0, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
Mat im_one = im(rect_one);
Mat binary;
transToBinary(im_one, &binary, ×, 125);
Rect rect_two(im.cols * 3 / 4, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
Mat im_two = im(rect_two);
Mat binary_two;
transToBinary(im_two, &binary_two, ×, 125);
Rect rect_three(0, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
Mat im_three = im(rect_three);
Mat binary_three;
transToBinary(im_three, &binary_three, ×, 40);
Rect rect_four(im.cols * 3 / 4, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
Mat im_four = im(rect_four);
Mat binary_four;
transToBinary(im_four, &binary_four, ×, 40);
Mat tmp1(binary.rows + binary_three.rows, binary.cols, binary.type());
Mat tmp2(binary_two.rows + binary_four.rows, binary_two.cols, binary_two.type());
Mat im_real(tmp1.rows, tmp1.cols + tmp2.cols, tmp1.type());
binary.rowRange(0, binary.rows).copyTo(tmp1.rowRange(0, binary.rows));
binary_three.rowRange(0, binary_three.rows).copyTo(tmp1.rowRange(binary.rows, binary.rows + binary_three.rows));
binary_two.rowRange(0, binary_two.rows).copyTo(tmp2.rowRange(0, binary_two.rows));
binary_four.rowRange(0, binary_four.rows).copyTo(tmp2.rowRange(binary_two.rows, binary_two.rows + binary_four.rows));
tmp1.colRange(0, tmp1.cols).copyTo(im_real.colRange(0, tmp1.cols));
tmp2.colRange(0, tmp2.cols).copyTo(im_real.colRange(tmp1.cols, im_real.cols));
imwrite("im_real.jpg", im_real);
ocr->SetImage(im_real.data, im_real.cols, im_real.rows, 1, im_real.step);
outText = string(ocr->GetUTF8Text());
trim(outText);
cout << outText << endl; // Destroy used object and release memory ocr->End();
gettimeofday(&end, NULL);
double time = end.tv_sec - beg.tv_sec + (double)(end.tv_usec - beg.tv_usec)/1000000.0;
cout << "recognize time is: " << time << endl;
return 0;
void transToBinary(Mat src, Mat* dst, int* time, int thresh_max, int thresh_min)
{
Mat gray;
Mat tmp;
ostringstream oss;
ostringstream oss1;
ostringstream oss2;
cvtColor(src, gray, CV_BGR2GRAY);
oss2 << "gray_" << *time<< ".jpg";
imwrite(oss2.str(), gray);
threshold(gray, tmp, thresh_max, 255, THRESH_TOZERO_INV);
oss1 << "binary_" << *time << ".jpg";
imwrite(oss1.str(), tmp);
threshold(tmp, *dst, thresh_min, 255, THRESH_BINARY_INV);
oss << "binary_final_" << *time << ".jpg";
imwrite(oss.str(), *dst);
*time = *time + 1;
}
gettimeofday(&beg, NULL);
#if 1
Rect rect_one(0, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
Mat im_one = im(rect_one);
Mat binary;
transToBinary(im_one, &binary, ×, 125, 115); //119 51
Rect rect_two(im.cols * 3 / 4, im.cols * 1020 / 1080, im.cols / 4, im.cols * 110 / 1080);
Mat im_two = im(rect_two);
Mat binary_two;
transToBinary(im_two, &binary_two, ×, 0, 255);
Rect rect_three(0, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
Mat im_three = im(rect_three);
Mat binary_three;
transToBinary(im_three, &binary_three, ×, 40, 30); // 34
Rect rect_four(im.cols * 3 / 4, im.cols * 1080 / 1080, im.cols / 4, im.cols * 330 / 1080);
Mat im_four = im(rect_four);
Mat binary_four;
transToBinary(im_four, &binary_four, ×, 40, 30); //34
Mat tmp1(binary.rows + binary_three.rows, binary.cols, binary.type());
Mat tmp2(binary_two.rows + binary_four.rows, binary_two.cols, binary_two.type());
Mat im_real(tmp1.rows, tmp1.cols + tmp2.cols, tmp1.type());
binary.rowRange(0, binary.rows).copyTo(tmp1.rowRange(0, binary.rows));
binary_three.rowRange(0, binary_three.rows).copyTo(tmp1.rowRange(binary.rows, binary.rows + binary_three.rows));
binary_two.rowRange(0, binary_two.rows).copyTo(tmp2.rowRange(0, binary_two.rows));
binary_four.rowRange(0, binary_four.rows).copyTo(tmp2.rowRange(binary_two.rows, binary_two.rows + binary_four.rows));
tmp1.colRange(0, tmp1.cols).copyTo(im_real.colRange(0, tmp1.cols));
tmp2.colRange(0, tmp2.cols).copyTo(im_real.colRange(tmp1.cols, im_real.cols));
imwrite("im_real.jpg", im_real);
// int start_rows = getIndex(im_real, 0);
// int end_rows = getIndex(im_real, im_real.rows);
// if (start_rows >= end_rows)
// {
ocr->SetImage(im_real.data, im_real.cols, im_real.rows, 1, im_real.step);
#endif