sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
安装模型
sudo apt-get install tesseract-ocr-chi-sim
tesseract test.jpeg ./test -l chi-sim
#include <stdio.h> #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, "eng+chi_sim")) { fprintf(stderr, "Could not initialize tesseract. "); exit(1); } // Open input image with leptonica library Pix *image = pixRead("./invoice_dialog.png"); api->SetImage(image); // Get OCR result outText = api->GetUTF8Text(); printf("OCR output: %s", outText); // Destroy used object and release memory api->End(); delete [] outText; pixDestroy(&image); return 0; }
g++ -o myprogram myprogram.cpp -llept -ltesseract
出错
/usr/bin/ld: /tmp/ccIl3vUD.o: undefined reference to symbol '__gxx_personality_v0@@CXXABI_1.3' //usr/lib/x86_64-linux-gnu/libstdc++.so.6: 无法添加符号: DSO missing from command line collect2: error: ld returned 1 exit status The terminal process terminated with exit code: 1
加入选项
g++ -o myprogram myprogram.cpp -llept -ltesseract -L/usr/lib/x86_64-linux-gnu -lstdc++
出错
fatal error: leptonica/allheaders.h: No such file or directory
sudo apt install libleptonica-dev