Skip to content

Intra-process image pipeline example - #12

Merged
wjwwood merged 3 commits into
masterfrom
intra_process_img
Aug 28, 2015
Merged

wjwwood merged 3 commits into
masterfrom
intra_process_img

Conversation

@jackieokay

Copy link
Copy Markdown
Contributor

This is a first pass at an example that exercises intra-process communication for an image processing pipeline.

Adds:

  • img_pipeline.cpp, a simple main class which loads the nodes into one SingleThreadedExecutor and spins.
  • utils.hpp, contains Mat <-> ROS Image.msg conversion functions.
  • 4 subclasses of Node which execute intra-process comms by default:
    • DeviceNode, which publishes a video stream to a rostopic.
    • ExtractorNode, which performs a read-only operation on the image it receives (histogram calculation) and republishes the message on a new topic.
    • ModifierNode, which writes to the image (color inversion) and publishes the modified message on a new topic.
    • VisualizerNode, which shows the modified image in an OpenCV window.

Todo:

  • Terminal output that shows that intra-process communication is being exercised (e.g. print out pointer values for messages).
  • Write more main examples exercising different process compositions, or integrate with class_loader.
  • Copy performance issue: currently, convert_message_to_frame performs a zero-copy conversion from ROS message to OpenCV Mat "frame" by assigning to Mat data pointer to the start of the ROS Image data vector. However, convert_frame_to_message copies the Mat data into the ROS Image data vector, because I couldn't figure out how to directly assign the pointer to the underlying data of a vector (and I don't think this is possible).
  • Framerate is fairly low right now; publishing at higher than ~33Hz causes a race condition similar to Implement subscription side filtering when intra process is being used ros2#91 where the publishers for the other nodes do not get initialized before the executor starts spinning.

@jackieokay jackieokay added the in progress Actively being worked on (Kanban column) label Aug 25, 2015
@jackieokay jackieokay self-assigned this Aug 25, 2015
@esteve

esteve commented Aug 26, 2015

Copy link
Copy Markdown
Member

