首页 > 解决方案 > Python 脚本在到达 CV2 函数时卡住了

问题描述

我正在尝试使用 boost:python 从 C++ 调用 python 脚本。

正在调用模块和类,并且一切正常,直到脚本到达使用 opencv 的函数

调用 python 的 c++ 代码是通过线程启动的类的一部分。调用 python 的代码部分是:

#include <boost/python.hpp>
#include "boost/python/stl_iterator.hpp"

#include <opencv2/opencv.hpp>

...

imencode(".jpg", image, imageBuffer);

namespace python = boost::python;

vector<unsigned char>::iterator iter;
python::list list;
for (iter = imageBuffer.begin(); iter != imageBuffer.end(); ++iter) {
    list.append(*iter);
}


python::object python_module = python::import("file");
python::object klass = python_module.attr("klass")(this->modelPath.c_str());
python::object res =  klass.attr("fun")(list);

file.py 看起来像:

import numpy as np
import cv2
class klass:

    def __init__(self, model_path):

        self.model = model_path

    def fun(self, image):

        image = np.asarray(image, dtype=np.uint8)
        print("b4")
        image = cv2.imdecode(image, 1)
        print("after")
        return 1

"print("b4")" 行被打印,但是一旦达到 imdecode,代码就会卡住,什么也没有发生

opencv-python 安装使用

sudo pip3 install opencv-python

opencv-python 版本是 4.1.0 numpy 版本是 1.16.3

我正在使用 python 3.5

为什么代码会暂停以及如何解决

编辑

我只需要提到调用是从一个线程完成的。如果调用是从 Main 完成的,则代码有效。此代码通过 pthread_create() 调用

显然我在线程中遗漏了一些东西

编辑 2

所以这确实是一个线程问题,我想出了一个“解决方案”,我将作为答案发布。我不知道这是否是解决它的正确方法,但它有效

标签: python-3.xopencvboost-pythonopencv-python

解决方案


为了解决这个问题,我在 py_initialize 之后添加了以下内容

Py_Initialize();
PyEval_InitThreads();
PyEval_ReleaseLock();

然后当线程开始时我做了:

PyGILState_STATE gstate;
gstate = PyGILState_Ensure();

然后做所有的python调用....

在结束通话时

PyGILState_Release(gstate);

请注意,在 PyGILState_Release() 之后的任何 python 调用(甚至是 DECREF 调用)都将不起作用并导致代码崩溃,这就是为什么我将该调用放在我的线程类析构函数中作为最后调用的原因。


推荐阅读