adray
adray

Reputation: 1448

Run python script with opencv from C++

I'm trying to execute python script from C++,

C++ part:

Py_Initialize();
PyRun_SimpleString("import sys");
PyRun_SimpleString("print(sys.version)");
PyRun_SimpleString("sys.path.append('/path/to/module')");
pName = PyString_FromString("mymodule");
pModule = PyImport_Import(pName);
import_array();
PyArrayObject *np_ret, *np_arg;
pDict = PyModule_GetDict(pModule);
pFunc = PyDict_GetItemString(pDict, "process");
PyObject_CallFunctionObjArgs(pFunc, NULL);

python module:

import random
import numpy as np
import cv2
import sys
import segno


def process():
  n = random.randint(1, 1000);
  qr = segno.make(n)
  mark = np.uint8(np.array(qr.to_pil(scale=2)))*255
  mark = cv2.cvtColor(mark, cv2.COLOR_GRAY2RGB)

When I compile it with opencv 3.2.0 it's working fine, but with 2.4.0 it gives segmentation fault on calling opencv function cv2.cvtColor(mark, cv2.COLOR_GRAY2RGB)

Is it possible to do so with 2.4.0? Maybe there is some module initializer such as import_array()for numpy?

Upvotes: 1

Views: 432

Answers (1)

onur aslan
onur aslan

Reputation: 292

Syntax is different at 2.4 so modify as following

mark = cv2.cvtColor(mark,mark, CV_GRAY2RGB)

Upvotes: 1

Related Questions