This repository has been archived by the owner on Apr 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.py
133 lines (110 loc) · 3.11 KB
/
stream.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import threading
import time
from base64 import b64encode
from datetime import datetime
from io import BytesIO
import picamera
from PIL import Image
from websocket import create_connection
# Default settings
RESOLUTION = (640, 480)
HOST = 'makentnu.no'
KEY = 'SECRET'
STREAM_NAME = 'STREAM'
STREAM_URL = f'wss://{HOST}/ws/stream/{STREAM_NAME}/'
DEBUG = True
VFLIP = False
HFLIP = False
THREADS = 3
DELAY = 0
TIME_BETWEEN_CONNECTION_RETRIES = 3
QUALITY = 50
# Override defaults with local_settings
try:
from local_settings import *
except ImportError:
pass
def debug(*args):
if DEBUG:
print(datetime.now(), *args)
def read(ws):
while True:
try:
ws.recv()
except:
break
def decode_and_send(ws, image):
result = ws.send(json.dumps({
'image': b64encode(image).decode('ascii'),
'key': KEY,
}))
debug("Send from", threading.current_thread().name)
return result
def sender(images, cv, error):
try:
ws = create_connection(STREAM_URL)
threading.Thread(target=read, args=(ws,)).start()
while not error[0]:
with cv:
while not len(images):
if error[0]:
return
cv.wait()
image = images.pop(0)
decode_and_send(ws, image)
except:
debug("Unable to connect")
error[0] = True
with cv:
cv.notify_all()
def capture():
debug(f"Connecting to {HOST}")
cv = threading.Condition()
stream_capture = BytesIO()
stream_image = BytesIO()
images = []
error = [False]
if not THREADS:
try:
ws = create_connection(STREAM_URL)
threading.Thread(target=read, args=(ws,)).start()
except:
debug("Unable to connect")
return
for _ in range(THREADS):
threading.Thread(target=sender, args=(images, cv, error)).start()
with picamera.PiCamera() as camera:
camera.resolution = RESOLUTION
camera.vflip = VFLIP
camera.hflip = HFLIP
for _ in camera.capture_continuous(stream_capture, 'jpeg', use_video_port=True):
time.sleep(DELAY)
debug("Capture")
debug("Active threads:", threading.active_count())
stream_capture.seek(0)
stream_image.seek(0)
stream_image.truncate()
try:
Image.open(stream_capture).save(stream_image, 'jpeg', optimize=True, quality=QUALITY)
except:
pass
if THREADS:
if error[0]:
break
with cv:
if len(images) < 3:
images.append(stream_image.getvalue())
cv.notify_all()
else:
try:
decode_and_send(ws, stream_image.getvalue())
except:
return
stream_capture.seek(0)
stream_capture.truncate()
def main():
while True:
capture()
time.sleep(TIME_BETWEEN_CONNECTION_RETRIES)
main()