Ask Your Question
0

How to use gen2.py and c++ file to make Python module

asked 2019-02-01 03:31:55 -0600

gino0717 gravatar image

updated 2019-02-01 03:33:22 -0600

I want to make my c++ function as python module in linux, to make the question easy I make a simple sample as:

testcv.h:

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
namespace JustATest{
CV_EXPORTS_W class TestCV
{
public:
    CV_WRAP TestCV();
    CV_WRAP void TakeALook();
};
}

testcv.cpp

#include "testcv.h"
using namespace JustATest;
TestCV::TestCV()
{

}
void TestCV::TakeALook()
{
    Mat src=imread("a.jpg");
    imshow("see!",src);
    waitKey(0);
}

these two files are in the /src/ folder in the same path of gen2.py

and a header.txt as:

src/testcv.h

now I have some command copy from there (not really know what the pybv is but I just take a look of gen2.py)

python3 gen2.py pybv build header.txt

and the message shows:

Traceback (most recent call last):
  File "gen2.py", line 1181, in <module>
    with open(sys.argv[2], 'r') as f:
IsADirectoryError: [Errno 21] Is a directory: 'build'

it seems gen2.py don't let me put anything before the build path. so I use

 python3 gen2.py build header.txt

now I see many "pyopencv_generated..." file in the build folder without any content (0 byte). What should I do to make the modules exactly?

edit retag flag offensive close merge delete

Comments

which opencv version is it ? (there seem to be changes in current 4.0.1, e.g. there's no "module prefix" any more, when calling cv2.py)

berak gravatar imageberak ( 2019-02-01 03:55:16 -0600 )edit

I use 4.0.1. It seems nowhere to find the actual usage of current version of gen2.py

gino0717 gravatar imagegino0717 ( 2019-02-01 04:54:14 -0600 )edit
1

have a look here, so, imho your : python3 gen2.py build header.txt is correct (maybe "build" needs an absolute path,idk.)

also try an absolute path inside headers.txt !

berak gravatar imageberak ( 2019-02-01 04:57:51 -0600 )edit

ok, I use absolute path and use some comand like:

g++ -shared -rdynamic -g -O3 -Wall -fPIC \
 /home/gino/opencv/modules/python/src2/src/testcv.cpp \
-DNDEBUG -DPY_MAJOR_VERSION=3 \
`pkg-config --cflags --libs opencv`  \
`python3-config --includes --ldflags` \
-I . -I/usr/local/lib/python3.5/dist-packages/numpy/core/include \
-o build/JustATest.so

now It seems can build a JustATest.so for me to import, when I tried some code in python:

import cv2
import sys
sys.path.append('/home/gino/opencv/modules/python/src2/build')
import JustATest

and python report

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_JustATest)

What should I do?

gino0717 gravatar imagegino0717 ( 2019-02-10 20:37:56 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-02-01 04:16:10 -0600

berak gravatar image

updated 2019-02-01 04:58:20 -0600

i'd propose a similar, but maybe easier approach: instead of generating a new, seperate python module (.so), embed your code into the existing cv2.so

1. put your code into the opencv src tree:

make a new folder inside opencv/modules, containing your code:

opencv/modules/testcv
|   CMakeLists.txt
|
+---include
|   \---opencv2
|           testcv.hpp    // hpp, not h, please !
|
\---src
        testcv.cpp

2. edit CmakeLists.txt (and your code):

your code depends on opencv_core, opencv_imgcodecs, and opencv_highgui, so put the dependancies there:

set(the_description "my fancy cvtest")

ocv_define_module(cvtest opencv_imgproc opencv_core opencv_highgui WRAP python)

.

//testcv.hpp:

#include <opencv2/opencv.hpp>
namespace cv { // put it into cv2 namespace
// if you add your own namespace (inside cv2) here, it will be: cv2.myspace.TestCV() later

CV_EXPORTS_W class TestCV
{
public:
    CV_WRAP TestCV();
    CV_WRAP void TakeALook();
};
}

.

//testcv.cpp

#include "testcv.hpp"
using namespace std;
using namespace cv;

TestCV::TestCV()
{

}
void TestCV::TakeALook()
{
    Mat src=imread("a.jpg");
    imshow("see!",src);
    waitKey(0);
}

3. build the opencv libs & python wrappers as usual:

(along with your own code, now !)

https://docs.opencv.org/master/d7/d9f...

cmake <lots of args> <opencv_src>

make && make install

4. use your code:

import cv2

t = cv2.testCV() # it's inside cv2 now!
t.TakeALook()
edit flag offensive delete link more

Comments

write cmake file as:

set(the_description "my fancy cvtest")

ocv_define_module(cvtest opencv_imgproc opencv_core opencv_highgui WRAP python)

include_directories(include/opencv2 src)

and it works good. However I expected it would be a version controll disasters if really put something in the python cv module.

gino0717 gravatar imagegino0717 ( 2019-02-11 01:14:04 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-02-01 03:31:55 -0600

Seen: 536 times

Last updated: Feb 01 '19