Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am committing a newbie mistake somewhere here that I can't seem to figure out. I have the below sample code that does work. The second half is my attempt at getting the video feed from my robot. When launching the code, I get nothing.. not even a window.

#!/usr/bin/env python
import rospy
import cv2
import numpy as np
from cv_bridge import CvBridge, CvBridgeError
from sensor_msgs.msg import Image, CompressedImage
import time
class LineFollower(object):
    def __init__(self):
        rospy.logwarn("Init line Follower")
        self.bridge = CvBridge()       
        self.image_sub = rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, self.camera_callback, queue_size = 1)
        time.sleep(5) 
    def camera_callback(self,data):
            image_np = self.bridge.compressed_imgmsg_to_cv2(data)
        except CvBridgeError as e:
            print(e)
        cv2.imshow("Full Img", image_np)
        cv2.waitKey(1)
    def clean_up(self):
         cv2.destroyAllWindows()
def main():
    rospy.init_node('line_following_node', anonymous=True)
    line_follower_object = LineFollower()
    rate = rospy.Rate(5) #originally 5
    ctrl_c = False
    def shutdownhook():
        # Works better than the rospy.is_shut_down()
        line_follower_object.clean_up()
        rospy.loginfo("shutdown time!")
        ctrl_c = True
    rospy.on_shutdown(shutdownhook)
    while not ctrl_c:
        rate.sleep()
if __name__ == '__main__':
    main()

The code that I am working on is as follows:

#!/usr/bin/env python
import cv2
import numpy as np
from timeit import default_timer as timer
from std_msgs.msg import Float64
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy
import sys
print(sys.version)
print(cv2.__version__)
height = 480
width = 640
global_frame = np.zeros((height,width,3), np.uint8)
def calculate_lane_pose(frame):
    # Display the resulting frame
    cv2.imshow('Frame', frame)
def camera_callback(self,data):
        global_frame = self.CvBridge.compressed_imgmsg_to_cv2(data)
    except CvBridgeError as e:
        print(e)
    height, width, channels = global_frame.shape
    print(height)
    cv2.imshow("Original", global_frame)
def lane_pose_publisher():
    # Set the node name
    rospy.init_node('lane_pose_publisher', anonymous=True)
    rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, queue_size = 1)
    # set rate
    rate = rospy.Rate(1000) # 1000hz
    while (1):
        rate.sleep()
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
    # cap.release()
    cv2.destroyAllWindows()
if __name__ == '__main__':
        lane_pose_publisher()
    except rospy.ROSInterruptException:
                It's printing  print(sys.version) and print(cv2.__version__)  but nothing being outputted from the camera...
– gup08
                Sep 23, 2021 at 3:10
                that means the publisher isn't actually publishing anything; this can also be verified with rostopic echo. Where is the publisher defined and used at?
– BTables
                Sep 23, 2021 at 3:53
                BTables, the rostopic that i'm subscribing to is /camera/image_raw/compressed that is putting out a jpeg format. Again, I believe it's something in my code? The example code works just fine
– gup08
                Sep 23, 2021 at 4:14

Your example code does not set a callback in the call to rospy.Subscriber(). It needs to be defined like this

#!/usr/bin/env python
import cv2
import numpy as np
from timeit import default_timer as timer
from std_msgs.msg import Float64
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge, CvBridgeError
import rospy
import sys
print(sys.version)
print(cv2.__version__)
height = 480
width = 640
global_frame = np.zeros((height,width,3), np.uint8)
def calculate_lane_pose(frame):
    # Display the resulting frame
    cv2.imshow('Frame', frame)
def camera_callback(data):
    bridge = CvBridge() 
        global_frame = bridge.compressed_imgmsg_to_cv2(data)
    except CvBridgeError as e:
        print(e)
    height, width, channels = global_frame.shape
    print(height)
    cv2.imshow("Original", global_frame)
def lane_pose_publisher():
    # Set the node name
    rospy.init_node('lane_pose_publisher', anonymous=True)
    rospy.Subscriber('/camera/image_raw/compressed', CompressedImage, camera_callback, queue_size = 1)
    # set rate
    rate = rospy.Rate(1000) # 1000hz
    while (1):
        rate.sleep()
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
    # cap.release()
    cv2.destroyAllWindows()
if __name__ == '__main__':
        lane_pose_publisher()
    except rospy.ROSInterruptException:
                I get an error as follows: TypeError: camera_callback() takes exactly 2 arguments (1 given)
– gup08
                Sep 23, 2021 at 4:31
                I had tried this too. If I run it, it says self is not defined. I add self in the def camera_callback(self, data) but then I go back to having the error that says "TypeError: camera_callback() takes exactly 2 arguments (1 given)..  I'll update the code on the original post with your correction and the "self" but I still get the argument error..
– gup08
                Sep 23, 2021 at 4:38
                @gup08 that was my fault, I missed a line. self should not be used in your code since in the context of the working example it returns a class instance; something your code doesn't have. Instead you should create a local CvBridge object and use that. See my edited code now.
– BTables
                Sep 23, 2021 at 4:42
                Thank you BTables. Seems you're in the right direction. I got this new error: [xcb] Unknown sequence number while processing queue [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called [xcb] Aborting, sorry about that. Gdk-Message: 23:44:48.226: Original: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.  python: ../../src/xcb_io.c:259: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
– gup08
                Sep 23, 2021 at 4:45
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.