-
Notifications
You must be signed in to change notification settings - Fork 0
/
Streamer.py
83 lines (64 loc) · 2.49 KB
/
Streamer.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
74
75
76
77
78
79
80
81
82
83
import argparse
import cv2
import zmq
from camera.Camera import Camera
from constants import PORT, SERVER_ADDRESS
from utils import image_to_string
class Streamer:
def __init__(self, server_address=SERVER_ADDRESS, port=PORT):
"""
Tries to connect to the StreamViewer with supplied server_address and creates a socket for future use.
:param server_address: Address of the computer on which the StreamViewer is running, default is `localhost`
:param port: Port which will be used for sending the stream
"""
print("Connecting to ", server_address, "at", port)
context = zmq.Context()
self.footage_socket = context.socket(zmq.PUB)
self.footage_socket.connect('tcp://' + server_address + ':' + port)
self.keep_running = True
def start(self):
"""
Starts sending the stream to the Viewer.
Creates a camera, takes a image frame converts the frame to string and sends the string across the network
:return: None
"""
print("Streaming Started...")
camera = Camera()
camera.start_capture()
self.keep_running = True
while self.footage_socket and self.keep_running:
try:
frame = camera.current_frame.read() # grab the current frame
image_as_string = image_to_string(frame)
self.footage_socket.send(image_as_string)
except KeyboardInterrupt:
cv2.destroyAllWindows()
break
print("Streaming Stopped!")
cv2.destroyAllWindows()
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
server_address = SERVER_ADDRESS
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--server',
help='IP Address of the server which you want to connect to, default'
' is ' + SERVER_ADDRESS,
required=True)
parser.add_argument('-p', '--port',
help='The port which you want the Streaming Server to use, default'
' is ' + PORT, required=False)
args = parser.parse_args()
if args.port:
port = args.port
if args.server:
server_address = args.server
streamer = Streamer(server_address, port)
streamer.start()
if __name__ == '__main__':
main()