-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera.py
493 lines (402 loc) · 15.5 KB
/
camera.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import pygame
import time
import imp
from fcntl import fcntl, F_GETFL, F_SETFL
from os import O_NONBLOCK, read
import subprocess
import shlex
from utils import GenericClassFactory
from shutil import copyfile
from datetime import datetime
from abc import ABCMeta, abstractmethod
from subprocess import Popen, PIPE
class AbstractCamera(object):
"""
Abstract interface for camera implementations
"""
__metaclass__ = ABCMeta
@abstractmethod
def set_memory_capture(self):
"""
Configure memory capture mode (photos are as well stored on camera memory)
"""
raise NotImplementedError
@abstractmethod
def set_idle(self):
"""
Set camera to idle mode
"""
raise NotImplementedError
@abstractmethod
def get_preview(self):
"""
Get a preview image
:return: preview pygame.image
"""
raise NotImplementedError
@abstractmethod
def take_photo(self):
"""
Trigger a photo on the camera
:return: (photo pygame.image,PATH_TO_PHOTO)
"""
raise NotImplementedError
@abstractmethod
def enable_live_autofocus(self):
"""
enable camera autofocus in preview mode
"""
raise NotImplementedError
@abstractmethod
def disable_live_autofocus(self):
"""
disable camera autofocus in preview mode
"""
raise NotImplementedError
def close(self):
"""
optional closing method
"""
pass
# Create singleton factory object
camera_factory = GenericClassFactory(AbstractCamera)
def get_camera_factory():
"""
Provide external access to the factory instance
:return: factory instance
"""
return camera_factory
try:
imp.find_module('piggyphoto')
found_piggyphoto_module = True
except ImportError:
found_piggyphoto_module = False
print("Couldn't find piggyphoto module, proceeding without. Run 'git submodule update --init' to clone the necessary submodule if the 'piggyphoto' option is enabled.")
if found_piggyphoto_module:
import piggyphoto.piggyphoto as piggyphoto
class PiggyphotoCamera(AbstractCamera):
"""
Class wrapping camera access through piggyphoto gphoto2 library for Nikon DSLR, you probably have to adjust it
for other camera brands
"""
def __init__(self, photo_directory, tmp_directory, **kwargs):
"""
:param photobooth: app instance
:param kwargs:
"""
self.cam = None
self.cam = piggyphoto.Camera(auto_init=True)
self._photo_directory = photo_directory
self._tmp_directory = tmp_directory
def set_memory_capture(self):
# set capturetarget to memory card
config_value = self.cam.config.get_child_by_name("capturetarget")
config_value.value = 'Memory card'
def set_idle(self):
self.disable_liveview()
def disable_liveview(self):
config_value = self.cam.config.get_child_by_name("viewfinder")
config_value.value = 0
def get_preview(self):
file = self._tmp_directory +'/preview.jpg'
cfile = self.cam.capture_preview(destpath=file)
cfile.clean()
picture = pygame.image.load(file)
return picture
def enable_live_autofocus(self):
config_value = self.cam.config.get_child_by_name("liveviewaffocus")
config_value.value = 'Full-time-servo AF'
def disable_live_autofocus(self):
config_value = self.cam.config.get_child_by_name("liveviewaffocus")
config_value.value = 'Single-servo AF'
def take_photo(self):
"""
Trigger photo capture
TODO it seems like capture_image of the library has a memory leak
:return: tuple of pygame image and path to file
"""
#disable liveview to use better internal autofocus of camera
self.disable_liveview()
file = self._photo_directory + "/dsc_" + str(datetime.now()).replace(':','-') + ".jpg"
self.cam.capture_image(destpath=file)
return pygame.image.load(file), file
def close(self):
if self.cam:
self.set_idle()
self.cam.close()
self.cam = None
def __del__(self):
self.close()
camera_factory.register_algorithm("piggyphoto", PiggyphotoCamera)
#following implementation is optional and only acivated if requirements are fulfilled
try:
imp.find_module('gphoto2cffi')
found_gphoto2_cffi_module = True
except ImportError:
found_gphoto2_cffi_module = False
print("Couldn't find gphoto2cffi module, proceeding without")
if found_gphoto2_cffi_module:
import gphoto2cffi as gp
class GPhoto2CffiCamera(AbstractCamera):
"""
Class wrapping camera access through gphoto2cffi library for Nikon DSLR, you probably have to adjust it
for other camera brands
"""
def __init__(self, photo_directory, tmp_directory, **kwargs):
"""
:param photobooth: app instance
:param kwargs:
"""
self.cam = gp.Camera()
self._photo_directory = photo_directory
self._tmp_directory = tmp_directory
def _save_picture(self, filename, data):
f = open(filename, 'wb')
f.write(data)
f.close()
def set_memory_capture(self):
# set capturetarget to memory card
self.cam.config['settings']['capturetarget'].set('Memory card')
def set_idle(self):
self.disable_liveview()
def disable_liveview(self):
try:
self.cam.config['actions']['viewfinder'].set(False)
except:
print("disable liveview failed")
def get_preview(self):
file = self._tmp_directory +'/preview.jpg'
preview = self.cam.get_preview()
self._save_picture(filename=file, data=preview)
picture = pygame.image.load(file)
return picture
def enable_live_autofocus(self):
try:
self.cam.config['capturesettings']['liveviewaffocus'].set('Full-time-servo AF')
except:
print('could not change liveview AF settings, please enable AF on lens')
def disable_live_autofocus(self):
try:
self.cam.config['capturesettings']['liveviewaffocus'].set('Single-servo AF')
except:
print('could not change liveview AF settings, please enable AF on lens')
def take_photo(self):
"""
Trigger photo capture
:return: tuple of pygame image and path to file
"""
#disable liveview to use better internal autofocus of camera
self.disable_liveview()
file = self._photo_directory + "/dsc_" + str(datetime.now()).replace(':','-') + ".jpg"
photo = self.cam.capture()
self._save_picture(filename=file, data=photo)
return pygame.image.load(file), file
def close(self):
pass
def __del__(self):
self.close()
camera_factory.register_algorithm("gphoto2cffi", GPhoto2CffiCamera)
class DummyCamera(AbstractCamera):
"""
Dummy camera class that just provides some example images for testing
"""
def __init__(self, photo_directory, tmp_directory, **kwargs):
# Load some dummy images
self._photo_directory = photo_directory
self._preview_cnt = 0
if tmp_directory:
self._tmp_directory = tmp_directory + '/'
else:
self._tmp_directory = ""
self._previews = [pygame.image.load(self._tmp_directory + "dummy_preview00.jpg"), pygame.image.load(self._tmp_directory + "dummy_preview01.jpg")]
self._photo = pygame.image.load(self._tmp_directory + "dummy_snap.jpg")
def set_memory_capture(self):
pass
def set_idle(self):
pass
def enable_live_autofocus(self):
pass
def disable_live_autofocus(self):
pass
def get_preview(self):
self._preview_cnt = (self._preview_cnt + 1) % len(self._previews)
return self._previews[self._preview_cnt]
def take_photo(self):
photo_file = self._photo_directory + "/dummy_snap_" +str(datetime.now()).replace(':','-')+ ".jpg"
copyfile(self._tmp_directory + "dummy_snap.jpg", photo_file)
return self._photo, photo_file
def __del__(self):
pass
camera_factory.register_algorithm("dummy", DummyCamera)
class GPhotoCMDCamera(AbstractCamera):
"""
Class wrapping camera access through piggyphoto gphoto2 library for Nikon DSLR, you probably have to adjust it
for other camera brands
"""
def __init__(self, photo_directory, tmp_directory, **kwargs):
"""
:param photobooth: app instance
:param kwargs:
"""
self._photo_directory = photo_directory
self._tmp_directory = tmp_directory
self._shell_p = None
def set_memory_capture(self):
# set capturetarget to memory card
# self._set_config('/main/settings/capturetarget', 'Memory card')
self._set_config_by_index('/main/settings/capturetarget', 1)
def set_idle(self):
self._disable_shell()
def disable_liveview(self):
self._set_config('/main/actions/viewfinder', 0)
def enable_liveview(self):
self._set_config('/main/actions/viewfinder', 1)
def get_preview(self):
"""
get a camera preview
:return: pygame image
"""
preview_picture = None
try:
if not self._shell_p:
self._enable_shell()
command = "capture-preview "
self._input_shell(command)
file = 'capture_preview.jpg'
preview_picture = pygame.image.load(file)
if not preview_picture:
self._disable_shell()
except Exception as e :
print(e)
pass
return preview_picture
def enable_live_autofocus(self):
try:
# self._set_config('/main/capturesettings/liveviewaffocus', 'Full-time-servo AF')
self._set_config_by_index('/main/capturesettings/liveviewaffocus', 1)
except:
print('could not change liveview AF settings, please enable AF on lens')
def disable_live_autofocus(self):
try:
# self._set_config('/main/capturesettings/liveviewaffocus', 'Single-servo AF')
self._set_config_by_index('/main/capturesettings/liveviewaffocus', 0)
except:
print('could not change liveview AF settings, please enable AF on lens')
def take_photo(self):
"""
Trigger photo capture
:return: tuple of pygame image and path to file
"""
# disable liveview to use better internal autofocus of camera by disabling shell mode
self._disable_shell()
file = self._photo_directory + "/dsc_" + str(datetime.now()).replace(':','-') + ".jpg"
command = "gphoto2 --capture-image-and-download --filename {filename}".format(filename=r'"%s"' % file)
self._execute_process(command)
return pygame.image.load(file), file
def _set_config_by_index(self, key, value):
"""
set a gphoto2 config value using the option index in order to avoid language problems
function automatically handles non-shell and shell mode
:param key: config key
:param value: config value
"""
value = int(value)
if not self._shell_p:
command = "gphoto2 --set-config-index {key}={value}".format(key=key, value=value)
self._execute_process(command)
else:
command = "set-config-index {key}={value}".format(key=key, value=value)
self._input_shell(command)
def _set_config(self, key, value):
"""
set a gphoto2 config value
function automatically handles non-shell and shell mode
:param key: config key
:param value: config value
"""
if not self._shell_p:
if isinstance(value, basestring):
command = "gphoto2 --set-config {key}={value}".format(key=key, value=r'"%s"' % value)
else:
command = "gphoto2 --set-config {key}={value}".format(key=key, value=value)
self._execute_process(command)
else:
if isinstance(value, basestring):
command = "set-config {key}={value}".format(key=key, value=value)
else:
command = "set-config {key}={value}".format(key=key, value=value)
self._input_shell(command)
def _execute_process(self, command):
"""
Execute a process command
:param command: full command string
"""
cmd = shlex.split(command)
p = subprocess.Popen(cmd, shell=False, stderr=subprocess.STDOUT)
p.wait()
def _enable_shell(self):
"""
enable gphoto2 interactive shell mode
"""
command = 'gphoto2 --shell --force-overwrite'
cmd = shlex.split(command)
if self._shell_p:
self._disable_shell()
self._shell_p = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
# set the O_NONBLOCK flag of p.stdout file descriptor:
flags = fcntl(self._shell_p.stderr, F_GETFL) # get current p.stdout flags
fcntl(self._shell_p.stderr, F_SETFL, flags | O_NONBLOCK)
time.sleep(0.5)
if self._shell_p.poll():
raise Exception("Gphoto2 shell failed ")
def _disable_shell(self):
"""
disable gphoto2 interactive shell mode
check if it was started at all
"""
if self._shell_p:
try:
self._shell_p.stdin.write('exit\n')
self._shell_p.wait()
self._shell_p = None
except Exception as e:
print("Failed closing shell mode: "+ str(e))
def _input_shell(self, cmd):
"""
send a command to a running interactive gphoto shell
:param cmd: the gphoto2 command
"""
if self._shell_p and self._shell_p.poll() is None:
self._shell_p.stdin.write(cmd + '\n')
self._shell_p.stdin.flush()
while True:
try:
res = self._shell_p.stderr.readline()
except Exception as e:
if e.errno == 11: # corresponds to "Resource temporarily unavailable" and indicates we do not have more data
break
else:
raise e
if 'Error' in res: # this is unfortunately language specific
self._disable_shell()
raise Exception("Shell cmd failed: " + res)
else:
self._shell_p = None
raise Exception("Gphoto2 shell mode not running")
def close(self):
if self._shell_p:
self.set_idle()
def __del__(self):
self.close()
camera_factory.register_algorithm("gphotocmd", GPhotoCMDCamera)
if __name__ == '__main__':
cam = GPhotoCMDCamera('images', 'tmp')
#cam = PiggyphotoCamera('images', 'tmp')
preview = cam.get_preview()
cam.enable_live_autofocus()
cnt = 0
while True:
#photo = cam.take_photo()
preview = cam.get_preview()
cnt +=1
#print(cnt)