How to get a python .pyd for Windows from c/c++ source code? (update: brisk now in Python in case that's what you want)

后端 未结 2 412
萌比男神i
萌比男神i 2021-02-08 10:25

How to get from C/C++ extension source code to a pyd file for windows (or other item that I could import to Python)?

edit: The specific libra

2条回答
  •  情话喂你
    2021-02-08 11:09

    In case someone needs it, this what I have so far. Basically a BriskFeatureDetector that can be created in Python and then have detect called. Most of this is just confirming/copying what obmarg showed me, but I have added the details that get all the way to the pyd library.

    The detect method is still incomplete for me though since it does not convert data types. Anyone who knows a good way to improve this, please do! I did find, for example, this library which seems to convert a numpy ndarray to a cv::Mat, but I don't have the time to figure out how to integrate it now. There are also other data types that need to be converted.

    Install OpenCV 2.2

    • for the setup below, I installed to C:\opencv2.2
    • Something about the API or implementation has changed by version 2.4 that gave me problems (maybe the new Algorithm object?) so I stuck with 2.2 which BRISK was developed with.

    Install Boost with Boost Python

    • for the setup below, I installed to C:\boost\boost_1_47

    Create a Visual Studio 10 Project:

    • new project --> win32
    • for the setup below, I named it brisk
    • next --> DLL application type; empty project --> finished
    • at the top, change from Debug Win32 to Release Win32

    Create main.cpp in Source Files

    Do this before the project settings so the C++ options become available in the project settings

    #include 
    #include 
    #include 
    
    BOOST_PYTHON_MODULE(brisk)
    {
        using namespace boost::python;
    
        //this long mess is the only way I could get the overloaded signatures to be accepted
        void (cv::BriskFeatureDetector::*detect_1)(const cv::Mat&,
                    std::vector>&,
                    const cv::Mat&) const
                    = &cv::BriskFeatureDetector::detect;
        void (cv::BriskFeatureDetector::*detect_vector)(const std::vector>&,
                    std::vector< std::vector< cv::KeyPoint, std::allocator>, std::allocator< std::vector>>>&,
                    const std::vector>&) const
                    = &cv::BriskFeatureDetector::detect;
    
        class_< cv::BriskFeatureDetector >( "BriskFeatureDetector", init())
            .def( "detect", detect_1)
        ;
    }
    

    Project Settings (right-click on the project --> properties):

    1. Includes / Headers

      • Configuration Properties --> C/C++ --> General
      • add to Additional Include Directories (adjust to your own python / brisk / etc. base paths):

        C:\opencv2.2\include;

        C:\boost\boost_1_47;

        C:\brisk\include;C:\brisk\thirdparty\agast\include;

        C:\python27\include;

    2. Libraries (linker)

      • Configuration Properties --> Linker --> General
      • add to Additional Library Directories (adjust to your own python / brisk / etc. base paths):

        C:\opencv2.2\lib;

        C:\boost\boost_1_47\lib;

        C:\brisk\win32\lib;

        C:\python27\Libs;

      • Configuration Properties --> Linker --> Input

      • add to Additional Dependencies (adjust to your own python / brisk / etc. base paths):

        opencv_imgproc220.lib;opencv_core220.lib;opencv_features2d220.lib;

        agast_static.lib; brisk_static.lib;

        python27.lib;

    3. .pyd output instead of .dll

      • Configuration Properties --> General
      • change Target Extension to .pyd

    Build and rename if necessary

    • Right-click on the solution and build/rebuild
    • you may need to rename the output from "Brisk.pyd" to "brisk.pyd" or else python will give you errors about not being able to load the DLL
    • Make brisk.pyd available to python by putting it in site packages or by putting a .pth file that links to its path

    Update Path environment variable

    • In windows settings, make sure the following are included in your path (again, adjust to your paths):

      `C:\boost\boost_1_47\lib;C:\brisk\win32\bin`
      

提交回复
热议问题