-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathview.py
328 lines (263 loc) · 10.9 KB
/
view.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# This plugin lets you remotely query and view a
# number of different image sources in Sideband,
# including remote or local webcams, video sources
# or images stored in a filesystem.
#
# This plugin requires the "pillow" pip package.
#
# For HTTP and local file sources, no extras are
# required, but for fetching images from connected
# video sources, you need "opencv-python" from pip.
import io
import os
import RNS
import time
import queue
import requests
import threading
import importlib
from PIL import Image as PilImage
if importlib.util.find_spec("cv2") != None:
import cv2
# Add view sources to the plugin
def register_view_sources():
ViewCommandPlugin.add_source("xkcd", HttpSource("https://imgs.xkcd.com/comics/tsp_vs_tbsp.png"))
ViewCommandPlugin.add_source("camera", CameraSource(camera_index=0))
ViewCommandPlugin.add_source("rocks", FileSource("~/Downloads/rocks.jpg"))
ViewCommandPlugin.add_source("osaka", StreamSource("http://honjin1.miemasu.net/nphMotionJpeg?Resolution=640x480&Quality=Standard"))
ViewCommandPlugin.add_source("factory", StreamSource("http://takemotopiano.aa1.netvolante.jp:8190/nphMotionJpeg?Resolution=640x480&Quality=Standard&Framerate=1"))
quality_presets = {
"lora": {"max": 160, "quality": 18},
"low": {"max": 256, "quality": 25},
"default": {"max": 320, "quality": 33},
"medium": {"max": 480, "quality": 50},
"high": {"max": 960, "quality": 65},
"hd": {"max": 1920, "quality": 75},
"4k": {"max": 3840, "quality": 65},
}
if not "default" in quality_presets:
raise ValueError("No default quality preset defined, please define one and reload the plugin")
class ViewSource():
DEFAULT_STALE_TIME = 3.14159
def __init__(self):
self.source_data = None
self.last_update = 0
self.stale_time = ViewSource.DEFAULT_STALE_TIME
def is_stale(self):
return time.time() > self.last_update + self.stale_time
def update(self):
raise NotImplementedError()
def scaled_image(self, max_dimension, quality):
with PilImage.open(io.BytesIO(self.source_data)) as im:
im.thumbnail((max_dimension, max_dimension))
buf = io.BytesIO()
im.save(buf, format="webp", quality=quality)
return buf.getvalue()
def get_image_field(self, preset="default"):
if not preset in quality_presets:
preset = "default"
try:
if self.is_stale():
self.update()
if self.source_data != None:
max_dimension = quality_presets[preset]["max"]
quality = quality_presets[preset]["quality"]
return ["webp", self.scaled_image(max_dimension, quality)]
except Exception as e:
RNS.log(f"Could not create image field for {self}. The contained exception was: {e}", RNS.LOG_ERROR)
RNS.trace_exception(e)
return None
class HttpSource(ViewSource):
def __init__(self, url):
self.url = url
super().__init__()
def update(self):
image_request = requests.get(self.url, stream=True)
if image_request.status_code == 200:
self.source_data = image_request.content
self.last_update = time.time()
else:
self.source_data = None
class CameraSource(ViewSource):
def __init__(self, camera_index=0, camera_width=1280, camera_height=720):
self.camera_index = camera_index
self.camera_width = camera_width
self.camera_height = camera_height
self.camera_ready = False
self.frame_queue = queue.Queue()
super().__init__()
self.start_reading()
def start_reading(self):
self.camera = cv2.VideoCapture(self.camera_index)
self.camera.set(cv2.CAP_PROP_FRAME_WIDTH, self.camera_width)
self.camera.set(cv2.CAP_PROP_FRAME_HEIGHT, self.camera_height)
threading.Thread(target=self.read_frames, daemon=True).start()
def read_frames(self):
try:
while True:
ret, frame = self.camera.read()
self.camera_ready = True
if not ret:
self.camera_ready = False
break
if not self.frame_queue.empty():
try:
self.frame_queue.get_nowait()
except queue.Empty:
pass
self.frame_queue.put(frame)
except Exception as e:
RNS.log("An error occurred while reading frames from the camera: "+str(e), RNS.LOG_ERROR)
self.release_camera()
def update(self):
if not self.camera:
self.start_reading()
while not self.camera_ready:
time.sleep(0.2)
retval, frame = self.camera.read()
if not retval:
self.source_data = None
else:
retval, buffer = cv2.imencode(".png", frame)
self.source_data = io.BytesIO(buffer).getvalue()
self.last_update = time.time()
def release_camera(self):
try:
self.camera.release()
except:
pass
self.camera = None
self.camera_ready = False
class StreamSource(ViewSource):
DEFAULT_IDLE_TIMEOUT = 10
def __init__(self, url=None):
self.url = url
self.stream_ready = False
self.frame_queue = queue.Queue()
self.stream = None
self.started = 0
self.idle_timeout = StreamSource.DEFAULT_IDLE_TIMEOUT
super().__init__()
self.start_reading()
def start_reading(self):
self.stream = cv2.VideoCapture(self.url)
self.started = time.time()
threading.Thread(target=self.read_frames, daemon=True).start()
def read_frames(self):
try:
while max(self.last_update, self.started)+self.idle_timeout > time.time():
ret, frame = self.stream.read()
if not ret:
self.stream_ready = False
else:
self.stream_ready = True
if not self.frame_queue.empty():
if self.frame_queue.qsize() > 1:
try:
self.frame_queue.get_nowait()
except queue.Empty:
pass
self.frame_queue.put(frame)
RNS.log(str(self)+" idled", RNS.LOG_DEBUG)
except Exception as e:
RNS.log("An error occurred while reading frames from the stream: "+str(e), RNS.LOG_ERROR)
self.release_stream()
def update(self):
if not self.stream:
self.start_reading()
while not self.stream_ready:
time.sleep(0.2)
if self.stream == None:
self.source_data = None
return
frame = self.frame_queue.get()
retval, buffer = cv2.imencode(".png", frame)
self.source_data = io.BytesIO(buffer).getvalue()
self.last_update = time.time()
def release_stream(self):
try:
self.stream.release()
except:
pass
self.stream = None
self.stream_ready = False
class FileSource(ViewSource):
def __init__(self, path):
self.path = os.path.expanduser(path)
super().__init__()
def update(self):
try:
with open(self.path, "rb") as image_file:
self.source_data = image_file.read()
except Exception as e:
RNS.log("Could not read image at \"{self.path}\": "+str(e), RNS.LOG_ERROR)
self.source_data = None
class ViewCommandPlugin(SidebandCommandPlugin):
command_name = "view"
sources = {}
stamptimefmt = "%Y-%m-%d %H:%M:%S"
def start(self):
RNS.log("View command plugin starting...")
super().start()
def stop(self):
super().stop()
@staticmethod
def add_source(name, source):
ViewCommandPlugin.sources[name] = source
def message_response(self, message, destination):
self.get_sideband().send_message(
message,
destination,
False, # Don't use propagation by default, try direct first
skip_fields = True, # Don't include any additional fields automatically
no_display = True, # Don't display this message in the message stream
)
def image_response(self, message, image_field, destination):
self.get_sideband().send_message(
message,
destination,
False, # Don't use propagation by default, try direct first
skip_fields = True, # Don't include any additional fields automatically
no_display = True, # Don't display this message in the message stream
image = image_field, # Add the scaled and compressed image
)
def timestamp_str(self, time_s):
timestamp = time.localtime(time_s)
return time.strftime(self.stamptimefmt, timestamp)
def handle_command(self, arguments, lxm):
requestor = lxm.source_hash
if len(arguments) == 0:
self.message_response("No view source was specified", requestor)
return
if arguments[0] == "--list" or arguments[0] == "-l":
if len(self.sources) == 0:
response = "No sources available on this system"
else:
response = "Available Sources:\n"
for source in self.sources:
response += "\n - "+str(source)
self.message_response(response, requestor)
return
try:
source = arguments[0]
if len(arguments) > 1:
quality_preset = arguments[1]
else:
quality_preset = "default"
if not source in self.sources:
self.message_response("The specified view source does not exist on this system", requestor)
else:
image_field = self.sources[source].get_image_field(quality_preset)
image_timestamp = self.timestamp_str(self.sources[source].last_update)
message = "#!md\n" # Tell sideband to format this message
message += f"Source [b]{source}[/b] at [b]{image_timestamp}[/b]"
if image_field != None:
self.image_response(message, image_field, requestor)
else:
self.message_response("The image source could not be retrieved or prepared", requestor)
except Exception as e:
self.message_response(f"An error occurred:\n\n{e}", requestor)
register_view_sources()
# Finally, tell Sideband what class in this
# file is the actual plugin class.
plugin_class = ViewCommandPlugin