使用Cython包装具有OpenCV参数的C ++函数

时间:2015-04-11 20:33:14

标签: python c++ opencv cython

我有以下用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] *'

1 个答案:

答案 0 :(得分:2)

不幸的是,这是一个部分答案:我知道问题,但我不知道解决方案。

你的第二个问题很容易解决。问题是它需要一个指向c ++向量的指针,但它得到一个python列表。 Cython可以自动将列表转换为向量并返回,但在指针处丢失。

 # put this at the of your file top, and remove
 # cdef extern from "<vector>" namespace "std": (etc)
 #
 # Cython has automatically defined these!
 from libcpp.vector cimport vector

 # ... other code goes here ...

 # accept a list
 def SegmentVideo(self, VideoCapture *vc, list indices):
    cdef vector[Segment] indices_vector = indices # autoconversion happens
    output = self.thisptr.SegmentVideo(vc, &indices_vector)
    # are you expecting the vector to be changed, and do you want to keep the changes?
    # if so then do
    indices[:] = indices_vector
    # if you don't care about changes then don't bother
    return output

你的第一个问题(Cannot convert Python object argument to type 'VideoCapture *')是我无法解决的问题。本质上,cv2模块生成python对象(可能是围绕OpenCV中定义的C ++指针的包装器),并且没有一种明显的方法可以从这些python对象生成C ++指针 - 您必须弄清楚它存储在何处。

我不会完全重复的快速推文表明我的系统上的VideoCapture python对象是32个字节(空的Python对象是16个字节)。但是,2个额外的8字节空格的内容对我来说都不像“指针”。

大多数OpenCV python包装器看起来都是从C ++代码自生成的(至少据我所知),这意味着要弄清楚它是如何包装的并不是特别容易。一种可能有用的方法是尝试通过代码生成器运行UserDivide,看看你是否可以通过这种方式为它生成python绑定....