linux pybind11 python c++ 混合编程 opencv(暂记)

linux pybind11 python c++ 混合编程 opencv(暂记)_第1张图片

linux pybind11 python c++ 混合编程 opencv(暂记)_第2张图片

simple

cmake_minimum_required(VERSION 2.8.12)
project(example)
#set(PYBIND11_PYTHON_VERSION "3.7")

#include_directories("/usr/include/python3.10/")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
#find_package(OpenCV)
#include_directories(${OpenCV})
include_directories("/home/pdd/anaconda3/envs/yolocopy/bin/")


#link_libraries(${OpenCV_LIBS})
#target_link_libraries(example ${OpenCV_LIBS})
add_subdirectory(pybind11)
pybind11_add_module(example example.cpp)
#add_executable(untitled example.cpp)

#target_link_libraries(${PROJECT_NAME} INTERFACE
#        ${OpenCV_LIBRARIES}
##        pybind11::pybind11
#        )
target_link_libraries(${PROJECT_NAME} PRIVATE
       ${OpenCV_LIBRARIES}
        pybind11::pybind11
        )
#link_libraries(${OpenCV_LIBRARIES})
#include 
#include 
#include 
//#include 
#include 
using namespace std;
using namespace cv;
// ----------------
// Regular C++ code
// ----------------

// multiply all entries by 2.0
// input:  std::vector ([...]) (read only)
// output: std::vector ([...]) (new copy)
std::vector modify(const std::vector& input)
{
  std::vector output;

  std::transform(
    input.begin(),
    input.end(),
    std::back_inserter(output),
    [](double x) -> double { return 2.*x; }
  );
   //N.B. this is equivalent to (but there are also other ways to do the same)
   for ( size_t i = 0 ; i < input.size() ; ++i )
     output[i] = 2. * input[i];
  return output;
}


std::vector OPticalFlowGpu(const std::string& input)
{


    string path = "/home/pdd/Downloads/a.jpeg";
    Mat src = imread(path);
    std::vector output={1,(double)src.rows};
    return output;
}

// ----------------
// Python interface
// ----------------

namespace py = pybind11;

PYBIND11_MODULE(example,m)
{
  m.doc() = "pybind11 example plugin   lalallalallalallalalallalalal";

  m.def("modify", &modify, "Multiply all entries of a list by 2.0");
  m.def("OPticalFlowGpu", &OPticalFlowGpu);


}

advance

  • 有时调用opencv中的某些函数可能报错 > - Expected Ptrcv::UMat for argument ‘contour’,但是将输入转为numpy就可以运行了,就可能是以下的原理了
import example

A = [1.,2.,3.1,4.]

B = example.modify(A)
C = example.OPticalFlowGpu("")
D = example.opflowithmean(True,"http://223.151.51.27:85//tsfile/live/0013_1.m3u8?key=txiptv&playlive=1&authid=0",True,)  # (usestream ,streamname ,savepic)
print(B)
print(C)
print(D)



print("-"*60)
import time
import cv2
camera = cv2.VideoCapture("http://223.151.51.27:85//tsfile/live/0013_1.m3u8?key=txiptv&playlive=1&authid=0")
ret, frame1 = camera.read()
ret, frame2 = camera.read()

t1 = time.time()
E = example.opflow(frame1,frame2,True)
t2 = time.time()
print(t2-t1)
print(E)
#include 
#include 
#include 
#include 
#include 
using namespace std;
using namespace cv;
#include 

#include 

#include 

#include 

#include "opencv2/core.hpp"

#include "opencv2/core/utility.hpp"

#include "opencv2/highgui.hpp"

#include "opencv2/video.hpp"

#include "opencv2/cudaoptflow.hpp"

#include "opencv2/cudaarithm.hpp"

using namespace std;

using namespace cv;

using namespace cv::cuda;


std::vector modify(const std::vector& input)
{
  std::vector output={0,0};
  return output;
}

/*
 * define:
 * */
