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

在这里插入图片描述

在这里插入图片描述

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 <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
//#include <iostream>
#include <opencv2/opencv.hpp>
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<double> modify(const std::vector<double>& input)
{
  std::vector<double> 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<double> OPticalFlowGpu(const std::string& input)
{


    string path = "/home/pdd/Downloads/a.jpeg";
    Mat src = imread(path);
    std::vector<double> 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 <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
#include <iostream>

#include <vector>

#include <sstream>

#include <cmath>

#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<double> modify(const std::vector<double>& input)
{
  std::vector<double> output={0,0};
  return output;
}

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

    Ptr<cuda::FarnebackOpticalFlow> 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 !!!"<<endl ;
    }
    return 0;
}
#include <pybind11/numpy.h>
#include<pybind11/pybind11.h>
#include<pybind11/numpy.h>

namespace py = pybind11;
cv::Mat numpy_uint8_3c_to_cv_mat(py::array_t<unsigned char>& 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<unsigned char>& input1  , py::array_t<unsigned char>& 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<cuda::FarnebackOpticalFlow> 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 !!!"<<endl ;
    }
    return 0;
}









std::vector<double> OPticalFlowGpu(const std::string& input)
{


    string path = "/home/pdd/Downloads/a.jpeg";
    Mat src = imread(path);
    cout<< (double)src.rows ;
    std::vector<double> 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值