Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

run mxnet-cpp in ROS, got frozen after calling SyncCopyFromCPU for converting cv::Mat to NDArray #14847

Closed
hallazie opened this issue Apr 30, 2019 · 4 comments
Labels
C API C++ Related to C++ OpenCV OpenCV related issues

Comments

@hallazie
Copy link

Description

I'm running mxnet-c++ under a ROS node for object detection, which takes a sequence of images as input by subscribing a image message.
The node got stuck when I'm trying to construct input NDArray from cv::Mat data, either by

NDArray image_data(mat.ptr<float>(), input_shape, global_ctx);

or

NDArray image_data = NDArray(input_shape, global_ctx, true);
image_data.SyncCopyFromCPU(mat.ptr<float>(), input_shape.Size());
image_data.WaitToRead();

Environment info (Required)

----------Python Info----------
('Version :', '2.7.15rc1')
('Compiler :', 'GCC 7.3.0')
('Build :', ('default', 'Nov 12 2018 14:31:15'))
('Arch :', ('64bit', ''))
------------Pip Info-----------
('Version :', '19.0.3')
('Directory :', '/usr/local/lib/python2.7/dist-packages/pip')
----------MXNet Info-----------
('Version :', '1.4.0')
('Directory :', '/usr/local/lib/python2.7/dist-packages/mxnet')
('Commit Hash :', 'a03d59ed867ba334d78d61246a1090cd1868f5da')
----------System Info----------
('Platform :', 'Linux-4.18.0-17-generic-x86_64-with-Ubuntu-18.04-bionic')
('system :', 'Linux')
('node :', 'xsh-ThinkPad-T480')
('release :', '4.18.0-17-generic')
('version :', '#18~18.04.1-Ubuntu SMP Fri Mar 15 15:27:12 UTC 2019')

Package used (Python/R/Scala/Julia):
c++

Build info (Required if built from source)

Compiler (gcc/clang/mingw/visual studio):
g++

MXNet commit hash:
b3b952f

Minimum reproducible example

// ROS node callback
void YoloNode::call_back(const sensor_msgs::Image::ConstPtr& msg_image){
	cv_bridge::CvImageConstPtr cv_ptrdepth;
	cv::Mat mat_depth_f32;
	try{
		cv_ptrdepth = cv_bridge::toCvShare(msg_image);
		mat_depth_f32 = cv_ptrdepth->image;
// mxnet code part: get_rect(mat_depth_f32) takes a cv::Mat in and run a yolo-like model to detect a single target bounding box
		std::vector<int> rect = detector.get_rect(mat_depth_f32);
		printf("[DEBUG] current bbox is [%d, %d : %d, %d]\n", rect[0], rect[1], rect[2], rect[3]);
		cv::rectangle(mat_depth_f32, cv::Rect(rect[0], rect[1], 80, 20), cv::Scalar(125, 220, 5), 1, 1, 0);
		cv::namedWindow("cv_check", 0);
		cv::imshow("cv_check", mat_depth_f32);
		cv::waitKey(1);
	}catch(cv_bridge::Exception& e){
		std::cout << "[ERROR] " << e.what() << std::endl;
		return;
	}
}

// detector part which convert cv::Mat to NDArray
NDArray Detector::cvmat_2_ndarray(cv::Mat mat) {
	std::vector<mx_float> array;
	for (int i = 0; i < 172; ++i) {
		for (int j = 0; j < 224; ++j) {
			mx_float t = static_cast<mx_float>(mat.data[i * 224 + j]);
			array.push_back(t);
		}
	}
	NDArray image_data = NDArray(input_shape, global_ctx, true);
// stucked here
	image_data.SyncCopyFromCPU(array.data(), input_shape.Size());
	image_data.WaitToRead();
	return image_data;
}
@mxnet-label-bot
Copy link
Contributor

Hey, this is the MXNet Label Bot.
Thank you for submitting the issue! I will try and suggest some labels so that the appropriate MXNet community members can help resolve it.
Here are my recommended labels: Bug

@lanking520
Copy link
Member

@hallazie In Scala/Python, there are NDArray.Image API which used C operator to do the image processing. I am not sure why it is hang for using OpenCV alone with MXNet.

@leleamol Do we have CV toolkit available in C?

@lanking520 lanking520 added C API C++ Related to C++ OpenCV OpenCV related issues Question labels May 2, 2019
@hallazie
Copy link
Author

hallazie commented May 3, 2019

@hallazie In Scala/Python, there are NDArray.Image API which used C operator to do the image processing. I am not sure why it is hang for using OpenCV alone with MXNet.

@leleamol Do we have CV toolkit available in C?

converting cv::Mat to NDArray from image file outside ROS node works fine though

@leleamol
Copy link
Contributor

@hallazie @lanking520
We do not have CV toolkit available in Cpp package.

@hallazie hallazie closed this as completed Jun 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C API C++ Related to C++ OpenCV OpenCV related issues
Projects
None yet
Development

No branches or pull requests

4 participants