Files
starcells 45249b59f9 光谱工具集:采集、回放、寻峰、波长标定
- spectro_core.py: 共享核心(加载/标定/基线/寻峰/高斯拟合/双峰联合拟合/CSV-JSON)

- peak_finder.py: 寻峰与峰信息导出,--pick 交互选峰(含 RAW 预览)

- wavelength_calibration.py: 改用 core 寻峰,汞灯 577/579 双峰可分

- wavelength_player.py / hq_average.py: 复用 core 的标定与解码加载
2026-11-14 13:48:24 +08:00

177 lines
8.7 KiB
Python

#!/usr/bin/env python3
"""Average hq_record recording directories using the existing player's decoder."""
from __future__ import annotations
import argparse
import json
import os
from pathlib import Path
import sys
import time
import uuid
import numpy as np
try: # shared core lives beside this script
import spectro_core as core
except ImportError as exc: # pragma: no cover - placement error
raise SystemExit('hq_average.py needs spectro_core.py in the same directory '
'(or on sys.path)') from exc
def load_player(source, module_dir=None):
"""Decoder module: --module-dir, then this folder, then the project above."""
for directory in ([Path(module_dir)] if module_dir
else [Path(__file__).resolve().parent, Path(source).parent.parent]):
if (directory / 'hq_spectrum.py').is_file():
return core.load_hq(directory)
return core.load_hq()
def write_json(path, value):
core.write_json(path, value)
def average(source, output, group=0, drop_last=False, module_dir=None):
source, output = Path(source).resolve(), Path(output).resolve()
if group < 0:
raise ValueError('group must be >= 0')
if source == output or source in output.parents or output in source.parents:
raise ValueError('Input and output must be separate, non-nested directories')
if output.exists():
raise ValueError(f'Output already exists: {output}')
meta = json.loads((source / 'meta.json').read_text(encoding='utf-8'))
if meta.get('kind') != 'hq_raw_recording' or meta.get('format_version') != 1:
raise ValueError('Expected hq_raw_recording format_version 1')
w, h, stride = (int(meta[k]) for k in ('width', 'height', 'stride'))
fb = int(meta['frame_bytes'])
if min(w, h, stride) <= 0 or fb != h * stride:
raise ValueError('Invalid dimensions/stride/frame_bytes')
size = (source / 'frames.bin').stat().st_size
count, remainder = divmod(size, fb)
if remainder or not count or int(meta.get('frames', count)) != count:
raise ValueError('Truncated recording or frame count mismatch; repair source separately')
if not meta.get('completed'):
raise ValueError('Source recording is not marked completed')
records = []
metadata_path = source / 'frames.jsonl'
if metadata_path.exists():
for i, line in enumerate(metadata_path.read_text(encoding='utf-8').splitlines()):
row = json.loads(line)
if not isinstance(row, dict):
raise ValueError(f'Invalid metadata row {i}')
nested = row.pop('metadata', None)
if isinstance(nested, dict):
row = {**nested, **row}
for key, expected in [('i', i), ('offset', i * fb), ('bytes', fb)]:
if key in row and row[key] != expected:
raise ValueError(f'Incorrect metadata {key} in row {i}')
records.append(row)
if len(records) != count:
raise ValueError('frames.jsonl count mismatch')
else:
records = [{} for _ in range(count)]
# Exposure/gain changes are retained as ranges in each averaged frame's provenance.
player = load_player(source, module_dir)
pattern = str(meta.get('pattern') or player.pattern_from_format(meta['raw_format']))
if pattern not in ('BGGR', 'RGGB', 'GBRG', 'GRBG'):
raise ValueError('Unsupported Bayer pattern')
config = {'size': (w, h), 'stride': stride}
n = group or count
groups = [(a, min(a + n, count)) for a in range(0, count, n)
if not (drop_last and min(a + n, count) - a < n)]
if not groups:
raise ValueError('No output frames; reduce group size or omit --drop-last')
output.parent.mkdir(parents=True, exist_ok=True)
temp = output.parent / (output.name + '.partial-' + uuid.uuid4().hex[:8])
temp.mkdir()
started = time.monotonic()
try:
depth = None
out_fb = w * h * 2
with (source / 'frames.bin').open('rb') as inp, (temp / 'frames.bin').open('wb') as raw_out, (temp / 'frames.jsonl').open('w', encoding='utf-8') as mout:
for oi, (a, b) in enumerate(groups):
acc = np.zeros((h, w), dtype=np.float64)
inp.seek(a * fb)
for index in range(a, b):
data = inp.read(fb)
if len(data) != fb:
raise ValueError('Input changed or was truncated while reading')
image, bits = player.decode_raw(np.frombuffer(data, dtype=np.uint8).reshape(h, stride), config, meta['raw_format'])
if depth is not None and depth != bits:
raise ValueError('Decoded bit depth changed')
depth = bits
acc += image
image = np.clip(np.rint(acc / (b - a)), 0, (1 << depth) - 1).astype('<u2')
raw_out.write(image.tobytes())
row = dict(records[a])
for key in ('_frame_index', '_frame_total'):
row.pop(key, None)
ranges = {}
for key in ('ExposureTime', 'AnalogueGain', 'DigitalGain'):
values = [r[key] for r in records[a:b] if isinstance(r.get(key), (float, int))]
if values:
ranges[key] = [min(values), max(values)]
row.update(i=oi, offset=oi * out_fb, bytes=out_fb,
averaging={'start_index': a, 'end_index_exclusive': b, 'count': b-a,
'metadata_policy': 'representative first source frame',
'last_source_metadata': records[b-1], 'parameter_ranges': ranges})
mout.write(json.dumps(row, ensure_ascii=False) + '\n')
print(f'Output {oi+1}/{len(groups)}: source [{a}, {b})', flush=True)
newmeta = dict(meta)
fps = float(meta.get('average_capture_fps') or meta.get('fps_requested') or 15) / n
newmeta.update(raw_format=f'S{pattern}{depth}', bit_depth=depth, stride=w*2,
frame_bytes=out_fb, storage_dtype='uint8', frames=len(groups),
completed=True, frames_file='frames.bin', metadata_file='frames.jsonl',
average_capture_fps=fps, fps_requested=fps,
duration_s=len(groups)/fps, frame_limit=len(groups), duration_limit_s=0,
averaging={'source_directory': str(source), 'source_meta': meta,
'mode': 'all' if not group else 'group', 'group_size': n,
'drop_last': drop_last, 'discarded_frames': count-groups[-1][1],
'rounding': 'nearest, ties to even', 'accumulator': 'float64',
'timing': 'nominal group cadence; last partial group may be shorter'})
write_json(temp / 'meta.json', newmeta)
# Keep every original per-frame field, including within-group variations.
if metadata_path.exists():
import shutil
shutil.copyfile(metadata_path, temp / 'source_frames.jsonl')
os.rename(temp, output)
print(f'Saved: {output} ({time.monotonic()-started:.1f}s)')
return output
except BaseException:
print(f'Incomplete output kept for inspection: {temp}', file=sys.stderr)
raise
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--input', type=Path, help='Recording directory; omit for folder picker')
p.add_argument('--output', type=Path, help='NEW output directory')
m = p.add_mutually_exclusive_group()
m.add_argument('--all', action='store_true', help='Average entire recording (default)')
m.add_argument('--group', type=int, default=0, help='Average each N consecutive frames')
p.add_argument('--drop-last', action='store_true', help='Discard final incomplete group')
p.add_argument('--module-dir', type=Path, help='Directory containing original hq_spectrum.py')
args = p.parse_args()
if args.input is None:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
selected = filedialog.askdirectory(title='Select recording folder')
root.destroy()
if not selected:
return 0
args.input = Path(selected)
suffix = '_avg_all' if not args.group else f'_avg{args.group}'
output = args.output or args.input.resolve().with_name(args.input.name + suffix)
try:
average(args.input, output, args.group, args.drop_last, args.module_dir)
return 0
except (Exception, KeyboardInterrupt) as exc:
print(f'Error: {exc}', file=sys.stderr)
return 1
if __name__ == '__main__':
raise SystemExit(main())