-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyying.py
464 lines (408 loc) · 16.4 KB
/
pyying.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#to run a mjpeg server (like IP camera)
# run $ mjpg_streamer -i "/usr/local/lib/input_file.so -r -f /tmp/stream" -o "/usr/local/lib/output_http.so -w /usr/local/www -p 8080"
# TODO
# * get first ip from system if settings.server.host is ""
# * set standard port
# * do not use OSC if pyOSC not installed
from threading import Thread, Lock
import piggyphoto
import os
import sys
import time
import glob
import getopt
import signal
import re
import signal
from OSC import * #required, to install : sudo pip install pyOSC
from dotmap import DotMap
import pyStandardSettings
from pyStandardSettings import settings
from pySpacebroClient import SpacebroClient
from socketIO_client_nexus.exceptions import ConnectionError
from RootedHTTPServer import RootedHTTPServer, RootedHTTPRequestHandler
import socket
import sys
import json
from netifaces import interfaces, ifaddresses, AF_INET, AF_LINK
mutex = Lock()
class Pyying():
snap_path = 'snaps' # Don't forget to $ chown `whoami` this folder
snap_filename = 'snap'
snap_extension = 'jpg'
stream_path = '/tmp/stream'
stream_filename = 'preview'
stream_extension = 'jpg'
number = 0
snap_number = 0
media = {}
shootQueue = []
macAddress = ''
oscServer = None
oscThread = None
main_surface = None
nowindow = False
isStreaming = True
isShooting = False
isClosing = False
def __init__(self, host="localhost", port=8010, nowindow=False):
self.nowindow = nowindow
self.host = host
self.port = port
self.settings = DotMap(pyStandardSettings.getSettings())
self.snap_path = str(self.settings.folder.output)
self.stream_path = str(self.settings.folder.stream)
self.isStreaming = self.settings.streamAtStartup
# osc
self.oscServer = OSCServer((host, int(port)))
self.oscServer.addDefaultHandlers()
self.oscServer.addMsgHandler("/pyying/stream", self.stream_handler)
self.oscServer.addMsgHandler("/pyying/shoot", self.shoot_handler)
self.oscThread = Thread(target=self.oscServer.serve_forever)
self.oscThread.start()
print("Starting OSCServer. Use ctrl-C to quit.")
# static file server
self.staticFileServerThread = Thread(target=self.startStaticFileServer)
self.staticFileServerThread.start()
# TERM
signal.signal(signal.SIGTERM, self.sigclose)
try:
# MAC Address
try:
self.macAddress = ifaddresses('eth0')[AF_LINK][0]['addr']
except ValueError as e:
print(e)
print('mac address: ' + self.macAddress)
# check folders exist
if not os.path.exists(self.stream_path):
os.makedirs(self.stream_path)
if not os.path.exists(self.snap_path):
os.makedirs(self.snap_path)
self.find_last_number_in_directory()
# camera
self.camera = piggyphoto.camera(False)
if settings.camera.port:
settings.camera.port = str(settings.camera.port)
elif settings.camera.devpath:
try:
import pyudev
context = pyudev.Context()
device = pyudev.Devices.from_path(context, str(settings.camera.devpath))
settings.camera.port = 'usb:{0},{1}'.format(device['BUSNUM'], device['DEVNUM'])
except pyudev.device._errors.DeviceNotFoundAtPathError as e:
print('devpath not found', settings.camera.devpath)
self.close()
return
print 'init camera'
self.camera.init(settings.camera.port)
self.camera.leave_locked()
fullpath = self.getStreamPath()
self.camera.capture_image(fullpath, delete=True)
# spacebro
if settings.service.spacebro.enabled:
self.spacebroThread = Thread(target=self.startSpacebroClient)
self.spacebroThread.start()
# self.sendStatus()
# lightningbro
if settings.service.lightningbro.enabled:
self.lightningbroThread = Thread(target=self.startLightningbroClient)
self.lightningbroThread.start()
# create window from first preview
if (not self.nowindow):
picture = pygame.image.load(fullpath)
pygame.display.set_mode(picture.get_size())
self.main_surface = pygame.display.get_surface()
except KeyboardInterrupt:
self.close()
except Exception as e:
print(str(e))
self.close()
def start(self):
if (not self.nowindow):
clock = pygame.time.Clock()
try:
while not self.quit_pressed():
# trying to get a fixed fps. However the camera is limiting to approx 22 fps
# print str(clock.get_fps())
if (not self.nowindow):
clock.tick(25)
# repeat shoot
if (self.settings.interval):
r, s = divmod(time.time(), self.settings.interval)
if (s < 0.01):
self.isShooting = True
self.media = {}
self.media['albumId'] = str(r)
# Shoot picture
if (self.isShooting):
self.shoot()
# Stream pictures
if (self.isStreaming):
fullpath = self.getStreamPath()
self.camera.capture_preview(fullpath)
if (not self.nowindow):
self.show(fullpath)
self.number += 1
else:
time.sleep(0.0001) # avoid cpu > 100%
self.close()
except KeyboardInterrupt:
self.close()
except Exception as e:
print(str(e))
self.close()
def checkLibGphoto2Error(self, e):
print(e)
if e.result is -1:
self.quit()
def shoot(self):
#print('Shoot received! ', time.time())
cameraNumber = str(self.settings.cameraNumber)
if 'albumId' in self.media:
fullpath = self.getSnapPath(self.media['albumId'], cameraNumber)
else:
fullpath = self.getSnapPath()
if 'meta' in self.media and 'frameDelays' in self.media['meta'] and self.macAddress in self.media['meta']['frameDelays'] and cameraNumber in self.media['meta']['frameDelays'][self.macAddress]:
sleepDuration = int(self.media['meta']['frameDelays'][self.macAddress][cameraNumber])/1000.0
print('Sleep for frameDelay for', sleepDuration)
time.sleep(sleepDuration)
print('Shoot command! ', time.time())
mutex.acquire()
try:
self.camera.capture_image(fullpath, delete=True)
except piggyphoto.libgphoto2error as e:
self.checkLibGphoto2Error(e)
finally:
time.sleep(1.5) # the firmware generates error if we ask for settings again too quickly
mutex.release()
print('Shoot finished! ', time.time())
# say it on spacebro
self.media['path'] = os.path.abspath(fullpath)
self.media['file'] = os.path.basename(fullpath)
hostname = self.settings.server.host
if not hostname:
hostname = [i['addr'] for i in ifaddresses('eth0').setdefault(AF_INET, [{'addr':u'10.60.60.1'}] )][0]
self.media['url'] = "http://" + hostname + ":" + str(self.settings.server.port) \
+ "/" + self.media['file']
self.media['cameraNumber'] = self.settings.cameraNumber
self.media['macAddress'] = self.macAddress
spacebroSettings = self.settings.service.spacebro
self.spacebroClient.emit(spacebroSettings.client['out'].outMedia.eventName, self.media)
# clear
self.media = {}
self.isShooting = False
# shoot again if queue
if (len(self.shootQueue) > 0):
self.media = self.shootQueue.pop(0)
self.isShooting = True
def startLightningbroClient(self):
self.lightningbroSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.lightningbroSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Bind the socket to the port
server_address = (str(settings.service.lightningbro.host), int(settings.server.port) + 1)
print >>sys.stderr, 'starting lightningbroClient on %s port %s' % server_address
self.lightningbroSock.bind(server_address)
while not self.quit_pressed():
message, address = self.lightningbroSock.recvfrom(4096)
#print >>sys.stderr, 'received %s bytes from %s' % (len(message), address)
#print >>sys.stderr, message
#if message == "stop":
# return
try:
data = json.loads(message)
#print('data: ' + str(data))
self.onShoot(data)
except ValueError as e:
print(e)
return
def startSpacebroClient(self):
spacebroSettings = self.settings.service.spacebro
while not self.quit_pressed():
try:
self.spacebroClient = SpacebroClient(spacebroSettings.toDict(), wait_for_connection=False)
self.spacebroClient.on(spacebroSettings.client['in'].shoot.eventName, self.onShoot)
self.spacebroClient.on(spacebroSettings.client['in'].getConfig.eventName, self.onGetConfig)
self.spacebroClient.on(spacebroSettings.client['in'].setConfig.eventName, self.onSetConfig)
self.spacebroClient.on(spacebroSettings.client['in'].getStatus.eventName, self.onGetStatus)
self.spacebroClient.on(spacebroSettings.client['in'].setInterval.eventName, self.onSetInterval)
self.sendStatus()
while not self.quit_pressed():
self.spacebroClient.wait(3)
except ConnectionError as e:
print(str(e))
time.sleep(1)
if hasattr(self, 'spacebroClient'):
self.spacebroClient.disconnect()
return
def startStaticFileServer(self):
server_address = ('', self.settings['server']['port'])
self.httpd = RootedHTTPServer(self.settings['folder']['output'], server_address, RootedHTTPRequestHandler)
hostname = settings.server.host
if not hostname:
hostname = [i['addr'] for i in ifaddresses('eth0').setdefault(AF_INET, [{'addr':u'10.60.60.1'}] )][0]
print("Serving folder '" + self.settings.folder.output + "' on " + hostname + ":" + str(settings.server.port) + " ...")
self.httpd.serve_forever()
def sigclose(self, signum, frame):
self.quit()
def quit(self):
self.isClosing = True
def close(self):
print("Closing application")
self.isClosing = True
self.oscServer.close()
self.oscThread.join()
if hasattr(self, 'spacebroThread'):
self.spacebroThread.join()
if hasattr(self, 'lightningbroThread'):
if hasattr(self, 'lightningbroSock'):
#self.lightningbroSock.shutdown()
server_address = (str(settings.service.lightningbro.host), int(settings.server.port) + 1)
self.lightningbroSock.sendto("stop", server_address)
self.lightningbroSock.close()
self.lightningbroThread.join()
self.httpd.shutdown()
self.staticFileServerThread.join()
try:
self.camera.leave_locked()
except AttributeError:
pass
print("Have a good day!")
def quit_pressed(self):
if (not self.nowindow):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_SPACE :
self.isShooting = True
return self.isClosing
def show(self,file):
try:
picture = pygame.image.load(file)
self.main_surface.blit(picture, (0, 0))
pygame.display.flip()
except Exception as e:
print(str(e))
def stream_handler(self, addr, tags, data, client):
print("Stream: " + str(data) + " is that " + str(True) + " ?")
self.isStreaming = data
return
def shoot_handler(self, addr, tags, data, client):
print("osc shoot")
self.isShooting = True
return
def onShoot(self, data, callback = 0):
print("spacebro shoot")
if not self.isShooting:
self.media = data
self.isShooting = True
else:
self.shootQueue.append(data)
#self.shoot()
return
def onGetStatus(self, data):
self.sendStatus()
def onSetInterval(self, data):
self.settings.interval = data["interval"]
def sendStatus(self, error = 0):
print("spacebro get status")
data = {}
data['macAddress'] = self.macAddress
data['cameraNumber'] = self.settings.cameraNumber
data['stream'] = str(self.settings.service.mjpg_streamer.url)
data['connected'] = self.camera.initialized
data['lastError'] = str(error)
spacebroSettings = self.settings.service.spacebro
self.spacebroClient.emit(spacebroSettings.client['out'].status.eventName, data)
return
def onGetConfig(self, data):
print("___________________")
print("spacebro get config")
currentIsStreaming = self.isStreaming
self.isStreaming = False
cfgmap = False
retries = 5
#retries = 0
for i in range(1 + retries):
mutex.acquire()
print("ask firmware the config")
try:
cfgmap = self.camera.get_map_config()
break
except piggyphoto.libgphoto2error as e:
self.checkLibGphoto2Error(e)
if e.result is -1:
break
finally:
time.sleep(2) # the firmware generates error if we ask for settings again too quickly
mutex.release()
time.sleep(0.01)
self.isStreaming = True
print('finished get config')
self.isStreaming = currentIsStreaming
if cfgmap:
cfgmap['cameraNumber'] = self.settings.cameraNumber
cfgmap['stream'] = str(self.settings.service.mjpg_streamer.url)
cfgmap['macAddress'] = self.macAddress
spacebroSettings = self.settings.service.spacebro
self.spacebroClient.emit(spacebroSettings.client['out'].config.eventName, cfgmap)
return
def onSetConfig(self, data):
#print("spacebro set config", data)
print("spacebro set config")
#self.onGetConfig(0)
currentIsStreaming = self.isStreaming
self.isStreaming = False
cfgmap = data
cfgmap.pop('cameraNumber')
cfgmap.pop('stream')
cfgmap.pop('_to')
cfgmap.pop('_from')
retries = 100
for i in range(1 + retries):
mutex.acquire()
try:
self.camera.set_map_config(cfgmap)
break
except piggyphoto.libgphoto2error as e:
self.checkLibGphoto2Error(e)
if e.result is -1:
break
finally:
time.sleep(2) # the firmware generates error if we set settings again too quickly
mutex.release()
time.sleep(0.01)
print('finished set config')
self.isStreaming = currentIsStreaming
self.onGetConfig(data)
def getStreamPath(self):
fullpath = os.path.join(self.stream_path, self.stream_filename + ("%05d" % self.number) + '.' + self.stream_extension)
#fullpath = "/tmp/fifo.mjpg"
return fullpath
def getSnapPath(self, albumId=-1, cameraNumber='01'):
if albumId is -1:
albumId = ("%05d" % self.snap_number)
self.snap_number+=1
fullpath = os.path.join(self.snap_path, self.snap_filename + '-' + albumId + '-' + cameraNumber + '.' + self.snap_extension)
return str(fullpath)
def find_last_number_in_directory(self):
fullpath = os.path.join(self.snap_path, self.snap_filename + '-[0-9]*[0-9]*-*.' + self.snap_extension)
files = sorted(glob.glob(fullpath))
if (len(files) > 0):
filename = files[-1]
regex = re.compile(r'\d\d\d\d\d')
number = regex.findall(filename)
if (len(number) > 0):
self.snap_number = int(number[0]) + 1
return
def main(argv):
settings = DotMap(pyStandardSettings.getSettings())
if (not settings.nowindow):
import pygame
hostname = settings.server.host
if not hostname:
hostname = [i['addr'] for i in ifaddresses('eth0').setdefault(AF_INET, [{'addr':u'10.60.60.1'}] )][0]
ying = Pyying(host=hostname, port=settings.server.port, nowindow=settings.nowindow)
ying.start()
if __name__ == '__main__':
main(sys.argv[1:])