-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_gphoto.py
executable file
·546 lines (453 loc) · 16.3 KB
/
camera_gphoto.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#!/usr/bin/python
import gphoto2 as gp
import subprocess
import logging
import os
import sys
import time
import io
import numpy as np
import cv2
from gui import ui
from cmd import cmdQueue
from stacktraces import stacktraces
import gc
log = logging.getLogger()
def apply_gamma(img, gamma):
lut = np.fromiter( ( (x / 255.0)**gamma * 65535.0 for x in xrange(256)), dtype=np.uint16 )
return np.take(lut, img)
class Camera_gphoto:
def __init__(self, status, focuser = None):
self.status = status
self.status.setdefault('iso', 400)
self.status.setdefault('test-iso', 3200)
self.status.setdefault('exp-sec', 120)
self.status.setdefault('test-exp-sec', 15)
self.status.setdefault('f-number', '5.6')
self.status['cur_time'] = 0
self.status['exp_in_progress'] = False
self.status['interrupt'] = False
self.focuser = focuser
self.fpshack = ''
self.fpshackiso = 0
self.status.setdefault('capture_idx', 0)
self.status['capture'] = False
self.status.setdefault('capture_path', None)
def get_config_value(self, name):
config = gp.check_result(gp.gp_camera_get_config(self.camera, self.context))
OK, widget = gp.gp_widget_get_child_by_name(config, name)
if OK >= gp.GP_OK:
# set value
value = gp.check_result(gp.gp_widget_get_value(widget))
log.info("get %s => %s", name, value)
return value
def set_config_choice(self, name, num):
for t in range(0, 20):
try:
config = gp.check_result(gp.gp_camera_get_config(self.camera, self.context))
OK, widget = gp.gp_widget_get_child_by_name(config, name)
if OK >= gp.GP_OK:
# set value
value = gp.check_result(gp.gp_widget_get_choice(widget, num))
log.info("set %s => %s", name, value)
gp.check_result(gp.gp_widget_set_value(widget, value))
# set config
gp.check_result(gp.gp_camera_set_config(self.camera, config, self.context))
break
except gp.GPhoto2Error as ex:
log.exception('failed')
time.sleep(0.1)
continue
def set_config_value(self, name, value):
for t in range(0, 20):
try:
config = gp.check_result(gp.gp_camera_get_config(self.camera, self.context))
OK, widget = gp.gp_widget_get_child_by_name(config, name)
if OK >= gp.GP_OK:
log.info("set %s => %s", name, value)
# set value
gp.check_result(gp.gp_widget_set_value(widget, value))
# set config
gp.check_result(gp.gp_camera_set_config(self.camera, config, self.context))
break
except gp.GPhoto2Error as ex:
log.exception('failed')
time.sleep(0.1)
continue
def set_config_value_checked(self, name, value):
value = str(value)
ret = False
for t in range(0, 20):
try:
config = gp.check_result(gp.gp_camera_get_config(self.camera, self.context))
OK, widget = gp.gp_widget_get_child_by_name(config, name)
if OK >= gp.GP_OK:
num = None
choice_count = gp.check_result(gp.gp_widget_count_choices(widget))
log.info("count %d", choice_count)
for i in range(choice_count):
vi = gp.check_result(gp.gp_widget_get_choice(widget, i))
if vi.lower() == value.lower():
num = i
value = vi
break
try:
if abs(float(vi) - float(value)) < 0.000001:
value = vi
num = i
break
except ValueError:
pass
try:
if '/' in vi:
fr = vi.split('/')
fr = float(fr[0]) / float(fr[1])
if abs(fr - float(value)) < abs(fr * 0.001):
value = vi
num = i
break
except:
pass
if num is not None:
log.info("set %s => %s (choice %d)" % (name, value, num))
# set value
gp.check_result(gp.gp_widget_set_value(widget, value))
ret = True
else:
log.info("cant't set %s => %s" % (name, value))
# set config
gp.check_result(gp.gp_camera_set_config(self.camera, config, self.context))
break
except gp.GPhoto2Error as ex:
log.exception('failed')
time.sleep(0.1)
ret = False
continue
return ret
def do_fps_hack(self):
if self.status['exp-sec'] < 1.0:
return
if self.fpshack == 'output':
time.sleep(.2)
self.set_config_choice('output', 1)
time.sleep(.2)
self.set_config_choice('output', 0)
time.sleep(3)
elif self.fpshack == 'iso':
self.fpshackiso = 5
def capture_bulb(self, test = False, callback_start = None, callback_end = None):
if test:
sec = self.status['test-exp-sec']
self.set_config_value_checked('iso', self.status['test-iso'])
try:
self.status['test-iso'] = int(self.get_config_value('iso'))
except:
pass
#self.set_config_choice('capturetarget', 0) #mem
self.set_config_choice('capturetarget', 1) #card
self.set_config_choice('imageformat', 1) #Large Normal JPEG
else:
sec = self.status['exp-sec']
self.set_config_value_checked('iso', self.status['iso'])
try:
self.status['iso'] = int(self.get_config_value('iso'))
except:
pass
self.set_config_choice('capturetarget', 1) #card
#self.set_config_choice('imageformat', 24) #RAW
self.set_config_choice('imageformat', 7) #RAW + Large Normal JPEG
self.set_config_value('aperture', self.status['f-number'])
self.status['f-number'] = self.get_config_value('aperture')
if callback_start is not None:
try:
callback_start()
except:
log.exception('Unexpected error')
self.t_start = time.time()
while True:
e, file_path = gp.check_result(gp.gp_camera_wait_for_event(self.camera, 100,self.context))
t = time.time() - self.t_start
log.info("camera event %f %s %s", t, e, file_path)
if t > 1 or file_path is None:
break
if sec <= 4:
bulbmode = None
self.set_config_value_checked('autoexposuremode', 'Manual')
self.set_config_value_checked('shutterspeed', sec)
for t in range(0, 1):
try:
log.info("trgger capture")
gp.check_result(gp.gp_camera_trigger_capture(self.camera, self.context))
except gp.GPhoto2Error as ex:
log.exception('failed')
time.sleep(0.1)
continue
else:
self.set_config_value_checked('autoexposuremode', 'Manual')
if not self.set_config_value_checked('shutterspeed', 'Bulb'):
self.set_config_value_checked('autoexposuremode', 'Bulb')
bulbmode = 'eosremoterelease'
if not self.set_config_value_checked('eosremoterelease', 'Immediate'):
self.set_config_value('bulb', 1)
bulbmode = 'bulb'
self.t_start = time.time()
t = 0
self.status['exp_in_progress'] = True
self.status['interrupt'] = False
while True:
#if t < sec - 4 and not self.status['interrupt']:
# time.sleep(3)
e, file_path = gp.check_result(gp.gp_camera_wait_for_event(self.camera, 1000,self.context))
t = time.time() - self.t_start
log.info("camera event %f %s %s", t, e, file_path)
try:
ev = str(file_path)
if ev.startswith("BulbExposureTime "):
tp = int(ev[len("BulbExposureTime "):])
log.info("exp time parsed %f", tp)
self.t_start += t - tp
t = tp
except:
pass
if self.status['exp_in_progress']:
self.status['cur_time'] = int(t)
if self.status['exp_in_progress'] and (t >= sec or self.status['interrupt']):
if bulbmode == 'bulb':
self.set_config_value('bulb', 0)
elif bulbmode == 'eosremoterelease':
self.set_config_value_checked('eosremoterelease', 'Release Full')
self.status['exp_in_progress'] = False
if not self.status['exp_in_progress']:
log.info("waiting for image")
if e == gp.GP_EVENT_FILE_ADDED:
log.info("filepath: %s %s", file_path.folder, file_path.name)
filename, file_extension = os.path.splitext(file_path.name)
if file_extension == ".jpg" or file_extension == ".JPG":
break
if t > sec + 60:
file_path = None
log.info("image timeout")
break
self.status['exp_in_progress'] = False
self.status['cur_time'] = 0
self.status['interrupt'] = False
file_data = None
filename = None
if file_path is not None:
filename = file_path.name
target = os.path.join('/tmp', file_path.name)
n = 20
while True:
n -= 1
try:
camera_file = gp.check_result(gp.gp_camera_file_get(self.camera, file_path.folder, file_path.name, gp.GP_FILE_TYPE_NORMAL, self.context))
file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file))
break
except gp.GPhoto2Error as ex:
if ex.code == gp.GP_ERROR_CAMERA_BUSY:
time.sleep(1)
if (n > 0):
continue
if callback_end is not None:
try:
callback_end(memoryview(file_data).tobytes(), filename)
except:
log.exception('Unexpected error')
log.info('callback end')
#stop review on display
self.set_config_value_checked('eosremoterelease', 'Press Half')
self.set_config_value_checked('eosremoterelease', 'Release Half')
log.info('review end')
self.do_fps_hack()
self.set_config_value_checked('iso', 100)
log.info('fps hack end')
def prepare(self):
self.shape = (704, 1056)
self.zoom_shape = (680, 1024)
subprocess.call(['killall', 'gvfsd-gphoto2'])
subprocess.call(['killall', 'gvfs-gphoto2-volume-monitor'])
gp.check_result(gp.use_python_logging())
self.context = gp.gp_context_new()
while True:
try:
self.camera = gp.check_result(gp.gp_camera_new())
gp.check_result(gp.gp_camera_init(self.camera, self.context))
break
except gp.GPhoto2Error as ex:
log.info("gphoto2 camera is not ready")
time.sleep(2)
continue
# wake up the camera
self.set_config_value_checked('eosremoterelease', 'Press Half')
self.set_config_value_checked('eosremoterelease', 'Release Half')
self.set_config_value_checked('eosremoterelease', 'Release Full')
self.set_config_value('bulb', 0)
self.cameramodel = self.get_config_value('cameramodel')
log.info(self.cameramodel)
if self.cameramodel == "Canon EOS 40D":
self.shape = (680, 1024)
self.zoom_shape = (800, 768)
self.fpshack = 'iso'
self.zoom_scale = 3.8
elif self.cameramodel == "Canon EOS 7D":
self.shape = (704, 1056)
self.zoom_shape = (680, 1024)
self.fpshack = 'output'
self.zoom_scale = 5
cur_time = self.get_config_value('datetime')
if cur_time is None:
cur_time = self.get_config_value('datetimeutc')
try:
cur_time = int(cur_time)
if cur_time - time.time() > 1500:
log.info("adjusting time %s %s", time.time(), cur_time)
subprocess.call(['date', '--set', '@' + str(cur_time) ])
except:
pass
self.set_config_value_checked('aperture', self.status['f-number'])
self.status['f-number'] = self.get_config_value('aperture')
self.status['lensname'] = self.get_config_value('lensname')
self.set_config_choice('drivemode', 0)
self.set_config_value_checked('autoexposuremode', 'Manual')
if not self.set_config_value_checked('shutterspeed', 'Bulb'):
self.set_config_value_checked('autoexposuremode', 'Bulb')
# required configuration will depend on camera type!
self.set_config_choice('capturesizeclass', 2)
self.do_fps_hack()
self.zoom = 1
self.x = (self.shape[1] * self.zoom_scale - self.zoom_shape[1]) / 2
self.y = (self.shape[0] * self.zoom_scale - self.zoom_shape[0]) / 2
self.status['zoom_pos'] = [int(v) for v in [ self.x / self.zoom_scale, self.y / self.zoom_scale, (self.x + self.zoom_shape[1]) / self.zoom_scale, (self.y + self.zoom_shape[0]) / self.zoom_scale]]
self.set_config_value('eoszoomposition', "%d,%d" % (self.x, self.y))
time.sleep(3)
def cmd(self, cmd, x = None, y = None):
try:
if cmd in ["f-3", "f-2", "f-1", "f+3", "f+2", "f+1"]:
if self.focuser is not None:
self.focuser.cmd(cmd)
if cmd == "f-3":
self.set_config_choice('manualfocusdrive', 2)
if cmd == "f-2":
self.set_config_choice('manualfocusdrive', 1)
if cmd == "f-1":
self.set_config_choice('manualfocusdrive', 0)
if cmd == "f+1":
self.set_config_choice('manualfocusdrive', 4)
if cmd == "f+2":
self.set_config_choice('manualfocusdrive', 5)
if cmd == "f+3":
self.set_config_choice('manualfocusdrive', 6)
if cmd == "z1":
zoom = 5
#self.x = x * zoom - self.zoom_shape[1] / 2
#self.y = y * zoom - self.zoom_shape[0] / 2
self.set_config_value('eoszoomposition', "%d,%d" % (self.x, self.y))
self.set_config_value('eoszoom', '5')
time.sleep(.2)
self.do_fps_hack()
im, t = self.capture()
while im.shape[0] != self.zoom_shape[0] or im.shape[1] != self.zoom_shape[1]:
log.info("zoom shape %s %s", im.shape, self.zoom_shape)
im, t = self.capture()
if cmd == "z0":
zoom = 1
self.set_config_value('eoszoom', '1')
time.sleep(.2)
self.do_fps_hack()
im, t = self.capture()
while im.shape[0] != self.shape[0] or im.shape[1] != self.shape[1]:
log.info("shape %s %s", im.shape, self.shape)
im, t = self.capture()
if cmd == "zcenter":
self.x = (self.shape[1] * self.zoom_scale - self.zoom_shape[1]) / 2
self.y = (self.shape[0] * self.zoom_scale - self.zoom_shape[0]) / 2
self.status['zoom_pos'] = [int(v) for v in [ self.x / self.zoom_scale, self.y / self.zoom_scale, (self.x + self.zoom_shape[1]) / self.zoom_scale, (self.y + self.zoom_shape[0]) / self.zoom_scale]]
if cmd == "zpos":
self.x = x * self.zoom_scale - self.zoom_shape[1] / 2
self.y = y * self.zoom_scale - self.zoom_shape[0] / 2
self.status['zoom_pos'] = [int(v) for v in [ self.x / self.zoom_scale, self.y / self.zoom_scale, (self.x + self.zoom_shape[1]) / self.zoom_scale, (self.y + self.zoom_shape[0]) / self.zoom_scale]]
if cmd == 'left':
self.x = max(100, self.x - 100)
self.set_config_value('eoszoomposition', "%d,%d" % (self.x, self.y))
if cmd == 'right':
self.x = self.x + 100
self.set_config_value('eoszoomposition', "%d,%d" % (self.x, self.y))
if cmd == 'up':
self.y = max(100, self.y - 100)
self.set_config_value('eoszoomposition', "%d,%d" % (self.x, self.y))
if cmd == 'down':
self.y = self.y + 100
self.set_config_value('eoszoomposition', "%d,%d" % (self.x, self.y))
if cmd.startswith('iso-'):
self.status['iso'] = cmd[len('iso-'):]
if cmd.startswith('test-iso-'):
self.status['test-iso'] = cmd[len('test-iso-'):]
if cmd.startswith('exp-sec-'):
try:
self.status['exp-sec'] = float(cmd[len('exp-sec-'):])
except:
pass
if cmd.startswith('test-exp-sec-'):
try:
self.status['test-exp-sec'] = float(cmd[len('test-exp-sec-'):])
except:
pass
if cmd.startswith('f-number-'):
self.set_config_value('aperture', cmd[len('f-number-'):])
self.status['f-number'] = self.get_config_value('aperture')
if cmd == 'capture_start' and self.status['capture_path'] is not None:
self.status['capture'] = True
elif cmd == 'capture_stop':
self.status['capture'] = False
except gp.GPhoto2Error as ex:
log.exception('Unexpected error')
time.sleep(1)
if ex.code == -7 or ex.code == -1:
gp.gp_camera_exit(self.camera, self.context)
self.prepare()
def capture(self):
while True:
try:
for i in range(0,20):
try:
camera_file = gp.check_result(gp.gp_camera_capture_preview(self.camera, self.context))
break
except gp.GPhoto2Error as ex:
if i < 19:
continue
raise
file_data = gp.check_result(gp.gp_file_get_data_and_size(camera_file))
if self.status['capture'] and self.status['capture_path'] is not None:
i = self.status['capture_idx']
while os.path.isfile(self.status['capture_path'] + 'capt%04d.jpg' % i):
i += 1
f = open(self.status['capture_path'] + 'capt%04d.jpg' % i, "wb")
f.write(file_data)
f.close()
log.info("saved {}".format(i))
i += 1
self.status['capture_idx'] = i
im = cv2.imdecode(np.fromstring(memoryview(file_data).tobytes(), dtype=np.uint8), -1)
im = apply_gamma(im, 2.2)
if self.fpshackiso > 0:
self.set_config_value_checked('iso', 1600)
self.set_config_value_checked('iso', 100)
self.fpshackiso -= 1
return im, time.time()
except KeyboardInterrupt:
break
except gp.GPhoto2Error as ex:
log.exception('Unexpected error')
time.sleep(1)
if ex.code == -7 or ex.code == -1 or ex.code == -52:
gp.gp_camera_exit(self.camera, self.context)
self.prepare()
def shutdown(self):
time.sleep(1)
self.set_config_value_checked('eosremoterelease', 'Release Full')
self.set_config_value('viewfinder', 0)
gp.check_result(gp.gp_camera_exit(self.camera, self.context))
if __name__ == "__main__":
cam = Camera_gphoto()
cam.prepare()
cam.capture()