【发布时间】:2015-06-17 10:21:03
【问题描述】:
我有以下用 C++ 编写的类
#include "segmenter_interface.h"
#include "video_marker.cpp"
#include <opencv2/imgcodecs.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
class UserDivide : public SegmenterInterface{
public:
virtual void SegmentVideo(cv::VideoCapture *vc,
std::vector<Segment> *indices);
}
实施细节并不重要。理想情况下,我想使用 Cython 将这个类公开给 python。对象 VideoCapture 已经可以被 python 实例化,因为 OpenCV 已经包装了它的所有模块。根据我的阅读,vector 已经是 Cython 的一部分,因为它支持大多数 C++ 标准库。
目前,我写了这么多.pyx:
cdef extern from "<vector>" namespace "std":
cdef cppclass vector [T]:
pass
cdef extern from "<opencv2/videoio/videoio.hpp>" namespace "cv":
cdef cppclass VideoCapture:
pass # Don't care, just need a pointer
cdef extern from "segmenter_interface.h":
cdef struct Segment:
pass # I will need this eventually...
cdef extern from "user_divide.h":
cdef cppclass UserDivide:
UserDivide()
void SegmentVideo(VideoCapture *vc, vector[Segment] *indices)
cdef class PyUserDivide:
cdef UserDivide *thisptr # hold a C++ instance
def __cinit__(self):
self.thisptr = new UserDivide()
def __dealloc__(self):
del self.thisptr
def SegmentVideo(self, VideoCapture *vc, vector[Segment] *indices):
return self.thisptr.SegmentVideo(vc, indices)
问题在于 SegmentVideo 中的参数——cython 编译器抱怨转换它们。
我的问题是:如何在 Cython 中正确包装对象指针,特别是如果它不是标准数据类型或结构,而是一个类?我是否采取了正确的方法?
我的目标是在 Python 中执行以下操作
import cv2
cap = cv2.VideoCapture("my_video.mp4")
segments = []
ud = UserDivide()
ud.SegmentVideo(cap, segments)
# Do stuff with segments
错误信息如下:
Error compiling Cython file:
------------------------------------------------------------
...
cdef UserDivide *thisptr # hold a C++ instance
def __cinit__(self):
self.thisptr = new UserDivide()
def __dealloc__(self):
del self.thisptr
def SegmentVideo(self, VideoCapture *vc, vector[Segment] *indices):
^
------------------------------------------------------------
pyuser_divide.pyx:22:27: Cannot convert Python object argument to type 'VideoCapture *'
Error compiling Cython file:
------------------------------------------------------------
...
cdef UserDivide *thisptr # hold a C++ instance
def __cinit__(self):
self.thisptr = new UserDivide()
def __dealloc__(self):
del self.thisptr
def SegmentVideo(self, VideoCapture *vc, vector[Segment] *indices):
^
------------------------------------------------------------
pyuser_divide.pyx:22:45: Cannot convert Python object argument to type 'vector[Segment] *'
【问题讨论】:
-
您能否提供 Cython 报告的确切错误消息?