Load tuning files in without using Picamera2

This commit is contained in:
Julian Stirling 2025-10-30 13:15:50 +00:00
parent a2900b0c16
commit cec6f4af73

View file

@ -6,34 +6,34 @@ the future.
from typing import Any
from copy import deepcopy
from picamera2 import Picamera2
import numpy as np
import os
import json
import numpy as np
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class TuningFileError(RuntimeError):
"""Raised if the tuning file cannot be loaded for any reason."""
def load_default_tuning(sensor_model: str) -> dict:
"""Load the default tuning file for the camera.
This will load the tuning file based on the specified sensor model.
"""
fname = f"{sensor_model}.json"
custom_path = os.path.join(THIS_DIR, 'tuning_files', fname)
# Note the vc4 here. This locks us to Pi4. We will need to update this to support
# the Raspberry Pi 5.
tuning_path = os.path.join(THIS_DIR, "tuning_files", "vc4", fname)
# Attempt to load custom tuning file from OpenFlexure settings
if os.path.isfile(custom_path):
return Picamera2.load_tuning_file(fname, dir=os.path.join(THIS_DIR, 'tuning_files'))
try:
return Picamera2.load_tuning_file(fname)
except RuntimeError:
tuning_dir = "/usr/share/libcamera/ipa/raspberrypi"
# from picamera2 v0.3.9
# The directory above has been removed from the search path seems
# odd - as that's where the files currently are on a default
# Raspbian image. This may need updating if the files have moved
# in future updates to the system libcamera package
return Picamera2.load_tuning_file(fname, dir=tuning_dir)
with open(tuning_path, "r") as fp:
return json.load(fp)
except (json.decoder.JSONDecodeError, IOError) as e:
raise TuningFileError(f"Could not load tuning from {tuning_path}.") from e
def find_tuning_algo(tuning: dict[str, dict], name: str) -> dict[str, Any]: