-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamViewer.py
73 lines (56 loc) · 2.13 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
72
73
import argparse
import cv2
import numpy as np
import zmq
import msvcrt
import time
import base64
import threading
from ClientChat import *
class StreamViewer:
def __init__(self, server_address, port):
# Establish SUBSCRIBER connection, connects to address and port specified
context = zmq.Context()
self.footage_socket = context.socket(zmq.SUB)
self.footage_socket.connect('tcp://' + server_address + ':' + port)
self.footage_socket.setsockopt_string(zmq.SUBSCRIBE, np.unicode(''))
self.current_frame = None
# Recieves frame from the PUBLISHER and displays it
def receive_stream(self, display = True):
print("Connecting to stream...")
print("Enter P to pause")
pause = False
while self.footage_socket:
try:
frame = self.footage_socket.recv_string()
self.current_frame = self.string_to_image(frame)
if display:
cv2.imshow("Stream", self.current_frame)
cv2.waitKey(1)
# Pause if key "p" is pressed
if msvcrt.kbhit() and ord(msvcrt.getch()) == 112:
pause = True
print("Stream paused")
# Resume if key "p" is pressed
while pause == True:
if msvcrt.kbhit() and ord(msvcrt.getch()) == 112:
pause = False
print("Resume watching")
except KeyboardInterrupt:
cv2.destroyAllWindows()
break
print("Stopped watching stream!")
def string_to_image(self, string):
img = base64.b64decode(string)
npimg = np.frombuffer(img, dtype=np.uint8)
return cv2.imdecode(npimg, 1)
def main(address, username):
port = '5555'
server_address = address
stream_viewer = StreamViewer(server_address, port)
streamClient = threading.Thread(target=stream_viewer.receive_stream)
streamClient.start()
chatClient = ClientChat(username)
chatClient.start()
if __name__ == '__main__':
main()