-
Notifications
You must be signed in to change notification settings - Fork 8
/
show_video_zmq.py
45 lines (38 loc) · 1.1 KB
/
show_video_zmq.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
'''
Receives a live video stream from the robot using ZeroMQ
'''
import io
import socket
import struct
from PIL import Image
import cv2
import numpy as np
import zmq
# Configure the following parameter:
IP_ADDRESS = "192.168.0.56"
cv2.startWindowThread()
cv2.namedWindow('Robot Camera', cv2.WINDOW_NORMAL)
# Setup SUBSCRIBE socket
context = zmq.Context()
zmq_socket = context.socket(zmq.SUB)
zmq_socket.setsockopt(zmq.SUBSCRIBE, b'')
zmq_socket.setsockopt(zmq.CONFLATE, 1)
zmq_socket.connect("tcp://{}:5557".format(IP_ADDRESS))
try:
i = 0
while True:
# Construct a stream to hold the image data and read the image
# data from the connection
image_stream = io.BytesIO()
payload = zmq_socket.recv()
image_stream.write(payload)
# Rewind the stream, open it as an image with PIL and do some
# processing on it
image_stream.seek(0)
image = Image.open(image_stream)
downsampled_image = np.array(image.convert('L'))
cv2.imshow('Robot Camera', downsampled_image)
cv2.waitKey(1)
i += 1
finally:
pass