我的目标是实时运行TensorFlow模型,从学习模型中控制车辆.我们的车辆系统使用与OpenCV紧密相连的ROS(机器人操作系统).所以,我收到一个包含ROS感兴趣图像的OpenCV Mat.
cv::Mat cameraImg;
Run Code Online (Sandbox Code Playgroud)
我想直接从这个OpenCV矩阵中的数据创建一个Tensorflow Tensor,以避免逐行复制矩阵的费用.使用本课题的答案我已设法使用以下代码获得网络的正向传递:
cameraImg.convertTo(cameraImg, CV_32FC3);
Tensor inputImg(DT_FLOAT, TensorShape({1,inputheight,inputwidth,3}));
auto inputImageMapped = inputImg.tensor<float, 4>();
auto start = std::chrono::system_clock::now();
//Copy all the data over
for (int y = 0; y < inputheight; ++y) {
const float* source_row = ((float*)cameraImg.data) + (y * inputwidth * 3);
for (int x = 0; x < inputwidth; ++x) {
const float* source_pixel = source_row + (x * 3);
inputImageMapped(0, y, x, 0) = source_pixel[2];
inputImageMapped(0, y, x, 1) = source_pixel[1];
inputImageMapped(0, …Run Code Online (Sandbox Code Playgroud)