Hello, I am trying to read two consecutive images from my rostopic using rospy.Subscriber to compare the orientation between these pictures but using the code below, I found out two images I get are the same every time so I am trying to find out if there is a way to get the two images with delay, more specifically, getting the second image 3 seconds after I get the first image. I tried putting time.sleep() between functions but it wouldn't work out.
import cv2
import numpy as np
import rospy
import time
import os
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
def cam_callback(msg):
path = '/home/furki/img'
try:
# Converting ROS Image message to OpenCV2
cv2_img1 = bridge1.imgmsg_to_cv2(msg,"bgr8")
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
img1 = cv2.imwrite(os.path.join(path, f'img1.jpeg'), cv2_img1)
def cam_callback2(msg):
path = '/home/furki/img'
try:
# Converting ROS Image message to OpenCV2
cv2_img2 = bridge.imgmsg_to_cv2(msg,"bgr8")
except CvBridgeError as e:
print(e)
else:
# Save your OpenCV2 image as a jpeg
img2 = cv2.imwrite(os.path.join(path, f'img2.jpeg'), cv2_img2)
rospy.init_node("orientation_feature_detection_py")
image_topic = '/s500/usb_cam/image_raw'
rate = rospy.Rate(1)
rospy.Subscriber(image_topic, Image, cam_callback)
rate.sleep()
rospy.Subscriber(image_topic, Image, cam_callback2)
rate.sleep()
# Reading the two images
img1 = cv2.imread('/home/furki/img/img1.jpeg')
img2 = cv2.imread('/home/furki/img/img2.jpeg')
Originally posted by Furki on ROS Answers with karma: 3 on 2023-05-08
Post score: 0