-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamViewer.py
71 lines (56 loc) · 2.06 KB
/
StreamViewer.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
import cv2
import numpy as np
import zmq
from constants import PORT
from utils import string_to_image
class StreamViewer:
def __init__(self, port=PORT):
"""
Binds the computer to a ip address and starts listening for incoming streams.
:param port: Port which is used for streaming
"""
context = zmq.Context()
self.footage_socket = context.socket(zmq.SUB)
self.footage_socket.bind('tcp://*:' + port)
self.footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
self.current_frame = None
self.keep_running = True
def receive_stream(self, display=True):
"""
Displays displayed stream in a window if no arguments are passed.
Keeps updating the 'current_frame' attribute with the most recent frame, this can be accessed using 'self.current_frame'
:param display: boolean, If False no stream output will be displayed.
:return: None
"""
self.keep_running = True
while self.footage_socket and self.keep_running:
try:
frame = self.footage_socket.recv_string()
self.current_frame = string_to_image(frame)
if display:
cv2.imshow("Stream", self.current_frame)
cv2.waitKey(1)
except KeyboardInterrupt:
cv2.destroyAllWindows()
break
print("Streaming Stopped!")
def stop(self):
"""
Sets 'keep_running' to False to stop the running loop if running.
:return: None
"""
self.keep_running = False
def main():
port = PORT
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port',
help='The port which you want the Streaming Viewer to use, default'
' is ' + PORT, required=False)
args = parser.parse_args()
if args.port:
port = args.port
stream_viewer = StreamViewer(port)
stream_viewer.receive_stream()
if __name__ == '__main__':
main()