-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcamera.py
577 lines (418 loc) · 18.8 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
import time, math
class Camera:
selectable_shutter_speeds = {
1/4:1/4, 1/8:1/8, 1/15:1/16, 1/30:1/32, 1/60:1/64, 1/125:1/128, 1/250:1/256, 1/500:1/512
}
selectable_film_speeds = (25, 50, 100, 200, 400, 800)
def __init__(self):
# set up sub-systems
self.back = Back(camera=self)
self.exposure_control_system = ExposureControlSystem(
mode="Shutter priority", camera=self, film_speed=100, battery=1.44
)
self.film_advance_mechanism = FilmAdvanceMechanism(camera=self)
self.film_rewind_mechanism = FilmRewindMechanism(camera=self)
self.lens_cap = LensCap(on=False)
self.film = Film(camera=self)
self.environment = Environment(scene_luminosity=4096)
# set up camera settings and indicators
self.frame_counter = 0
self.film_speed = 100
self.shutter_speed = 1/125
self.aperture = "A"
self.exposure_indicator = self.exposure_control_system.read_meter
self.shutter_button = ShutterButton(camera=self)
self.film_advance_lever = FilmAdvanceLever(camera=self)
# ----------- Camera settings -----------
# The camera will only allow the shutter speeds marked on the shutter ring to be applied, and will
# raise an exception if you try to set shutter_speed to an illegal value.
@property
def shutter_speed(self):
return self._shutter_speed
@shutter_speed.setter
def shutter_speed(self, value):
if not value in self.selectable_shutter_speeds:
possible_settings = ", ".join([f"1/{int(1/s)}" for s in self.selectable_shutter_speeds.keys()])
raise self.NonExistentShutterSpeed(f"Possible shutter speeds are {possible_settings}")
self.exposure_control_system.shutter.timer = self.selectable_shutter_speeds[value]
self._shutter_speed = value
class NonExistentShutterSpeed(Exception):
pass
# The camera will only allow the aperture settings marked on the aperture ring to be applied, and will
# raise an exception if you try to set aperture to an illegal value.
@property
def aperture(self):
return self._aperture
@aperture.setter
def aperture(self, value):
if value == "A":
self.exposure_control_system.mode = "Shutter priority"
elif not 1.7 <= value <= 16:
raise self.ApertureOutOfRange
else:
self.exposure_control_system.mode = "Manual"
self.exposure_control_system.aperture_set_lever.aperture = value
self._aperture = value
class ApertureOutOfRange(Exception):
pass
# The camera will only allow the film speed settings marked on the aperture ring to be applied, and will
# raise an exception if you try to set the film speed to an illegal value.
@property
def film_speed(self):
return self._film_speed
@film_speed.setter
def film_speed(self, value):
if not value in self.selectable_film_speeds:
possible_settings = ", ".join([f"{s}" for s in self.selectable_film_speeds])
raise self.NonExistentFilmSpeed(f"Possible film speeds are {possible_settings}")
self.exposure_control_system.film_speed = value
self._film_speed = value
class NonExistentFilmSpeed(Exception):
pass
# ----------- Reporting -----------
def state(self):
print("================== Camera state =================")
print()
print("------------------ Controls ---------------------")
print(f"Film speed: {self.film_speed} ISO")
print(f"Selected speed: 1/{int(1/self.shutter_speed)}")
print()
print("------------------ Indicators -------------------")
print(f"Exposure indicator {self.exposure_indicator()}")
print(f"Frame counter: {self.frame_counter}")
print()
print("------------------ Mechanical -------------------")
print(f"Back closed: {self.back.closed}")
print(f"Lens cap on: {self.lens_cap.on}")
print(f"Film advance mechanism: {self.film_advance_mechanism.advanced}")
print(f"Shutter cocked: {self.exposure_control_system.shutter.cocked}")
print(f"Shutter timer: 1/{int(1/self.exposure_control_system.shutter.timer)} seconds")
print(f"Iris aperture: ƒ/{self.exposure_control_system.iris.aperture:.2g}")
print(f"Camera exposure settings: {self.exposure_control_system.exposure_value()} EV")
print()
print("------------------ Metering ---------------------")
print(f"Metered light: {self.exposure_control_system.light_meter.reading()} cd/m^2")
print(f"Exposure target: {self.exposure_control_system.measured_ev()} EV")
print(f"Mode: {self.exposure_control_system.mode}")
print(f"Battery: {self.exposure_control_system.battery} V")
print(f"Film speed: {self.exposure_control_system.film_speed} ISO")
print()
print("------------------ Film -------------------------")
print(f"Speed: {self.film.speed} ISO")
print(f"Rewound into cartridge: {self.film.fully_rewound}")
print(f"Exposed frames: {self.film.frame} (of {self.film.frames})")
print(f"Ruined: {self.film.ruined}")
print()
print("------------------ Environment ------------------")
print(f"Scene luminosity: {self.environment.scene_luminosity} cd/m^2")
# ----------- Controls -----------
class ShutterButton(object):
def __init__(self, camera=None):
self.camera = camera
def press(self):
if not self.camera:
raise self.CannotBePressed
self.camera.exposure_control_system.shutter_release_lever.depress()
class CannotBePressed(Exception):
pass
class FilmAdvanceLever(object):
def __init__(self, camera=None):
self.camera = camera
def wind(self):
if not self.camera:
raise self.CannotBeWound
self.camera.film_advance_mechanism.advance()
class CannotBeWound(Exception):
pass
# ----------- Subsystems -----------
class FilmAdvanceMechanism:
def __init__(self, camera=None):
self.camera = camera
self.advanced = False
def advance(self):
if self.advanced:
raise self.AlreadyAdvanced
self.advanced = True
if self.camera:
if self.camera.film:
self.camera.film.advance()
if self.camera.exposure_control_system.shutter:
self.camera.exposure_control_system.shutter.cock()
class AlreadyAdvanced(Exception):
pass
class FilmRewindMechanism:
def __init__(self, camera=None):
self.camera = camera
def rewind(self):
if not self.camera.film:
return
self.camera.film.frame = 0
self.camera.film.fully_rewound = True
print("Rewinding film")
class ExposureControlSystem:
def __init__(self, mode="Shutter priority", film_speed=100, camera=None, battery=None):
self.mode = mode
self.film_speed = film_speed
self.camera = camera
self.battery = battery
self.light_meter = LightMeter(exposure_control_system=self, battery=self.battery)
self.shutter = Shutter(exposure_control_system=self)
self.iris = Iris(exposure_control_system=self)
self.shutter_release_lever = ShutterReleaseLever(exposure_control_system=self)
self.shutter_lock_lever = ShutterLockLever(exposure_control_system=self)
self.ee_lever = EELever(exposure_control_system=self)
self.exposure_level_lever = ExposureLevelLever(exposure_control_system=self)
self.exposure_bounds_lever = ExposureBoundsLever(exposure_control_system=self)
self.aperture_set_lever = ApertureSetLever(exposure_control_system=self)
# measured_ev is the exposure value from the system (that the aperture will need to
# respond to) and is determined by the light reading and the film-speed.
def measured_ev(self):
if self.light_meter.reading() is None:
return None
elif self.light_meter.reading() == 0:
return -math.inf
return math.log((self.light_meter.reading() * self.film_speed/12.5),2)
# the aperture that the system needs to set in order to match the measure_ev
def theoretical_aperture(self):
if self.measured_ev() is None:
return
return math.pow(2, self.measured_ev()/2) * math.sqrt(self.shutter.timer)
def meter(self):
if self.mode == "Manual" or self.theoretical_aperture() is None:
return
theoretical_aperture = self.theoretical_aperture()
if theoretical_aperture < 1.7 and math.isclose(theoretical_aperture, 1.7):
reading = 1.7
elif theoretical_aperture > 16 and math.isclose(theoretical_aperture, 16):
reading = 16
elif theoretical_aperture < 1.7:
reading = "Under"
elif theoretical_aperture > 16:
reading = "Over"
else:
reading = theoretical_aperture
return reading
# what the meter needle actually shows (but only if the camera is actually metering)
def read_meter(self):
if self.meter():
if type(self.meter()) is not str:
return f"ƒ/{self.meter():.2g}"
else:
return self.meter()
def exposure_value(self):
# returns the EV of the exposure system
if self.mode == "Manual":
return math.log(math.pow(self.iris.aperture, 2)/self.shutter.timer, 2)
else:
return "Shutter priority"
class Shutter:
def __init__(self, exposure_control_system=None, timer=1/128, closed=True, cocked=False):
self.exposure_control_system = exposure_control_system
self.timer = timer
self.closed = closed
self.cocked = cocked
def trip(self):
# The shutter may only be tripped if it's already cocked - otherwise,
# nothing at all happens.
if not self.closed or not self.cocked:
return
print(f"Shutter opening for 1/{int(1/self.timer)} seconds")
time.sleep(self.timer)
self.closed = True
print("Shutter closes")
self.cocked = False
print("Shutter uncocked")
if self.exposure_control_system and self.exposure_control_system.camera:
self.exposure_control_system.camera.film_advance_mechanism.advanced = False
return "Tripped"
def cock(self):
if self.cocked:
raise self.AlreadyCocked
print("Cocking shutter")
self.cocked = True
# cocking the shutter causes the set_aperture_lever value to be applied to the iris
if self.exposure_control_system:
if self.exposure_control_system.mode == "Shutter priority":
self.exposure_control_system.aperture_set_lever.aperture = 1.7
self.exposure_control_system.iris.aperture = self.exposure_control_system.aperture_set_lever.aperture
print(f"Applying aperture value ƒ/{self.exposure_control_system.aperture_set_lever.aperture:.2g} to iris")
print("Cocked")
return "Cocked"
class AlreadyCocked(Exception):
pass
class ShutterReleaseLever:
# part number 19-0562
def __init__(self, exposure_control_system=None):
self.exposure_control_system = exposure_control_system
def depress(self):
if not self.exposure_control_system:
return
self.exposure_control_system.exposure_level_lever.activate()
self.exposure_control_system.read_meter()
if self.exposure_control_system.shutter_lock_lever.blocks:
self.exposure_control_system.exposure_level_lever.deactivate()
print("Shutter release blocked")
return
if self.exposure_control_system.mode == "Shutter priority":
aperture = self.exposure_control_system.aperture_set_lever.aperture
if self.exposure_control_system.shutter.cocked:
self.exposure_control_system.iris.aperture = aperture
elif aperture > self.exposure_control_system.iris.aperture:
self.exposure_control_system.iris.aperture = aperture
print(f"Applying aperture value ƒ/{self.exposure_control_system.aperture_set_lever.aperture:.2g} to iris")
self.exposure_control_system.shutter.trip()
self.exposure_control_system.exposure_level_lever.deactivate()
class ExposureLevelLever:
# no individual part number available
def __init__(self, exposure_control_system=None):
self.exposure_control_system = exposure_control_system
def activate(self):
if not self.exposure_control_system:
return
self.exposure_control_system.exposure_bounds_lever.activate()
if not self.exposure_control_system.shutter_lock_lever.blocks:
meter_value = self.exposure_control_system.meter()
self.exposure_control_system.ee_lever.activate(meter_value)
return "Activated EE lever"
else:
return "Blocked"
def deactivate(self):
if not self.exposure_control_system:
return
self.exposure_control_system.ee_lever.deactivate()
class ExposureBoundsLever:
# no individual part number available
def __init__(self, exposure_control_system=None):
self.exposure_control_system = exposure_control_system
def activate(self):
if not self.exposure_control_system:
return
elif self.exposure_control_system.ee_lever.locked():
return "Blocked"
meter_value = self.exposure_control_system.meter()
if meter_value is None or meter_value == "Under" or meter_value == "Over":
self.exposure_control_system.shutter_lock_lever.activate()
return "Activated shutter lock lever"
def deactivate(self):
if not self.exposure_control_system:
return
self.exposure_control_system.shutter_lock_lever.activate()
class ShutterLockLever:
# part number 19-0566
def __init__(self, exposure_control_system=None):
self.exposure_control_system = exposure_control_system
self.blocks = False
def activate(self):
if not self.exposure_control_system:
return
self.blocks = True
def deactivate(self):
if not self.exposure_control_system:
return
self.blocks = False
class EELever:
def __init__(self, exposure_control_system=None):
self.exposure_control_system = exposure_control_system
if exposure_control_system:
self.position = 0 # 0 is rotated fully anti-clockwise; 1 is rotated fully clockwise
def activate(self, aperture_value):
if self.locked():
return "Locked"
self.exposure_control_system.aperture_set_lever.aperture = aperture_value
return aperture_value
def deactivate(self):
if not self.exposure_control_system:
return
def locked(self):
if not self.exposure_control_system:
return
return self.exposure_control_system.mode == "Manual"
class ApertureSetLever:
# part number Y13-5255 (I think)
# the lever is partially decoupled from the iris; only under certain circumstances does the iris
# respond to it
def __init__(self, exposure_control_system=None, aperture=16):
self.exposure_control_system = exposure_control_system
self._aperture = aperture
# self.aperture = aperture
@property
def aperture(self):
return self._aperture
@aperture.setter
def aperture(self, value):
# when the shutter is cocked, the aperture_set_lever follows the aperture setting
if self.exposure_control_system.mode == "Manual":
if self.exposure_control_system.shutter.cocked:
self.exposure_control_system.iris.aperture = value
elif value > self.aperture:
self.exposure_control_system.iris.aperture = value
self._aperture = value
class Iris:
def __init__(self, exposure_control_system=None, aperture=16):
self.exposure_control_system = exposure_control_system
self.aperture = aperture
class LightMeter:
def __init__(self, exposure_control_system=None, incident_light=0, battery=None):
self.exposure_control_system = exposure_control_system
self.incident_light = incident_light
self.battery = battery
def reading(self):
if not self.battery:
return
# If the light meter has not been removed from the camera, we will take the
# measurement from the camera's environment - if the lens cap is not on.
if not self.exposure_control_system:
# If the rest of the camera is not around, we can still measure incident light on the meter.
return self.incident_light
if self.exposure_control_system.camera.lens_cap.on:
return 0
return self.exposure_control_system.camera.environment.scene_luminosity
class Back:
# The back is closed by default.
def __init__(self, camera, closed=True):
self.closed = closed
self.camera = camera
def close(self):
if not self.closed:
self.closed = True
print("Closing back")
def open(self):
if not self.closed:
return
self.closed = False
print("Opening back")
self.camera.frame_counter = 0
if self.camera.film.frame == 0:
return
print("Resetting frame counter to 0")
if self.camera.environment.scene_luminosity > 0 and self.camera.film:
self.camera.film.ruined = True
return "Film is ruined"
# ----------- Other objects -----------
class LensCap:
def __init__(self, on=True):
self.on = on
class Film:
def __init__(self, speed=100, frames=24, camera=None, fully_rewound=False):
self.speed = speed
self.frames = frames
self.frame = 0
self.camera = camera
self.fully_rewound = fully_rewound
self.ruined = False
def advance(self):
if not self.frame < self.frames:
raise self.NoMoreFrames
if self.fully_rewound:
return
print(f"On frame {self.frame} (of {self.frames})")
print("Advancing film")
self.frame += 1
if self.camera and self.camera.back.closed == True:
self.camera.frame_counter += 1
print(f"On frame {self.frame} (of {self.frames})")
class NoMoreFrames(Exception):
pass
class Environment:
def __init__(self, scene_luminosity=4096):
self.scene_luminosity = scene_luminosity