Aufruf von C ++ - Code aus Python mit Cython nach dem distutilis-Ansatz
Ich versuche, einen C ++ - Code aus einem Python-Skript mit Cython aufzurufen. Ich habe es bereits geschafft, mit einem Beispiel von zu arbeitenHier aber das ding ist: mein c ++ code enthält nicht standardisierte bibliotheken von opencv. Ich glaube, ich verbinde sie nicht richtig, also brauche ich jemanden, der meine anschautsetup.py und meincpp_rect.h undcpp_rect.cpp Dateien.
Der Fehler, den ich erhalte, bezieht sich auf die fette Linie in der * .cpp-Datei:cv :: Mat img1 (7,7, CV_32FC2, Skalar (1,3)); Wenn ich versuche, die Bibliothek zu testen, wird beim Ausführen ein Include-Fehler angezeigt$ python userect.py
:
Traceback (most recent call last):
File "userect.py", line 2, in <module>
from rectangle import Rectangle
ImportError: dlopen(/Users/marcelosalloum/Desktop/python_cpp_interface/rectangle.so, 2): Symbol not found: __ZN2cv3Mat10deallocateEv
Referenced from: /Users/marcelosalloum/Desktop/python_cpp_interface/rectangle.so
Expected in: flat namespace
in /Users/marcelosalloum/Desktop/python_cpp_interface/rectangle.so
Das nicht gefundene Symbol (__ZN2cv3Mat10deallocateEv) hat etwas mit dem zu tuncv::Mat::deallocate()
Funktion, was darauf hinweist, dass meine Importe nicht richtig funktionieren.
Irgendwelche Ideen?
Meine anderen Klassen sind die folgenden:
Das ist meinsetup.py Datei. Beachten Sie, dass ich bereits 2 Verzeichnisse eingebunden habe, obwohl ich nicht sicher bin, ob ich es richtig gemacht habe:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
name = 'Demos',
ext_modules=[
Extension("rectangle",
sources=["rectangle.pyx", "cpp_rect.cpp"], # Note, you can link against a c++ library instead of including the source
include_dirs=[".", "/usr/local/include/opencv/", "/usr/local/include/"],
language="c++"),
],
cmdclass = {'build_ext': build_ext},
)
Meinecpp_rect.h Die Datei enthält eine cv.h und einen Namespace cv, wie unten gezeigt:
#include "source/AntiShake.h"
#include <iostream>
#include "cv.h"
using namespace cv;
class Rectangle {
public:
int x0, y0, x1, y1;
Rectangle();
Rectangle(int x0, int y0, int x1, int y1);
~Rectangle();
int getLength();
int getHeight();
int getArea();
void move(int dx, int dy);
**void openCV();**
Rectangle operator+(const Rectangle& other);
};
und meine openCV () - Funktion instanziiert einfach ein cv :: Mat von opencv (filecpp_rect.cpp):
#include "cpp_rect.h"
Rectangle::Rectangle() {
x0 = y0 = x1 = y1 = 0;
}
Rectangle::Rectangle(int a, int b, int c, int d) {
x0 = a;
y0 = b;
x1 = c;
y1 = d;
}
Rectangle::~Rectangle() {
}
void Rectangle::openCV(){
**cv::Mat img1(7,7,CV_32FC2,Scalar(1,3));**
}
...
Ich kann die Datei mit folgendem Befehl kompilieren:$ python setup.py build_ext --inplace
, was mir die * .so Datei liefert. Wenn ich jedoch mein Skript userect.py ausführe, wird der zuerst in dieser Frage beschriebene Include-Fehler angezeigt.
Irgendwelche Ideen?