-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrender.py
More file actions
396 lines (381 loc) · 16.4 KB
/
render.py
File metadata and controls
396 lines (381 loc) · 16.4 KB
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
import argparse
import json
import math
import os
import pathlib
import struct
import sys
import typing
import faery
import faery.frame_filter
import numpy
import numpy.typing
import PIL.Image
import pidng.core
import pidng.defs
import transform
def draw_progress_bar(columns: int, image_index: int, image_count: int):
count_width = math.ceil(math.log10(image_count))
prefix = f"{image_index: {count_width}} / {image_count}"
progress_bar_width = columns - len(prefix) - 2
if progress_bar_width > 2:
sys.stdout.write(
f"\r{prefix} {faery.display.generate_progress_bar(width=progress_bar_width, progress=image_index / image_count)}"
)
else:
sys.stdout.write(f"\r{' ' * columns}\r{prefix}")
if image_index == image_count:
sys.stdout.write(f"\r{' ' * columns}\r")
def unpack_file(
path: pathlib.Path,
unpacker: typing.Optional[transform.Unpacker] = None,
demosaicer: typing.Optional[transform.Demosaicer] = None,
generate_png_frames: bool = True,
generate_dng_frames: bool = True,
generate_video: bool = True,
force: bool = False,
):
if unpacker is None:
unpacker = transform.Unpacker()
output_directory = path.parent / f"{path.stem}"
output_directory.mkdir(exist_ok=True)
if generate_png_frames:
png_frames_output = output_directory / f"{path.stem}_png_frames"
png_frames_output.mkdir(exist_ok=True)
else:
png_frames_output = None
if generate_dng_frames:
dng_frames_output = output_directory / f"{path.stem}_dng_frames"
dng_frames_output.mkdir(exist_ok=True)
else:
dng_frames_output = None
timestamps_list = []
# count frames
image_count = 0
file_size = path.stat().st_size
with open(path, "rb") as file:
assert file.read(6) == b"BASLER"
header = file.read(21)
if len(header) < 21:
return
pixel_format_id, width, height, exposure, gain = struct.unpack("<BHHdd", header)
pixel_format = transform.id_to_pixel_format(pixel_format_id)
if pixel_format == "BayerRG12p":
bit_depth = 12
elif pixel_format == "Mono10p":
bit_depth = 10
else:
return Exception(f"unknown pixel format {pixel_format}")
data_length = width * height * bit_depth // 8
while True:
timestamp_data = file.read(8)
if len(timestamp_data) < 8:
break
if file.tell() > file_size - data_length:
break
file.seek(data_length, 1)
image_count += 1
# render "raw" PNGs and raw DNGs
if generate_png_frames or generate_dng_frames:
print(f"{path.stem} – Render frames")
columns = os.get_terminal_size().columns
draw_progress_bar(
columns=columns,
image_index=0,
image_count=image_count,
)
with open(path, "rb") as file:
image_index = 0
assert file.read(6) == b"BASLER"
header = file.read(21)
if len(header) < 21:
return
pixel_format_id, width, height, exposure, gain = struct.unpack(
"<BHHdd", header
)
pixel_format = transform.id_to_pixel_format(pixel_format_id)
if pixel_format == "BayerRG12p":
bit_depth = 12
elif pixel_format == "Mono10p":
bit_depth = 10
else:
return Exception(f"unknown pixel format {pixel_format}")
data_length = width * height * bit_depth // 8
data = bytearray(data_length)
while True:
timestamp_data = file.read(8)
if len(timestamp_data) < 8:
break
timestamps_list.append(struct.unpack("<Q", timestamp_data)[0])
if file.readinto(data) != data_length:
break
unpacked_array: typing.Optional[numpy.typing.NDArray[numpy.uint16]] = (
None
)
if png_frames_output is not None:
png_output_path = (
png_frames_output / f"{path.stem}_{image_index:06d}.png"
)
if force or not png_output_path.is_file():
png_write_output_path = (
png_frames_output
/ f"{path.stem}_{image_index:06d}.png.write"
)
unpacked_array = unpacker.unpack(
width=width,
height=height,
data=data,
pixel_format=pixel_format,
)
PIL.Image.frombytes(
mode="I;16",
size=(width, height),
data=unpacked_array.tobytes(),
).save(
str(png_write_output_path), format="png", compress_level=1
)
png_write_output_path.replace(png_output_path)
if dng_frames_output is not None:
dng_output_path = (
dng_frames_output / f"{path.stem}_{image_index:06d}.dng"
)
if force or not dng_output_path.is_file():
if unpacked_array is None:
unpacked_array = unpacker.unpack(
width=width,
height=height,
data=data,
pixel_format=pixel_format,
)
unpacked_array >>= 16 - bit_depth
tags = pidng.core.DNGTags()
tags.set(pidng.core.Tag.ImageWidth, width) # type: ignore
tags.set(pidng.core.Tag.ImageLength, height) # type: ignore
tags.set(pidng.core.Tag.TileWidth, width) # type: ignore
tags.set(pidng.core.Tag.TileLength, height) # type: ignore
tags.set(
pidng.core.Tag.Orientation, # type: ignore
pidng.defs.Orientation.Horizontal,
)
tags.set(
pidng.core.Tag.PhotometricInterpretation, # type: ignore
pidng.defs.PhotometricInterpretation.Color_Filter_Array,
)
tags.set(pidng.core.Tag.SamplesPerPixel, 1) # type: ignore
tags.set(pidng.core.Tag.BitsPerSample, bit_depth) # type: ignore
tags.set(pidng.core.Tag.CFARepeatPatternDim, [2, 2]) # type: ignore
tags.set(
pidng.core.Tag.CFAPattern, # type: ignore
pidng.defs.CFAPattern.RGGB,
)
tags.set(pidng.core.Tag.WhiteLevel, ((1 << bit_depth) - 1)) # type: ignore
tags.set(
pidng.core.Tag.ColorMatrix1, # type: ignore
[
[32404, 10000],
[-15371, 10000],
[-4985, 10000],
[-9692, 10000],
[18760, 10000],
[415, 10000],
[556, 10000],
[-2040, 10000],
[10572, 10000],
],
)
tags.set(
pidng.core.Tag.CalibrationIlluminant1, # type: ignore
pidng.defs.CalibrationIlluminant.D65,
)
tags.set(pidng.core.Tag.AsShotNeutral, [[1, 1], [1, 1], [1, 1]]) # type: ignore
tags.set(pidng.core.Tag.BaselineExposure, [[-150, 100]]) # type: ignore
tags.set(pidng.core.Tag.Make, "Basler") # type: ignore
tags.set(pidng.core.Tag.Model, "-") # type: ignore
tags.set(
pidng.core.Tag.DNGVersion, # type: ignore
pidng.defs.DNGVersion.V1_4,
)
tags.set(
pidng.core.Tag.DNGBackwardVersion, # type: ignore
pidng.defs.DNGVersion.V1_2,
)
tags.set(
pidng.core.Tag.PreviewColorSpace, # type: ignore
pidng.defs.PreviewColorSpace.sRGB,
)
converter = pidng.core.RAW2DNG()
converter.options(tags, path="", compress=False)
converter.convert(unpacked_array, filename=str(dng_output_path))
image_index += 1
draw_progress_bar(
columns=columns,
image_index=image_index,
image_count=image_count,
)
# create configuration and timestamps_ns files
mean_timestamp_delta = numpy.mean(
numpy.diff(numpy.array(timestamps_list, dtype=numpy.uint64))
)
framerate = float(1e9 / mean_timestamp_delta)
duration = 0.0
if len(timestamps_list) > 1:
duration = timestamps_list[-1] / 1e3 - timestamps_list[0] / 1e3
duration += exposure
with open(
output_directory / f"{path.stem}_configuration.json", "w"
) as configuration_file:
json.dump(
{
"width": width,
"height": height,
"exposure_us": exposure,
"gain": gain,
"frame_rate_hz": framerate,
"frame_count": len(timestamps_list),
"duration": (int(round(duration)) * faery.us).to_timecode(),
},
configuration_file,
indent=4,
)
with open(
output_directory / f"{path.stem}_timestamps_ns.json", "w"
) as timestamps_file:
json.dump(timestamps_list, timestamps_file)
# render mp4 video
if generate_video:
print(f"{path.stem} – Render video")
columns = os.get_terminal_size().columns
output_path = output_directory / f"{path.stem}_render.mp4"
if force or not output_path.is_file():
draw_progress_bar(
columns=columns,
image_index=0,
image_count=image_count,
)
write_output_path = output_directory / f"{path.stem}_render.mp4.write"
image_index = 0
with open(path, "rb") as file:
assert file.read(6) == b"BASLER"
header = file.read(21)
if len(header) < 21:
return
pixel_format_id, width, height, exposure, gain = struct.unpack(
"<BHHdd", header
)
gray_frame: typing.Optional[numpy.ndarray] = None
factor = min(
math.ceil(4800 / width),
math.ceil(3600 / height),
max(1.0, math.ceil(960 / width), math.ceil(720 / height)),
)
dimensions = (
int(round(width * factor)),
int(round(height * factor)),
)
SPEED_UP_PRECISION: float = 1e-3
speed_up = 60.0 / framerate
if (
speed_up < 1.0 + SPEED_UP_PRECISION
and speed_up > 1.0 - SPEED_UP_PRECISION
):
speedup_label = "Real-time"
elif speed_up < 1.0:
inverse_speedup = 1.0 / speed_up
speedup_label = f"× 1/{faery.frame_filter.number_to_string(inverse_speedup, SPEED_UP_PRECISION)}"
else:
speedup_label = f"× {faery.frame_filter.number_to_string(speed_up, SPEED_UP_PRECISION)}"
color = faery.color.color_to_ints("#FFFFFF")
with faery.mp4.Encoder(
path=write_output_path,
dimensions=dimensions,
frame_rate=60.0,
crf=17.0,
preset="medium",
tune="none",
profile="baseline",
) as encoder:
while True:
timestamp_data = file.read(8)
if len(timestamp_data) < 8:
break
timestamp = (
struct.unpack("<Q", timestamp_data)[0] - timestamps_list[0]
)
if file.readinto(data) != data_length:
break
unpacked_array = unpacker.unpack(
width=width,
height=height,
data=data,
pixel_format=pixel_format,
).reshape((height, width))
if pixel_format == "BayerRG12p":
if demosaicer is None:
demosaicer = transform.Demosaicer()
frame = demosaicer.demosaicize(unpacked_array)
elif pixel_format == "Mono10p":
if gray_frame is None:
gray_frame = numpy.zeros(
(height, width, 3), dtype=numpy.uint8
)
numpy.right_shift(
unpacked_array,
8,
out=unpacked_array,
dtype=numpy.uint16,
)
gray_frame[:, :, 0] = unpacked_array
gray_frame[:, :, 1] = gray_frame[:, :, 0]
gray_frame[:, :, 2] = gray_frame[:, :, 0]
frame = gray_frame
else:
return Exception(f"unknown pixel format {pixel_format}")
output_frame = faery.image.resize(
frame,
dimensions,
sampling_filter="nearest",
)
faery.image.annotate(
frame=output_frame,
text=(int(round(timestamp / 1e3)) * faery.us).to_timecode(),
x=21,
y=15,
size=30,
color=color,
)
faery.image.annotate(
frame=output_frame,
text=speedup_label,
x=21,
y=15 + round(30 * 1.2),
size=30,
color=color,
)
encoder.write(output_frame)
image_index += 1
draw_progress_bar(
columns=columns,
image_index=image_index,
image_count=image_count,
)
write_output_path.rename(output_path)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--no-png-frames", action="store_true")
parser.add_argument("--no-dng-frames", action="store_true")
parser.add_argument("--no-video", action="store_true")
parser.add_argument("--force", action="store_true")
args = parser.parse_args()
unpacker = transform.Unpacker()
demosaicer = transform.Demosaicer()
for path in sorted((faery.dirname / "recordings").iterdir()):
if path.is_file() and path.suffix == ".basler":
unpack_file(
path=path,
unpacker=unpacker,
demosaicer=demosaicer,
generate_png_frames=not args.no_png_frames,
generate_dng_frames=not args.no_dng_frames,
generate_video=not args.no_video,
force=args.force,
)