ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

OpenCV "namedwindow" goes grey, is not getting events

asked 2018-09-24 10:07:30 -0500

pitosalas gravatar image

updated 2018-09-25 11:19:54 -0500

I am experimenting with opencv and ROS and Gazebo. This is a variant of an example I found in the Programming Robots with ROS book. I am running on Ubuntu, with Kinetic.

I run the program below, expecting that the window containing the image from the camera on the robot is displayed. It does do this, but after about 10 seconds, the window greys out. I looked at all the logs I can find and see no smoking gun. I have also used rqt_image_view and it does show the image as expected.

Apparently (thanks @gvdhoorn) the greying is because event handling for the graphical window is stalled. To debug this I've added "print n" lines all over the place. The prints show me I think that the loop in the main program "while(not rospy.is_shutdown()):" is not getting a "turn" after a short while, whereas the messages that are coming in via the subscription are getting processed. It is almost as if they are coming so fast that the main program doesn't get a "turn" and so doesn't display the window over and over again. I thought with the "is_shutdown" the processing of the main and the callback would play nice together. Or maybe the problem is something totally different.

Any idea?

import rospy
import cv2
import sys
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

# Follower callback
class Follower:
    def __init__(self):
        self.cv_img = None
        # Open CV Bridge, and create a window
        self.bridge = CvBridge()
        # subscribe to the topic, and give each image to the callback
        self.image_sub = rospy.Subscriber('camera/rgb/image_raw', Image, self.image_callback)

    # Called once per image received
    def image_callback(self, frame):
        # Convert image to cv2 and display it
        try:
            print 21
            self.cv_img = self.bridge.imgmsg_to_cv2(frame, desired_encoding="bgr8")
            print 23
        except:
            print("Unexpected error:", sys.exc_info()[0])
            raise

def main():
    cv2.namedWindow('Original')
    print 30
    follower = Follower()
    rospy.init_node('follower', anonymous=True)

    while(not rospy.is_shutdown()):
        print 35
        if follower.cv_img is not None:
            print 37
            cv2.imshow('Original', follower.cv_img)
            print 39
        key = cv2.waitKey(3) & 0xFF
        if key == 27:
            break
        print 43
        rospy.spin()

if __name__ == '__main__':
    main()
edit retag flag offensive close merge delete

Comments

2

A grey window does not mean the "process died", it just means that input even handling for that window is (temporarily) stalled. That could be because the process that spawned it has gone, or it could be because of something else.

It's also an Ubuntu UI thing.

gvdhoorn gravatar image gvdhoorn  ( 2018-09-24 10:35:56 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2018-09-25 11:28:34 -0500

pitosalas gravatar image

I figured it out, but I don't know why. The rospy.spin() near the end is the problem. But I don't know why. Is it because I both have a .spin() and a is_shutdown() in rapid succession? I need to understand more about what those two methods do. Any tips?

edit flag offensive delete link more

Comments

1

rospy.spin() is a blocking call. Your while loop does one iteration and then "hangs" on rospy.spin().

None of the other statements in the while loop get executed any longer at that point.

And rospy.spin() isn't even really needed in Python: it's essentially a sleep.

gvdhoorn gravatar image gvdhoorn  ( 2018-09-25 11:37:40 -0500 )edit

Thanks! I thought it was needed to make sure that callbacks get processed. In other words, if I have a while true in my main program, then callbacks and other background work don't happen. Is that what is_shutdown() allowing?

pitosalas gravatar image pitosalas  ( 2018-09-25 12:13:21 -0500 )edit

@pitosalas what you're trying to do here has an equivalent in C++ called ros::spinOnce(). But as @gvdhoorn mentioned you really don't need to do this in python. See q#110336 for more detail.

l4ncelot gravatar image l4ncelot  ( 2018-09-26 01:43:15 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-24 10:07:30 -0500

Seen: 1,141 times

Last updated: Sep 25 '18