Skip to content

How to pass "python-cv mat" or "numpy array" to my c++ class in BOOST_PYTHON_MODULE ? #1

Description

@dedoogong

Thank you for your great works!
I'm trying to pass a image(read from python-cv) to myclass's method as below.



[video.py]
import mymodule
img=cv2.imread(ImgPath)
boxes=mymodule.detect(img)


[module.cpp]
#include <opencv2/highgui/highgui.hpp>
#include <numpy/arrayobject.h>
#include <stdio.h>
#include "pyboostcvconverter/pyboostcvconverter.hpp"
#include "myclass.h"
using namespace cv;
using namespace boost::python;
BOOST_PYTHON_MODULE(mymodule)
{
class_< myclass > ("mymodule", init)//
.def("detect", &myclass::detect)
;
}


[myclass.cpp]
class myclass{
....
detect(Mat img){
....
}
}

But I failed....
Can you please tell me a guide or hint how to pass image to my c++ class using BOOST_PYTHON_MODULE macro?

is it possible to do it with some other methods?

If I declare a new "myclass" type variable and pass a mat from python as you do, it just create a new myclass instance that might run separately from python-bound myclass instance.


[module.cpp]
#include <opencv2/highgui/highgui.hpp>
#include <numpy/arrayobject.h>
#include <stdio.h>
#include "pyboostcvconverter/pyboostcvconverter.hpp"
#include "myclass.h"
using namespace cv;
using namespace boost::python;
BOOST_PYTHON_MODULE(mymodule)
{
class_< myclass > ("mymodule", init)//
.def("detect", &myclass::detect)
;
}

static PyObject * pyMirror(PyObject *self, PyObject *args) {
PyObject *ndArray;

if (!PyArg_ParseTuple(args, "O", &ndArray))
    return NULL;

Mat img = pbcvt::fromNDArrayToMat(ndArray);
myclass my("cfgFilePath","savePath"); /* 
this is a newly created myclass instance. it maybe run independently from python code I think...
I guess I need to pass "img" in the interface binding above "BOOST_PYTHON_MODULE" macro 
 block...*/
my.detect(img);

return pbcvt::fromMatToNDArray(mat);

}

Please let me know any solution.

Thank you!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions