-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpicamera2-test.py
303 lines (248 loc) · 7.68 KB
/
picamera2-test.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
#!/usr/bin/python3
# Example of setting controls. Here, after one second, we fix the AGC/AEC
# to the values it has reached whereafter it will no longer change.
import time
import pickle
import os
from picamera2 import Picamera2, Preview
from _libHQCam2.ramdisk import RAMDisk
from _libHQCam2.archive import ArchiveFolder
from os.path import join, basename, dirname
def AwaitInRange(pcam, targetValue, LoDeviation=0.95, HiDeviation=1.05):
# Get lower boundary
if type(LoDeviation) == float:
lo = targetValue * LoDeviation
elif type(LoDeviation) == int:
lo = targetValue - LoDeviation
if lo < 0:
lo = 0
else:
raise Exception("Unsupported LoDeviation type")
# Get upper boundary
if type(HiDeviation) == float:
hi = targetValue * HiDeviation
elif type(HiDeviation) == int:
hi = targetValue + HiDeviation
else:
raise Exception("Unsupported HiDeviation type")
maxIter = 10
while maxIter > 0:
meta = pcam.capture_metadata()
curSS = meta["ExposureTime"]
if curSS >= lo and curSS <= hi:
break
maxIter -= 1
time.sleep(0.05)
timedout = True if maxIter <= 0 else False
return curSS, timedout
################ Start ######################
ramdiskPath = "/media/ramdisk/Captures"
ramdisk = RAMDisk(mntPnt=dirname(ramdiskPath))
if(not os.path.isdir(ramdiskPath)):
os.system(f"sudo mkdir -p {ramdiskPath}")
os.system(f"sudo chown pi:pi {ramdiskPath}")
# Cam-Tuning can be found under /usr/share/libcamera/ipa/rpi/vc4/
camTune = Picamera2.load_tuning_file("imx477_scientific.json")
# Optional settings!
gamma = Picamera2.find_tuning_algo(camTune, "rpi.contrast")
# gamma["ce_enable"] = 1 # Normally = 0 (deactivated)
gamma["gamma_curve"] = [ # Linear Gamma-Curve
0, 0,
1024, 1024,
2048, 2048,
3072, 3072,
4096, 4096,
5120, 5120,
6144, 6144,
7168, 7168,
8192, 8192,
9216, 9216,
10240, 10240,
11264, 11264,
12288, 12288,
13312, 13312,
14336, 14336,
15360, 15360,
16384, 16384,
18432, 18432,
20480, 20480,
22528, 22528,
24576, 24576,
26624, 26624,
28672, 28672,
30720, 30720,
32768, 32768,
36864, 36864,
40960, 40960,
45056, 45056,
49152, 49152,
53248, 53248,
57344, 57344,
61440, 61440,
65535, 65535,
]
picam2 = Picamera2(tuning=camTune)
picam2.start_preview(Preview.NULL)
preview_config = picam2.create_preview_configuration(raw={"size": picam2.sensor_resolution})
# print(preview_config)
picam2.configure(preview_config)
picam2.start()
time.sleep(1)
metadata = picam2.capture_metadata()
controls = {c: metadata[c] for c in ["ExposureTime", "AnalogueGain", "ColourGains"]}
controls["AnalogueGain"] = 1.0
# controls["DigitalGain"] = 1.0
controls["ColourGains"] = (1.0, 1.0)
# print(controls)
picam2.set_controls(controls)
controlSS = {c: metadata[c] for c in ["ExposureTime"]}
SS = [
100,
200,
300,
400,
500,
600,
700,
800,
900,
1000,
1500,
2000,
2500,
3000,
3500,
4000,
4500,
5000,
5500,
6000,
6500,
7000,
7500,
8000,
8500,
9000,
9500,
10000,
12500,
15000,
17500,
20000,
22500,
25000,
27500,
30000,
32500,
35000,
37500,
40000,
42500,
45000,
47500,
50000,
52500,
55000,
57500,
60000,
62500,
65000,
67500,
70000,
72500,
75000,
77500,
80000,
82500,
85000,
87500,
90000,
92500,
95000,
97500,
100000,
]
# Preset shortest SS
startPresetSS = time.time()
_ss = SS[0]
controlSS["ExposureTime"] = _ss
picam2.set_controls(controlSS)
_ssI, to = AwaitInRange(pcam=picam2, targetValue=_ss, LoDeviation=0.85, HiDeviation=1.15)
durationPresetSS = time.time() - startPresetSS
print(str.format("Preset SS: {:.3f} - {}/{}", durationPresetSS, _ssI, _ss))
fNames = list()
ceEnabled = str(gamma["ce_enable"])
gcCurve = "lin" if gamma["gamma_curve"][2] == gamma["gamma_curve"][3] else "sciStd"
prefix = f"JSON=Sci, CE={ceEnabled}, GC={gcCurve}, PreClipBayer600x600"
for _ss in SS:
fNames.append(join(ramdiskPath, prefix + ", SS=" + str(_ss) + ".raw"))
# Preclip-preparation
w = 600
h = 600
x1 = int((4056-w)/2)
x2 = x1 + w
y1 = int((3040-h)/2)
y2 = y1 + h
cx1 = int(x1 * 1.5) # 1.5 = 12bit / 8bit
cx2 = int(x2 * 1.5) # 1.5 = 12bit / 8bit
cy1 = int(y1) # 1.5 = 12bit / 8bit
cy2 = int(y2) # 1.5 = 12bit / 8bit
nPics = 3
print(f"Taking {len(fNames)} images on {prefix}")
logOut = ""
t0 = time.time()
durationNPics = []
for _iSS in range(len(SS)):
_ss = SS[_iSS]
startSetSS = time.time()
controlSS["ExposureTime"] = _ss
picam2.set_controls(controlSS)
_ssI, to = AwaitInRange(pcam=picam2, targetValue=_ss, LoDeviation=0.95, HiDeviation=1.05)
durationSetSS = time.time() - startSetSS
print(str.format("Setting SS: {:.3f} ({}/{}) - Timeout: {}", durationSetSS, _ssI, _ss, to))
startNPics = time.time()
for _iImg in range(nPics): # Take 3 Images
savName = fNames[_iSS].replace(".raw", str.format(", #={:02d}.raw", _iImg))
# print("Capturing: " + fNames[_iSS])
print("Capturing: " + savName)
startCap = time.time()
# metadata = picam2.capture_metadata()
metadata = picam2.capture_metadata()
raw = picam2.capture_array("raw")
raw = raw[cy1:cy2, cx1:cx2]
durationCap = time.time() - startCap
print(str.format("Capture took: {}s", durationCap))
# print(raw.shape)
startSav = time.time()
f = open(savName, "wb")
# f = open(fNames[_iSS], "wb")
pickle.dump(raw, f)
f.close()
durationSav = time.time() - startSav
durationCapSav = time.time() - startCap
print(str.format("Pickle need: {}s", durationSav))
print(str.format("Capture + Save: {}s", durationCapSav))
_curSS = metadata["ExposureTime"]
_fps = 1 / (metadata["FrameDuration"] * 1e-6)
logOut += str.format("fName:\"{}\";FPS:{:.3f};ssSoll:{};ssIst:{};tCap:{:.3f};tSav:{:.3f};tCapSav:{:.3f};tSetSS:{:.3f}\n", fNames[_iSS], _fps, _ss, _curSS, durationCap, durationSav, durationCapSav, durationSetSS)
durationNPics.append(time.time() - startNPics)
print(str.format("{} Pics@SS={} took {:.3f}s", nPics, _ss, durationNPics[-1]))
durationOverallCapSav = time.time() - t0
print(str.format("Overall Capture & Save took: {}s", durationOverallCapSav))
startCompress = time.time()
ArchiveFolder(ramdiskPath, join(dirname(ramdiskPath), prefix + ".tar.gz"), compress=True, SuppressParents=True, Multicore=True)
durationCompress = time.time() - startCompress
print(str.format("Compressed in: {:.3f}s", durationCompress))
durationAllInAll = time.time() - t0
print(str.format("All in all: {:.3f}s", durationAllInAll))
for _iNPics in range(len(durationNPics)):
_ss = SS[_iNPics]
_tNPics = durationNPics[_iNPics]
logOut += str.format("{} Pics@SS={} took {:.3f}s\n", nPics, _ss, _tNPics)
logOut += str.format("tOverallCapSave:{:.3f};tCompress:{:.3f};tAllInAll:{:.3f}", durationOverallCapSav, durationCompress, durationAllInAll)
print(str.format("Saving log {}".ljust(60), join(dirname(ramdiskPath), prefix + ".log")), end="")
f = open(join(dirname(ramdiskPath), prefix + ".log"), "w")
f.writelines(logOut)
f.close()
print("Ok")
print("Finished")