I found this bug in OpenCV on Windows (see #13):

http://stackoverflow.com/questions/20854682/opencv-multiple-unwanted-window-with-garbage-name

replacing cv::Mat with CvMat (and imshow and waitKey with cvShowImage and cvWaitKey, respectively) should do the trick.

Comment thread intra_process_comms/CMakeLists.txt Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alpha order.

Same below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k. I'll keep OpenCV in its own category instead of sorting it into alphabetical order, since it's not a ROS package (I assume that's our convention based on https://github.com/ros2/demos/blob/master/image_tools/CMakeLists.txt).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. With same below I referred to line 33.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would the next node in the pipeline subscribe to this relayed topic rather then the original once?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wjwwood suggested that the read-only node republishes the message when we discussed the design of this demo on Monday, though looking at the code now I'm not sure why this is better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prevents copying of the message since there are never more than one subscription for each publisher.

@wjwwood

wjwwood commented Aug 27, 2015

Copy link
Copy Markdown
Member

Ok I updated this pr with a new example: https://github.com/ros2/demos/pull/12/files#diff-9c1f8132951cbb58bc4bbd4f97b870a6

Required changes in rclcpp, rmw, and system_tests to follow.

@wjwwood

wjwwood commented Aug 27, 2015

Copy link
Copy Markdown
Member

This is an example of the output from the above linked new example:

% ./install/bin/two_node_pipeline
Published message with value: 0, and address: 0x7fc5d5024cb0
Published message with value: 1, and address: 0x7fc5d2e40b10
 Received message with value: 1, and address: 0x7fc5d2e40b10
Published message with value: 2, and address: 0x7fc5d2d3acb0
 Received message with value: 2, and address: 0x7fc5d2d3acb0
Published message with value: 3, and address: 0x7fc5d5011a10
 Received message with value: 3, and address: 0x7fc5d5011a10
Published message with value: 4, and address: 0x7fc5d5011a10
 Received message with value: 4, and address: 0x7fc5d5011a10
Published message with value: 5, and address: 0x7fc5d5011a10
 Received message with value: 5, and address: 0x7fc5d5011a10
Published message with value: 6, and address: 0x7fc5d2d3acb0
 Received message with value: 6, and address: 0x7fc5d2d3acb0
Published message with value: 7, and address: 0x7fc5d5011a10
 Received message with value: 7, and address: 0x7fc5d5011a10
Published message with value: 8, and address: 0x7fc5d5011a10
 Received message with value: 8, and address: 0x7fc5d5011a10
Published message with value: 9, and address: 0x7fc5d5011a10
 Received message with value: 9, and address: 0x7fc5d5011a10
Published message with value: 10, and address: 0x7fc5d5011a10
 Received message with value: 10, and address: 0x7fc5d5011a10
...

@wjwwood

wjwwood commented Aug 27, 2015

Copy link
Copy Markdown
Member

Ok, so I've got a pretty nice demo going now. See the files in:

https://github.com/ros2/demos/tree/e9e70bfce41eae53c033d29d49a47df6d1e86224/intra_process_comms/src/image_pipeline

This is just a refactoring/reorganization of what @jacquelinekay did (Thanks for that btw, gave me a huge head start on the CV stuff).

So, now if you run image_pipeline you'll get a Window with the image annotated with text. Like this:

screen shot 2015-08-27 at 1 28 16 am

The demo is made up of three nodes at the moment: camera_node -> watermark_node -> image_view_node. Each of the nodes write the address of the pointer they got or are publishing. The watermark node additionally writes some other text. You can press space bar to pause/unpause and you can press q or ESC to stop it.

I managed to find a way to create a cv::Mat and operate on it without copying the data out of the message. So the only copying is when I copy the data from the captured image into the original ros message.

I've also added two other toy problems. I still have to write some of the tutorial, but I'll hopefully be able to knock that out quick now that I've got some of the demo done.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the input strings as const references.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wjwwood wjwwood added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Aug 27, 2015

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give some justification for using std::thread instead of create_wall_timer for publishing this message? My guess is that you don't want the image capture to run in the same thread as the executor, but you also don't want to use a MultiThreadedExecutor because we haven't vetted the intra-process stuff for thread safety yet.

Also, I noticed you don't sleep in the loop function. Does OpenCV block when we get a new frame? Or is this a busy while loop?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used a thread because I want the loop to run continuously (without a sleep), so the timer isn't well suited for that. OpenCV will block when you call frame_ << cap_;.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other idea I had was to add an "on_idle" callback list for the executor, which is similar to other event loop systems:

http://doc.qt.io/qt-4.8/qtimer.html#details

As a special case, a QTimer with a timeout of 0 will time out as soon as all the events in the window system's event queue have been processed. This can be used to do heavy work while providing a snappy user interface:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the mechanism for prioritizing queued work in the Executor is a bit inflexible right now, or should I say non-existent. (related: ros2/rclcpp/issues/76) Another feature to push on after the release...

@jackieokay

Copy link
Copy Markdown
Contributor Author

With regards to avoiding the bug Esteve found on Windows, I think you can convert to a CvMat without incurring copy using this constructor (it just assigns a data pointer without allocating new memory):

https://github.com/Itseez/opencv/blob/2.4/modules/core/include/opencv2/core/types_c.h#L729

you can do this just before calling CvShowImage.

@jackieokay jackieokay mentioned this pull request Aug 27, 2015
@esteve

esteve commented Aug 28, 2015

Copy link
Copy Markdown
Member

@wjwwood
wjwwood force-pushed the intra_process_img branch from a0bfd52 to b70baf3 Compare August 28, 2015 05:49
@wjwwood

wjwwood commented Aug 28, 2015

Copy link
Copy Markdown
Member

You guys are killing me... rebased again.

@dirk-thomas

Copy link
Copy Markdown
Member

None of the classes is in a proper namespace following the package name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nodes should not have the same name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the node name, and so they have different names.

I reverted the change in 58f7339.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it is actually the topic name. That is unexpected to change the constructor argument semantic in a subclass. Good that you catched it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, It's not very nice, but at the time I thought it was unnecessary to additionally pass the node name.

@esteve
esteve force-pushed the intra_process_img branch from b70baf3 to f38c978 Compare August 28, 2015 17:07
@esteve

esteve commented Aug 28, 2015

Copy link
Copy Markdown
Member

I just rebased this and updated with feedback from @dirk-thomas. The non-rebased version of the branch can be found at https://github.com/ros2/demos/tree/intra_process_img-prerebase if you need it @wjwwood

@wjwwood
wjwwood force-pushed the intra_process_img branch from 58f7339 to 983c98d Compare August 28, 2015 19:20
@wjwwood

wjwwood commented Aug 28, 2015

Copy link
Copy Markdown
Member

Ok, other than the flaky tests and a few compiler warnings on Windows (which I fixed and tested manually) this looks good to go.

wjwwood added a commit that referenced this pull request Aug 28, 2015
Intra-process image pipeline example
@wjwwood
wjwwood merged commit 1fead1c into master Aug 28, 2015
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Aug 28, 2015
@wjwwood
wjwwood deleted the intra_process_img branch August 28, 2015 20:11
@dirk-thomas

Copy link
Copy Markdown
Member

Fixing windows warnings in #20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants