Python嵌入式C ++ [英] Python Embedded C++

查看:70
本文介绍了Python嵌入式C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些有关python嵌入式c ++的教程.我已经提到了python对象. https://docs.python.org/3/c-api/function.html

I have read few tutorial on python embedded c++. I had refer back to python object. https://docs.python.org/3/c-api/function.html

Python脚本:

import picamera 
from pylibdmtx.pylibdmtx import decode 
from time import sleep

import cv2
def test():
    camera = picamera.PiCamera()
    camera.start_preview()
    sleep(10)
    camera.stop_preview()

    camera.capture('image3.png')
    camera.close()

    data = decode(cv2.imread('/home/pi/image3.png'))
    return(data)

C ++脚本

#include<Python.h>
#include<string>

int main(){
String data2;

Py_Initialize();

***Doing Some Stuff TO GET data from test() function in python script and store in variable data2

Py_Finalize();
}

我以前曾经使用PyRun_SimpleString()做,它可以工作.但是,它不能将变量传递给C ++.我想要的结果是它可以将字符串存储到C ++的变量中.C ++执行python脚本后的示例,python函数返回"1234".并且"1234"存储在C ++变量(data2)

I had use PyRun_SimpleString() to do before, it can work. But, it cannot pass the variable to C++. The result I want is it can store the string to the variable at C++. Example after C++ execute the python script, python function return "1234". And "1234" is store at C++ variable (data2)

请帮我解决这个问题.这是我第一次使用python嵌入c ++,请提供一些指南.

Please, help me solve this problem. This is my first time python embedding c++, and have some guide please.

同样,如果可以的话,请提供我的解决方案

Again, if can please provide me solution on

***在Python脚本中做一些事情以从test()函数获取数据并将其存储在变量data2中

***Doing Some Stuff TO GET data from test() function in python script and store in variable data2

非常感谢....感激

推荐答案

如果我理解正确,则希望C ++代码调用Python test()函数并获取该函数的字符串结果,以便C ++代码可以执行有东西.如果是这样,我认为类似这样的方法将为您解决问题:

If I understand correctly, you want your C++ code to call your Python test() function and get the string result of that function back so the C++ code can do something with it. If so, I think something like this would do the trick for you:

 std::string data;
 char fileName[] = "my_test_python_script.py";
 PyObject * moduleObj = PyImport_ImportModule(filename);
 if (moduleObj)
 {
    char functionName[] = "test";
    PyObject * functionObj = PyObject_GetAttrString(moduleObj, functionName);
    if (functionObj)
    {
       if (PyCallable_Check(functionObj))
       {
          PyObject * argsObject = PyTuple_New(0);
          if (argsObject)
          {
             PyObject * resultObject = PyEval_CallObject(functionObj, argsObject);
             if (resultObject)
             {
                if ((resultObject != Py_None)&&(PyString_Check(resultObject))) 
                {
                    data = PyString_AsString(resultObject);
                }
                Py_DECREF(resultObject);
             }
             else if (PyErr_Occurred()) PyErr_Print();

             Py_DECREF(argsObject);
          }
       }
       Py_DECREF(functionObj);
    }
    else PyErr_Clear();

    Py_DECREF(moduleObj);
 }

 std::cout << "The Python test function returned: " << data << std::endl;

这篇关于Python嵌入式C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