forked from rykov8/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 86
/
video_stream_publisher.py
46 lines (34 loc) · 1.08 KB
/
video_stream_publisher.py
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
#!/usr/bin/env python
from __future__ import print_function
import rospy
import cv2
import sys
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
if __name__ == '__main__':
node_name = 'image_publisher'
video_path = '/dev/video0'
topic_name = '/cam'
visualize = False
if len(sys.argv) > 1:
video_path = sys.argv[1]
if len(sys.argv) > 2:
topic_name = sys.argv[2]
bridge = CvBridge()
vid = cv2.VideoCapture(video_path)
image_pub = rospy.Publisher(topic_name, Image, queue_size=1)
rospy.init_node(node_name, anonymous=True)
print('publish %s at %s' % (video_path, topic_name))
while not rospy.is_shutdown():
retval, img = vid.read()
if not retval:
print("Done!")
break
if visualize:
cv2.imshow("Image window", img)
cv2.waitKey(3)
try:
image_pub.publish(bridge.cv2_to_imgmsg(img, "bgr8"))
except CvBridgeError as e:
print(e)
cv2.destroyAllWindows()