float opflowithmean(bool usestream , string streamname , bool savepic){
    GpuMat d_flow;

    Ptr d_calc = cuda::FarnebackOpticalFlow::create();

    Mat flowxy, flowx, flowy, image;

    bool running = true, gpuMode = true;

    int64 t, t0=0, t1=1, tc0, tc1;
    VideoCapture capture;
    if (usestream){
        capture.open(  streamname);
        // https://blog.csdn.net/wenhao_ir/article/details/123140718
        //VideoCapture capture(samples::findFile("/home/pdd/Documents/yolov5_fire/data/images/output.avi"));//VideoCapture capture(samples::findFile("/home/pdd/CLionProjects/denseFlow_gpu-master/ex.avi"));//https://docs.opencv.org/4.x/d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13
    }else{
        capture.open(streamname);
    }


    if (gpuMode)
    {
            cout << "Use GPU accer\n";
            if (!capture.isOpened()) {
                //error in opening the video input
                cerr << "Unable to open file!" << endl;
                return 0;
            }
            Mat frame1, grayframe1;// https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html
            Mat _frame1;
            capture >> _frame1;
            cv::resize(_frame1, frame1, cv::Size(640, 480), 0, 0, cv::INTER_AREA);
            cvtColor(frame1,grayframe1, COLOR_BGR2GRAY);
            GpuMat d_frameL(grayframe1);//skip frame for(int i =0;i<3;i++){  capture >> _frame1;   }

            Mat frame2, grayframe2;
            Mat _frame2;
            capture >> _frame2;
            cv::resize(_frame2, frame2, cv::Size(640, 480), 0, 0, cv::INTER_AREA);
            cvtColor(frame2,grayframe2, COLOR_BGR2GRAY);
            GpuMat d_frameR(grayframe2);





            tc0 = getTickCount();
            d_calc->calc(d_frameL, d_frameR, d_flow);
            tc1 = getTickCount();

            GpuMat planes[2];
            cuda::split(d_flow, planes);
            planes[0].download(flowx);
            cv::Scalar neam=cv::mean(flowx);
            float MyMeanValue = neam.val[0];//.val[0]表示第一个通道的均值
            planes[1].download(flowy);

            if(savepic){imwrite( std :: to_string(tc0)+"a.jpg",frame1);}
            return MyMeanValue;
    }else{
            cout<< "no cuda support !!!"<
#include
#include

namespace py = pybind11;
cv::Mat numpy_uint8_3c_to_cv_mat(py::array_t& input) { // https://www.jianshu.com/p/be16847b0b74

    if (input.ndim() != 3)
        throw std::runtime_error("3-channel image must be 3 dims ");

    py::buffer_info buf = input.request();

    cv::Mat mat(buf.shape[0], buf.shape[1], CV_8UC3, (unsigned char*)buf.ptr);

    return mat;
}


float opflow(py::array_t& input1  , py::array_t& input2 ,bool savepic){
    cv::Mat _frame1 = numpy_uint8_3c_to_cv_mat(input1);
    cv::Mat _frame2 = numpy_uint8_3c_to_cv_mat(input2);
    GpuMat d_flow;
    Ptr d_calc = cuda::FarnebackOpticalFlow::create();

    Mat flowxy, flowx, flowy, image;

    bool running = true, gpuMode = true;

    int64 t, t0=0, t1=1, tc0, tc1;



    if (gpuMode)
    {
        Mat frame1, grayframe1;// https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html
        cv::resize(_frame1, frame1, cv::Size(640, 480), 0, 0, cv::INTER_AREA);
        cvtColor(frame1,grayframe1, COLOR_BGR2GRAY);
        GpuMat d_frameL(grayframe1);//skip frame for(int i =0;i<3;i++){  capture >> _frame1;   }

        Mat frame2, grayframe2;
        cv::resize(_frame2, frame2, cv::Size(640, 480), 0, 0, cv::INTER_AREA);
        cvtColor(frame2,grayframe2, COLOR_BGR2GRAY);
        GpuMat d_frameR(grayframe2);


        tc0 = getTickCount();
        d_calc->calc(d_frameL, d_frameR, d_flow);
        tc1 = getTickCount();

        GpuMat planes[2];
        cuda::split(d_flow, planes);
        planes[0].download(flowx);
        cv::Scalar neam=cv::mean(flowx);
        float MyMeanValue = neam.val[0];//.val[0]表示第一个通道的均值
        planes[1].download(flowy);

        if(savepic){imwrite( std :: to_string(tc0)+"a.jpg",frame1);}
        return MyMeanValue;
    }else{
        cout<< "no cuda support !!!"< OPticalFlowGpu(const std::string& input)
{


    string path = "/home/pdd/Downloads/a.jpeg";
    Mat src = imread(path);
    cout<< (double)src.rows ;
    std::vector output={1,(double)src.rows};
    return output;
}



namespace py = pybind11;

PYBIND11_MODULE(example,m)
{
  m.doc() = "pybind11 example plugin   lalallalallalallalalallalalal";
  m.def("modify", &modify, "Multiply all entries of a list by 2.0");
  m.def("OPticalFlowGpu", &OPticalFlowGpu);
  m.def("opflowithmean", &opflowithmean);
    m.def("opflow", &opflow);
}


cg

  • ament_export_include_directories(

  • import example
    
  • ImportError: /home/pdd/PycharmProjects/aaa/example.so: undefined symbol: _ZN2cv3MatD1Ev

  • https://github.com/lihui-colin/pybind11_opencv

    The keyword signature for target_link_libraries has already been used with
    the target “example”. All uses of target_link_libraries with a target must
    be either all-keyword or all-plain.
    The uses of the keyword signature are here:

  • https://github.com/pthom/cvnp

  • An imagenet dataloader implemented with opencv and pybind11 based on multi-threading

  • https://learnopencv.com/getting-started-opencv-cuda-module/

你可能感兴趣的:(语言学习笔记,opencv,python,linux)