Search code examples
pythonc++opencvtensorflowboost-python

How can I transform from cv::Mat (in C++) to tf.placeholder(in C++ Boost.Python)?


I am studying about C++ BoostPython, But I have one problem about transforming Image data type.

  1. Receive DepthImage from Intel Realsense camera SR300.
  2. depthImage data type = cv::Mat(this is imageformat of opencv in C++)
  3. I want to put this Image of cv::Mat type in the tf.placeholder of Boost.Python, C++.

How can I do that? Please...


Solution

  • the follow code could work, i am not sure this is the best approach, but this is how i managed to get the the tensroflow working for me.

    void* object = (void*)resized.ptr() ;
    int64_t* dims; //set dims
    //C API
    TF_Tensor* tftensor = TF_NewTensor(TF_DataType::TF_FLOAT, dims, nDims, object, data_size, &deallocator, 0);
    
    //C++ API
    tensorflow::Tensor input_tensor(tensorflow::DT_FLOAT, tensorflow::TensorShape({ batch_size, objectHeight, objectWidth, objectChannels }));
    int data_size = objectHeight * objectWidth * objectChannels * batch_size * sizeof(float);
    std::copy_n((float*)object, data_size, (input_tensor.flat<float>()).data());