Simple image publisher with use of ROSPY cant work

asked 2020-07-01 02:16:28 -0500

updated 2020-07-01 02:45:29 -0500

gvdhoorn gravatar image

Hi all, I am a student learning rospy. I've tried to use a subcriber and publisher to display a image in pyqt5, how ever, when i click on the show image after i run the code, the image cant be shown.

This is my publisher code:

#!/usr/bin/python2.7

import sys
import rospy
import roslib
import cv2
import numpy as np

import random
import math
import time
from std_msgs.msg import String
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
from cv_bridge import CvBridge, CvBridgeError

def main():

    image_pub = rospy.Publisher("/image", Image, queue_size=1)
    rate = rospy.Rate(50)
    bridge = CvBridge()

    while not rospy.is_shutdown():
          print("------------Mask detection processing one time!------------")

          image_path='/home/sk/catkin_ws/src/test/scripts/p_e.png'
          cv_image = cv2.imread(image_path)

          # read a image using opencv
          # change to your real image path    

          print(cv_image.shape)
          #cv2.imshow("Pikachu", cv_image)
          msg_image = bridge.cv2_to_imgmsg(cv_image, encoding = "bgr8") 

          stamp = rospy.Time.now()
          msg_image.header.frame_id = "camera_link"
          msg_image.header.stamp = stamp

          image_pub.publish(msg_image)

          rate.sleep()

if __name__ == '__main__':
    try:
      rospy.init_node('image_pub_node', anonymous=True)
      main()
      rospy.spin()
    except KeyboardInterrupt:
      print("Shutting down")
      sys.exit(0)
      pass

This is my subcriber code:

#!/usr/bin/python2.7

import sys
import rospy
import roslib
import cv2
import numpy as np

import random
import math
import time
from std_msgs.msg import String
from sensor_msgs.msg import Image
from sensor_msgs.msg import CompressedImage
from cv_bridge import CvBridge, CvBridgeError
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QPixmap


class ImageWidget(QtWidgets.QWidget):
    #def callback(self,data):
    def callback(self,rosdata):
      global cv_image      
      #self.bridge = CvBridge()
      self.bridge = CvBridge()
      #print("in callback")
      rate = rospy.Rate(10)
      rate.sleep() 
      try:
        cv_image = self.bridge.imgmsg_to_cv2(rosdata, "bgr8")
        #cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")    

      except CvBridgeError as e:                            
          print(e)


    def __init__(self, parent=None):

        super(ImageWidget, self).__init__(parent)

        self.button = QtWidgets.QPushButton('Show picture')
        self.button.clicked.connect(self.main)
        self.image_frame = QtWidgets.QLabel()

        self.layout = QtWidgets.QVBoxLayout()
        self.layout.addWidget(self.button)
        self.layout.addWidget(self.image_frame)
        self.setLayout(self.layout)

    def main(self):

        rospy.init_node('image_sub_node', anonymous=True)
        self.image_sub = rospy.Subscriber("/image", Image, self.callback, queue_size=1, buff_size=52428800)

        rate = rospy.Rate(50) #50Hz
        bridge = CvBridge()

        while not rospy.is_shutdown():
          print("------------Mask detection processing one time!------------")

          try:
            self.cv_image = cv2.imread('Image')
            self.cv_image = QtGui.QImage(self.cv_image.data, self.cv_image.shape[1], self.cv_image.shape[0], QtGui.QImage.Format_RGB888).rgbSwapped()
            self.image_frame.setPixmap(QtGui.QPixmap.fromImage(self.cv_image))
          except Exception:
            continue

            rate.sleep()

if __name__ == '__main__':
    try:
      app = QtWidgets.QApplication(sys.argv)
      image_widget = ImageWidget()
      image_widget.show()
      sys.exit(app.exec_())
      rospy.init_node('image_sub_node', anonymous=True)
      main()
      rospy.spin()
    except KeyboardInterrupt:
      pass

I've tried almost everything but to no avail.

edit retag flag offensive close merge delete