-
Notifications
You must be signed in to change notification settings - Fork 6
/
camera.py
609 lines (498 loc) · 17.3 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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
from functions import Echo, Console
from libcamera import ColorSpace, controls, Transform
from picamera2 import MappedArray, Picamera2, Preview
from picamera2.controls import Controls
from picamera2.outputs import FileOutput
from picamera2.encoders import H264Encoder, Quality
from controls import Light, TrackballController
from PIL import Image
import argparse
import datetime
import fractions
import globals
import os
import piexif
import subprocess
import sys
import threading
import time
version = '2024.02.09'
console = Console()
echo = Echo()
globals.initialize()
camera = Picamera2()
camera.set_logging(Picamera2.ERROR)
controls = Controls(camera)
stillConfiguration = camera.create_still_configuration(main={"size": camera.sensor_resolution}, colour_space = ColorSpace.Sycc())
videoConfiguration = camera.create_video_configuration(main={"size": (1920, 1080)}, colour_space = ColorSpace.Rec709())
try:
camera.set_controls({"AfMode": controls.AfModeEnum.Continuous})
except Exception as ex:
console.info('Camera does not support autofocus.')
time.sleep(3)
pass
# === Argument Handling ========================================================
parser = argparse.ArgumentParser()
parser.add_argument('--rotate', dest='rotate', help='Rotate the camera in 90* increments', type=int)
parser.add_argument('--exifFStop', dest='exifFStop', help='Set the numeric F-Stop value in the image EXIF data', type=float)
parser.add_argument('--exifFocalLength', dest='exifFocalLength', help='Set the numeric Focal Length value (mm) in the image EXIF data', type=float)
parser.add_argument('--exifFocalLengthEquivalent', dest='exifFocalLengthEquivalent', help='Set the numeric 35mm Focal Length value (mm) in the image EXIF data', type=float)
args = parser.parse_args()
running = False
buttons = TrackballController()
previewVisible = False
previewWidth = 800
previewHeight = 460
rotate = args.rotate or 0
try:
rotate = int(rotate)
except:
rotate = 0
shutter = 'auto'
shutterLong = 30000
shutterLongThreshold = 1000
shutterShort = 0
defaultFramerate = 30
iso = 'auto'
isoMin = 100
isoMax = 1600
exposure = 'auto'
ev = 0
evMin = -25
evMax = 25
bracket = 0
bracketLow = 0
bracketHigh = 0
awb = 'auto'
outputFolder = 'dcim/'
timer = 0
raw = False
try:
from pidng.core import RPICAM2DNG
dng = RPICAM2DNG()
raw = False # Going to disable this by default as the Pi Zero W struggles with it
except:
console.info( ' WARNING: DNG file format not currently supported on this device. ')
# === Data Objects ============================================================
class EXIFData:
def __init__(self, Orientation = 1, FStop = None, FocalLength = None, FocalLengthEquivalent = None):
self.Orientation = Orientation
self.FStop = FStop
self.FocalLength = FocalLength
self.FocalLengthEquivalent = FocalLengthEquivalent
EXIFDataOverride = EXIFData()
EXIFDataOverride.FStop = args.exifFStop
EXIFDataOverride.FocalLength = args.exifFocalLength
EXIFDataOverride.FocalLengthEquivalent = args.exifFocalLengthEquivalent
# === Functions ================================================================
def setShutter(input, wait = 0):
global controls
global shutter
global shutterLong
global shutterLongThreshold
global shutterShort
global defaultFramerate
if str(input).lower() == 'auto' or str(input) == '0':
shutter = 0
else:
shutter = int(float(input))
if shutter < shutterShort:
shutter = shutterShort
elif shutter > shutterLong:
shutter = shutterLong
try:
if camera.framerate == defaultFramerate and shutter > shutterLongThreshold:
camera.framerate=fractions.Fraction(5, 1000)
elif camera.framerate != defaultFramerate and shutter <= shutterLongThreshold:
camera.framerate = defaultFramerate
except Exception as ex:
# console.info( ' WARNING: Could not set framerate!')
pass
try:
if shutter == 0:
controls.ExposureTime = 0
# print(str(controls.ExposureTime) + '|' + str(controls.FrameRate) + '|' + str(shutter))
print(' Shutter Speed: auto')
globals.statusDictionary.update({'message': 'Shutter Speed: auto'})
else:
controls.ExposureTime = shutter * 1000
# print(str(controls.ExposureTime) + '|' + str(controls.FrameRate) + '|' + str(shutter))
floatingShutter = float(shutter/1000)
roundedShutter = '{:.3f}'.format(floatingShutter)
if shutter > shutterLongThreshold:
print(' Shutter Speed: ' + str(roundedShutter) + 's [Long Exposure Mode]')
globals.statusDictionary.update({'message': ' Shutter Speed: ' + str(roundedShutter) + 's [Long Exposure Mode]'})
else:
print(' Shutter Speed: ' + str(roundedShutter) + 's')
globals.statusDictionary.update({'message': ' Shutter Speed: ' + str(roundedShutter) + 's'})
time.sleep(wait)
return
except Exception as ex:
console.info('WARNING: Invalid Shutter Speed! ' + str(shutter) + str(ex))
# ------------------------------------------------------------------------------
def setISO(input, wait = 0):
global controls
global iso
global isoMin
global isoMax
if str(input).lower() == 'auto' or str(input) == '0':
controls.AeEnable = 1
iso = 0
else:
controls.AeEnable = 0
iso = int(input)
if iso < isoMin:
iso = isoMin
elif iso > isoMax:
iso = isoMax
try:
analogGain = iso/100
controls.AnalogueGain = analogGain
# print(str(camera.iso) + '|' + str(iso))
if iso == 0:
print(' ISO: auto')
else:
print(' ISO: ' + str(iso))
globals.statusDictionary.update({'message': ' ISO: ' + str(iso)})
time.sleep(wait)
return
except Exception as ex:
console.info('WARNING: Invalid ISO Setting! ' + str(iso) + str(ex))
# ------------------------------------------------------------------------------
def setExposure(input, wait = 0):
global exposure
exposure = input
try:
controls.AeExposureMode = exposure
print(' Exposure Mode: ' + exposure)
globals.statusDictionary.update({'message': ' Exposure Mode: ' + exposure})
time.sleep(wait)
return
except Exception as ex:
console.info('WARNING: Invalid Exposure Mode! ' + str(ex))
# ------------------------------------------------------------------------------
def setEV(input, wait = 0, displayMessage = True):
global controls
global ev
global bracket
ev = input
ev = int(ev)
evPrefix = '+/-'
if ev > 0:
evPrefix = '+'
elif ev < 0:
evPrefix = ''
try:
controls.ExposureValue = ev
# print(str(camera.exposure_compensation) + '|' + str(ev))
if displayMessage == True:
print(' Exposure Compensation: ' + evPrefix + str(ev))
globals.statusDictionary.update({'message': ' Exposure Compensation: ' + evPrefix + str(ev)})
time.sleep(wait)
return
except Exception as ex:
console.info('WARNING: Invalid Exposure Compensation Setting! ' + str(ex))
# ------------------------------------------------------------------------------
def setBracket(input, wait = 0, displayMessage = True):
global controls
global bracket
global bracketLow
global bracketHigh
global evMax
global evMin
bracket = int(input)
try:
bracketLow = controls.ExposureValue - bracket
if bracketLow < evMin:
bracketLow = evMin
bracketHigh = controls.ExposureValue + bracket
if bracketHigh > evMax:
bracketHigh = evMax
if displayMessage == True:
print(' Exposure Bracketing: ' + str(bracket))
globals.statusDictionary.update({'message': ' Exposure Bracketing: ' + str(bracket)})
time.sleep(wait)
return
except Exception as ex:
console.info('WARNING: Invalid Exposure Bracketing Value! ' + str(ex))
# ------------------------------------------------------------------------------
def setAWB(input, wait = 0):
global controls
global awb
awb = input
try:
controls.AwbMode = awb
print(' White Balance Mode: ' + awb)
globals.statusDictionary.update({'message': ' White Balance Mode: ' + awb})
time.sleep(wait)
return
except Exception as ex:
console.info('WARNING: Invalid Auto White Balance Mode! ' + str(ex))
# ------------------------------------------------------------------------------
def getFileName(timestamped = True, isVideo = False):
now = datetime.datetime.now()
datestamp = now.strftime('%Y%m%d')
timestamp = now.strftime('%H%M%S')
if isVideo==True:
extension = '.h264'
return datestamp + '-' + timestamp + extension
else:
extension = '.jpg'
if timestamped == True:
return datestamp + '-' + timestamp + '-' + str(imageCount).zfill(2) + extension
else:
return datestamp + '-' + str(imageCount).zfill(8) + extension
# ------------------------------------------------------------------------------
def getfilePath(timestamped = True, isVideo = False):
try:
os.makedirs(outputFolder, exist_ok = True)
except OSError:
console.error(' ERROR: Creation of the output folder ' + outputFolder + ' failed!')
echo.on()
quit()
else:
return outputFolder + getFileName(timestamped, isVideo)
# ------------------------------------------------------------------------------
def postProcessImage(filePath, angle):
global EXIFDataOverride
try:
image = Image.open(filePath)
FileEXIFData = piexif.load(filePath)
if angle > 0:
newOrientation = 1
if angle == 90:
newOrientation = 6
image = image.rotate(-90, expand=True)
elif angle == 180:
newOrientation = 3
image = image.rotate(180, expand=True)
elif angle == 270:
newOrientation = 8
image = image.rotate(90, expand=True)
EXIFDataOverride.Orientation = newOrientation
FileEXIFData['Orientation'] = EXIFDataOverride.Orientation
try:
if EXIFDataOverride.FStop is not None:
FileEXIFData['Exif'][piexif.ExifIFD.FNumber] = (int(EXIFDataOverride.FStop * 100), 100)
if EXIFDataOverride.FocalLength is not None:
FileEXIFData['Exif'][piexif.ExifIFD.FocalLength] = (int(EXIFDataOverride.FocalLength * 100), 100)
if EXIFDataOverride.FocalLengthEquivalent is not None:
FileEXIFData['Exif'][piexif.ExifIFD.FocalLengthIn35mmFilm] = int(EXIFDataOverride.FocalLengthEquivalent)
except Exception as ex:
console.warn('Could not rotate apply additional EXIF data to image. Please check supplied data. ' + str(ex))
pass
EXIFBytes = piexif.dump(FileEXIFData)
image.save(filePath, exif=EXIFBytes)
except Exception as ex:
console.warn('Could not rotate ' + filePath + ' ' + str(angle) + ' degrees. ' + str(ex))
pass
# ------------------------------------------------------------------------------
def captureImage(filePath, raw = True):
global rotate
request = camera.switch_mode_and_capture_request(stillConfiguration)
request.save('main', filePath)
request.release()
postProcessImage(filePath, rotate)
if raw == True:
conversionThread = threading.Thread(target=convertBayerDataToDNG, args=(filePath,))
conversionThread.daemon = True
conversionThread.start()
# ------------------------------------------------------------------------------
def convertBayerDataToDNG(filePath):
try:
dng.convert(filePath)
except:
pass
# ------------------------------------------------------------------------------
def createControls():
global running
running = True
TrackballController.watch(running)
# -------------------------------------------------------------------------------
def darkMode():
Light.off()
TrackballController.off()
# === Image Capture ============================================================
controlsThread = threading.Thread(target=createControls)
controlsThread.daemon = True
controlsThread.start()
try:
echo.clear()
os.chdir('/home/pi')
console.info('Camera Zero ' + version )
console.print('----------------------------------------------------------------------', '\n ', '\n ')
try:
if "SSH_CONNECTION" in os.environ:
camera.start_preview(Preview.QT)
else:
camera.start_preview(Preview.QTGL, 0, 0, previewWidth, previewHeight) # transform=Transform(hflip=1)
except:
#console.warn('Could not start preview. ', '\n ')
pass
try:
camera.start(show_preview=False)
time.sleep(1.0)
except:
console.warn('Could not start camera. Is it already in use? ', '\n ')
pass
setShutter(shutter, 0)
setISO(iso, 0)
setExposure(exposure, 0)
setEV(ev, 0)
setBracket(bracket, 0)
setAWB(awb, 0)
imageCount = 1
isRecording = False
mode = 'persistent'
while True:
try:
# Exit / Spawn
if globals.buttonDictionary['exit'] == True or globals.buttonDictionary['remote'] == True:
running = False
darkMode()
echo.on()
camera.close()
try:
# Tell service to not auto restart after it is killed
subprocess.run('sudo svc -d /etc/service/camera.zero', shell=True)
# Kill the current running service
subprocess.run('sudo svc -k /etc/service/camera.zero', shell=True)
except Exception as ex:
console.warn(ex)
pass
time.sleep(1.0)
if globals.buttonDictionary['remote'] == True:
try:
console.info('Launching remote control...')
subprocess.Popen(['sudo', '/usr/bin/python3', '/home/pi/camera.remote/camera.py'])
except Exception as ex:
console.info('Could not launch remote control.')
sys.exit(0)
# Capture
elif globals.buttonDictionary['capture'] == True:
if mode == 'persistent':
# Normal photo
filePath = getfilePath(True)
console.info('Capturing image: ' + filePath + '\n')
captureImage(filePath, raw)
imageCount += 1
if (bracket != 0):
baseEV = ev
# Take underexposed photo
setEV(baseEV + bracketLow, 0, False)
filePath = getfilePath(True)
console.info('Capturing image: ' + filePath + ' [' + str(bracketLow) + ']\n')
captureImage(filePath, raw)
imageCount += 1
# Take overexposed photo
setEV(baseEV + bracketHigh, 0, False)
filePath = getfilePath(True)
console.info('Capturing image: ' + filePath + ' [' + str(bracketHigh) + ']\n')
captureImage(filePath, raw)
imageCount += 1
# Reset EV to base photo's value
setEV(baseEV, 0, False)
elif mode == 'timelapse':
# Timelapse photo series
if timer < 0:
timer = 1
while True:
filePath = getfilePath(False)
console.info('Capturing timelapse image: ' + filePath + '\n')
captureImage(filePath, raw)
imageCount += 1
time.sleep(timer)
else:
# Single photo and then exit
filePath = getfilePath(True)
console.info('Capturing single image: ' + filePath + '\n')
captureImage(filePath, raw)
echo.on()
break
globals.buttonDictionary.update({'capture': False})
elif globals.buttonDictionary['captureVideo'] == True:
# Video
if isRecording == False:
camera.stop()
isRecording = True
globals.statusDictionary.update({'action': 'recording'})
filePath = getfilePath(True, True)
camera.resolution = (1920, 1080)
console.info('Capturing video: ' + filePath + '\n')
globals.statusDictionary.update({'message': ' Recording: Started '})
globals.buttonDictionary.update({'captureVideo': False})
camera.configure(videoConfiguration)
encoder = H264Encoder()
camera.start_recording(encoder, filePath, quality=Quality.VERY_HIGH)
else:
isRecording = False
globals.statusDictionary.update({'action': ''})
camera.stop_recording()
console.info('Capture complete \n')
globals.statusDictionary.update({'message': ' Recording: Stopped '})
globals.buttonDictionary.update({'captureVideo': False})
camera.start()
time.sleep(1)
# Shutter Speed
elif globals.buttonDictionary['shutterUp'] == True:
if shutter == 0:
shutter = shutterShort
elif shutter > shutterShort and shutter <= shutterLong:
shutter = int(shutter / 1.5)
setShutter(shutter, 0.25)
globals.buttonDictionary.update({'shutterUp': False})
elif globals.buttonDictionary['shutterDown'] == True:
if shutter == 0:
shutter = shutterLong
elif shutter < shutterLong and shutter >= shutterShort:
shutter = int(shutter * 1.5)
elif shutter == shutterShort:
shutter = 0
setShutter(shutter, 0.25)
globals.buttonDictionary.update({'shutterDown': False})
# ISO
elif globals.buttonDictionary['isoUp'] == True:
if iso == 0:
iso = isoMin
elif iso >= isoMin and iso < isoMax:
iso = int(iso * 2)
setISO(iso, 0.25)
globals.buttonDictionary.update({'isoUp': False})
elif globals.buttonDictionary['isoDown'] == True:
if iso == 0:
iso = isoMax
elif iso <= isoMax and iso > isoMin:
iso = int(iso / 2)
elif iso == isoMin:
iso = 0
setISO(iso, 0.25)
globals.buttonDictionary.update({'isoDown': False})
# Exposure Compensation
elif globals.buttonDictionary['evUp'] == True:
if ev >= evMin and ev < evMax:
ev = int(ev + 1)
setEV(ev, 0.25)
globals.buttonDictionary.update({'evUp': False})
elif globals.buttonDictionary['evDown'] == True:
if ev <= evMax and ev > evMin:
ev = int(ev - 1)
setEV(ev, 0.25)
globals.buttonDictionary.update({'evDown': False})
# Exposure Bracketing
elif globals.buttonDictionary['bracketUp'] == True:
if bracket < evMax:
bracket = int(bracket + 1)
setBracket(bracket, 0.25)
globals.buttonDictionary.update({'bracketUp': False})
elif globals.buttonDictionary['bracketDown'] == True:
if bracket > 0:
bracket = int(bracket - 1)
setBracket(bracket, 0.25)
globals.buttonDictionary.update({'bracketDown': False})
except Exception as ex:
console.error(str(ex))
pass
except KeyboardInterrupt:
darkMode()
echo.on()
sys.exit(0)