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

Send image from Python to C++ through Boost

本文关键字:c++ Python Boost 图像 通过      更新时间:2023-10-16

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

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一次,并使用它来处理多个图像。