File size: 1,442 Bytes
ef877a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3


import roslib
#roslib.load_manifest('my_package')
import sys
import rospy
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError


def talker():
    rospy.init_node('talker', anonymous=True)
    
    use_camera = rospy.get_param('~use_camera', False)
    input_video_file = rospy.get_param('~input_video_file','test.mp4')
    # rospy.loginfo(f"Talker - params: use_camera={use_camera}, input_video_file={input_video_file}")

    # rospy.loginfo("Talker: Trying to open a video stream")
    if use_camera == True:
        cap = cv2.VideoCapture(0)
    else:
        cap = cv2.VideoCapture(input_video_file)

    pub = rospy.Publisher('image_topic', Image, queue_size=1)
    rate = rospy.Rate(30) # 30hz
    bridge = CvBridge()

    while not rospy.is_shutdown():
        ret, cv_image = cap.read()
        if ret==False:
            print("Talker: Video is over")
            rospy.loginfo("Video is over")
            return

        try:
            image = bridge.cv2_to_imgmsg(cv_image, "bgr8")
        except CvBridgeError as e:
            rospy.logerr("Talker: cv2image conversion failed: ", e)
            print(e)
            continue

        rospy.loginfo("Talker: Publishing frame")
        pub.publish(image)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass