首页 > 解决方案 > 从 python VideoCapture() 将帧传递给 c++

问题描述

我将 USB 摄像头读取为 cv2.VideoCapture() 函数并显示帧。我需要一些 c++ 应用程序从 python 应用程序读取帧。

我怎样才能做到这一点?

脚步:

标签: pythonc++video-captureimshow

解决方案


您可以在 C++ 中使用嵌入 python 并在 C++ 程序中调用 python 函数。放入 python 模块并在该模块中定义一个字典,其中包含文件中帧的数据,例如 test.py。

#include <Python.h>
int main(int argc, char *argv[])
{
    Py_Initialize();     
    pName = PyString_FromString("test.py");
    pModule = PyImport_Import(pName);
    pDict = PyModule_GetDict(pModule);
    pFunc = PyDict_GetItemString(pDict, "frame");
     if (PyCallable_Check(pFunc)) 
     {
          PyObject_CallObject(pFunc, NULL);
     } 
     else 
     {
         PyErr_Print();
     }

    // Clean up
    Py_DECREF(pModule);
    Py_DECREF(pName);

    // Finish the Python Interpreter
    Py_Finalize();
    return 0;
 }
   

推荐阅读