# spectrometer Raspberry Pi HQ Camera(IMX477)RAW 光谱工具集:录制、回放、寻峰、波长标定。 采集的是相机 **RAW** 数据(`BGGR_PISP_COMP1`,2028×1520,16 bit 容器),不做 ISP、 不去马赛克、不做平滑;所有分析都基于按 CFA 分离后的 R/G/B 三条一维谱。 ## 文件 | 文件 | 作用 | | --- | --- | | `hq_spectrum.py` | 核心库 + 实时播放器:PC1B 解码、CFA 分离、ROI、降维、预览、交互窗口 | | `hq_record.py` | 把相机 RAW 流录成 `recordings/<时间戳>/{frames.bin,frames.jsonl,meta.json}` | | `hq_average.py` | 多帧平均成一条新录制(降噪) | | `spectro_core.py` | **共享核心**:模块加载、标定解析、基线/噪声/寻峰/拟合/双峰分解、CSV/JSON 输出 | | `peak_finder.py` | 寻峰与峰信息导出:峰形图、峰形数据、像素坐标、RAW 像素块;`--pick` 交互选峰 | | `wavelength_calibration.py` | 用汞灯谱线标定 像素→波长,产出 `calibration_exports/` | | `wavelength_player.py` | 带已存标定的回放(波长轴、瀑布图) | | `PEAK_FINDER.md` / `WAVELENGTH_PLAYER.md` | 两个工具的详细说明 | | `test_peak_finder.py` / `test_wavelength_calibration.py` / `test_wavelength_player.py` | 单元测试(含真实录制的端到端用例) | | `calibration_exports/` | 汞灯标定的输出目录(**本地生成,不入库**):`calibration.json`、残差、逐像素波长表 | ## 依赖 ``` pip install -r requirements.txt ``` - Python ≥ 3.10,**NumPy + Matplotlib**,不需要 SciPy。 - 树莓派上采集还需要 `picamera2`(系统包)和 `python3-tk`(交互窗口)。 - 在 PC 上分析已有录制只需要 NumPy + Matplotlib。 ## 快速开始 录制(树莓派): ```bash python3 hq_record.py --output recordings --exposure 66351 --gain 16 --duration 10 ``` 回放 / 看谱(PC 或树莓派): ```bash python3 hq_spectrum.py --input recordings/hq_20261101_114417 --fps 15 python3 wavelength_player.py --input recordings/hq_20261101_114417 \ --calibration calibration_exports/hq_20261101_114417_f0000/calibration.json ``` 寻峰并导出(`--calibration` 只是**应用**已有的标定,不给就只输出像素坐标): ```bash python peak_finder.py --input recordings/hq_20261101_114417 \ --calibration calibration_exports/hq_20261101_114417_f0000/calibration.json --select all python peak_finder.py --input recordings/hq_20261101_114417 --pick ``` 重新标定(用寻峰结果,不重新造一套检测): ```bash # 1. 导出峰 python peak_finder.py --input recordings/hq_20261101_114417 --select all # → peak_exports/hq_20261101_114417_f0000/peaks.json # 2. 看峰表拿到编号,再指定身份 python wavelength_calibration.py --peaks peak_exports/hq_20261101_114417_f0000/peaks.json python wavelength_calibration.py --peaks peak_exports/hq_20261101_114417_f0000/peaks.json ^ --assign "P001:404.6563,P002:435.8328,P003:546.0735,P004:576.9598,P005:579.0663" ``` `--peaks` 也可以指向 `selected_peaks.csv`;`--input <录制>` 则让标定脚本在内部调用 `peak_finder.analyse()`,同样是同一套峰。 ## 测试 ```bash python -m unittest test_peak_finder test_wavelength_calibration test_wavelength_player -v ``` 端到端用例需要录制与标定;录制不在脚本附近时用环境变量指过去,否则自动跳过: ```powershell $env:PEAK_FINDER_TEST_RECORDING = "C:\Users\peng0\Dev\Light\recordings\hq_20261101_114417" $env:PEAK_FINDER_TEST_CALIBRATION = "calibration_exports\hq_20261101_114417_f0000\calibration.json" ``` ## 仓库里不放什么 `recordings/`(原始录制,单个约 470 MB)、`peak_exports/`(导出结果)、 `calibration_exports/`(标定产物)、`__pycache__/` 都在 `.gitignore` 里。 标定结果不随源码走:它绑定某一次实验的光路和传感器模式,`calibration.json` 里还记录了 参考录制的本机路径。需要时用 `wavelength_calibration.py` 重新生成,或用 `--calibration` 指到别处。 ## 许可证 本项目以 **MIT** 许可证发布,全文见 `LICENSE`,版权归 StarCells。 例外:`hq_spectrum.py` 里的 PC1B 解压代码(`decompress_pisp_comp1`、`_block_sum`) 改编自 Picamera2(Raspberry Pi Ltd.),该部分仍为 **BSD-2-Clause**,声明与条款见 `THIRD_PARTY_NOTICES.md`。其余运行依赖(NumPy、Matplotlib、picamera2)的许可证也 一并列在那份文件里。