通过 Boost 将图像从 Python 发送到 C++

Send image from Python to C++ through Boost

我正在尝试使用 Boost 在 Python 上优化我的程序,并用 C++ 函数替换一些 Python 代码。

Python代码:

 from PIL import Image
 for i in xrange(len(lines)):
   im = Image.fromarray(lines[i])
   line = pytesseract.image_to_string(im, "ukr+ukrb") # working to slow

C++ 代码:

Pix *image = pixRead("/home/lucas63/Downloads/test.tif"); # here i need to get image directly from Python
 api->SetImage(image);
 outText = api->GetUTF8Text();
 printf("OCR output:\n%s", outText);`

所以,我需要做两件事:

  1. 使用 Boost.Python 将图像从 Python 发送到 C++。
  2. 将图像数组发送到 C++(我想通过在 C++ 中使用多线程来提高性能)。

您可以尝试使用 tesserocr 环绕 tesseract 的 C++ API:

import tesserocr

with tesserocr.PyTessBaseAPI(lang='ukr+ukrb') as api:
    for l in lines:
        im = Image.fromarray(l)
        api.SetImage(im)
        line = api.GetUTF8Text()

这将初始化 API 一次并使用它来处理多个图像。