[ROS1_opencv] how to synchronize publishment and processing in subscription

asked 2022-08-24 02:26:17 -0500

kuyhnooj gravatar image

Hello, I'm a beginner of ROS1

The case is that The rates of publishment and subscription are about 0.1sec and the average time of process(edge detection) is 0.005sec.

However, when i visualize raw and processed images with RQT, processed images seem like getting slow.

So, i guess it is going oveloaded. How to solve this problem?? Or Please let me know helpful websites

The codes are below :

Publisher

class Camera(object):

    def __init__(self):
        self.height = 640
        self.width = 480
        self.fps = 10

        self.bridge = CvBridge()

        self.pubstr = "/Visualization/camera/raw"
        self.rawpub = rospy.Publisher(self.pubstr, Image, queue_size=10)

        self.cap = cv2.VideoCapture(0)

    def run(self):
        if not self.cap.isOpened():
            print("Camera is not opened\n")

        ret, cv_image = self.cap.read()

        if not ret:
            print("Detect no Image\n")

        else:
            self.rawpub.publish(self.bridge.cv2_to_imgmsg(cv_image, "bgr8"))

    if __name__ == '__main__':
        rospy.init_node("Publish_camera")
        camera = Camera()
        r = rospy.Rate(10)

    while not rospy.is_shutdown():
        camera.run()
        r.sleep()

Subscriber

class Camera(object):
    def __init__(self):
        self.height = 640
        self.width = 480
        self.cnt = 1
        self.rawcnt = 1
        self.fps = 10

        self.bridge = CvBridge()
        self.processbridge = CvBridge()
        self.rospack = rospkg.RosPack()
        self.cv2image = Image()

        self.substr = "/Visualization/camera/raw"
        self.pubstr = "/Visualization/camera/processed"
        self.save_path = self.rospack.get_path("graduate_project") + "/camera"

        self.pub = rospy.Publisher(self.pubstr, Image, queue_size=1)

    def callback(self, rawimage):
        try:
            src = self.bridge.imgmsg_to_cv2(rawimage, "bgr8")
        except CvBridgeError as e:
            print(e)

# Bird Wide View
#
#

        if src.data is '':  
            pass
        else:
            dst = cv2.Canny(src, 50, 200, None, 3)
            cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
            lines = cv2.HoughLines(dst, 0.8, np.pi / 180, 150, None, min_theta = (np.pi/2) - 1, max_theta = (np.pi/2) + 1)

            if lines is not None:
                for i in range(0, len(lines)):
                    rho = lines[i][0][0]
                    theta = lines[i][0][1]
                    a = math.cos(theta)
                    b = math.sin(theta)
                    x0 = a * rho
                    y0 = b * rho
                    pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
                    pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))

                    cv2.line(cdst, pt1, pt2, (0, 0, 255), 3, cv2.LINE_AA)

                imgmsg = self.processbridge.cv2_to_imgmsg(cdst, "bgr8")
                self.pub.publish(imgmsg)

    def run(self):
        self.sub = rospy.Subscriber(self.substr, Image, self.callback, queue_size=100)

    if __name__ == '__main__': 
        rospy.init_node("PC_process")
        camera = Camera()
        r = rospy.Rate(10)

        while not rospy.is_shutdown():
            camera.run()
            r.sleep()
edit retag flag offensive close merge delete