This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
forked from kpolley/Python_AVrecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_exposure.py
125 lines (99 loc) · 3.15 KB
/
set_exposure.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
import io
import sys
import math
from pathlib import Path
import picamera
from PIL import Image
from PIL import ImageStat
from p_iris_ctrl import p_iris_ctrl
home = str(Path.home())
iso_options = [100, 200, 320, 400, 500, 640, 800]
speed_options = [500, 1000, 2000, 4000, 8000, 16667, 33333]
# Starting values of iso and speed:
iso = 1
speed = 3
iris = 0
# Exposure control constants:
TARGET_XP = 120
TOLERANCE = 5
GAIN = 0.1
camera = picamera.PiCamera()
def aperture(value):
p_iris_ctrl(target_aperture=int(value))
def write_to_file(filename, content):
while True:
try:
f = open(filename, "w")
f.write(content)
f.close()
except:
continue
break
def brightness(preview_image):
try:
im = preview_image.convert('L')
stat = ImageStat.Stat(im)
return stat.mean[0]
except:
print("Error calculating brightness from image")
exit()
def saturate(value, minimum, maximum):
if value > maximum:
return maximum
if value < minimum:
return minimum
return value
def capture_preview():
global camera, iso, iso_options, speed, speed_options
camera.start_preview()
stream = io.BytesIO()
camera.iso = iso_options[iso]
camera.shutter_speed = speed_options[speed]
camera.zoom = (0, 0, 1, 1) # (x, y, w, h)
camera.capture(stream, format='jpeg', resize=(320, 240))
stream.seek(0)
image = Image.open(stream)
return image
try:
if len(sys.argv) - 1 > 1:
print("Too many arguments!")
exit()
if len(sys.argv) - 1 == 1:
TARGET_XP = sys.argv[1]
if not str(TARGET_XP).isdecimal():
print("Target must be a number between 0 and 255")
exit()
TARGET_XP = int(TARGET_XP)
TARGET_XP = saturate(TARGET_XP, 0, 255)
camera.start_preview()
ready = False
while not ready:
print("Testing iso " + str(iso_options[iso]) + ", speed " + str(speed_options[speed]) + ", aperture " + str(iris))
aperture(iris)
exposure_error = TARGET_XP - brightness(capture_preview())
if exposure_error > TOLERANCE:
if iris > 0:
iris = saturate(iris - math.ceil(exposure_error * GAIN), 0, 100)
elif speed < len(speed_options) - 1:
speed += 1
elif iso < len(iso_options) - 1:
iso += 1
else:
print("Environment too dark! Unable to expose image to target of " + str(TARGET_XP))
exit()
elif exposure_error < -TOLERANCE:
if iris < 100:
iris = saturate(iris + math.ceil(-exposure_error * GAIN), 0, 100)
elif speed > 0:
speed -= 1
elif iso > 0:
iso -= 1
else:
print("Environment too bright! unable to expose image to target of " + str(TARGET_XP))
else:
ready = True
print("Settings successful! Writing to camera_settings.txt")
write_to_file(home + "/aa-cam/camera_settings.txt", str(iso_options[iso]) + "\n" + str(speed_options[speed]) + "\n")
finally:
camera.stop_preview()
camera.close()