Consolidate the BaseCamera, CameraStub, and CameraProtocol, move StreamingPiCamera2 to this repo

This commit is contained in:
Julian Stirling 2025-05-21 20:31:51 +01:00
parent 628fd145f3
commit b5606984ae
11 changed files with 1617 additions and 143 deletions

View file

@ -0,0 +1,32 @@
from labthings_picamera2 import StreamingPiCamera2
from labthings_fastapi.server import ThingServer
from labthings_fastapi.client import ThingClient
from fastapi.testclient import TestClient
from PIL import Image
import numpy as np
from pytest import fixture
@fixture(scope="module")
def client():
server = ThingServer()
server.add_thing(StreamingPiCamera2(), "/camera/")
with TestClient(server.app) as test_client:
client = ThingClient.from_url("/camera/", client=test_client)
yield client
def test_calibration(client):
client.full_auto_calibrate()
def test_jpeg_and_array(client):
blob = client.grab_jpeg()
mjpeg_frame = Image.open(blob.open())
assert mjpeg_frame
blob = client.capture_jpeg(resolution="main")
jpeg_capture = Image.open(blob.open())
assert jpeg_capture
arrlist = client.capture_array(stream_name="main")
array_main = np.array(arrlist)
assert mjpeg_frame.size == jpeg_capture.size
assert array_main.shape[1::-1] == jpeg_capture.size

View file

@ -0,0 +1,32 @@
import logging
import time
from fastapi.testclient import TestClient
from labthings_fastapi.server import ThingServer
from labthings_fastapi.client import ThingClient
from labthings_picamera2.thing import StreamingPiCamera2
logging.basicConfig(level=logging.DEBUG)
def test_exposure_time_drift():
cam = StreamingPiCamera2()
server = ThingServer()
server.add_thing(cam, "/camera/")
with TestClient(server.app) as test_client:
client = ThingClient.from_url("/camera/", client=test_client)
client.exposure_time = 50000
time.sleep(0.1)
initial_et = client.exposure_time
print(f"Before capture, et is {client.exposure_time}")
for i in range(10):
client.capture_jpeg(resolution="full")
print(f"After capture, et is {client.exposure_time}")
final_et = client.exposure_time
assert initial_et == final_et
if __name__ == "__main__":
test_exposure_time_drift()

View file

@ -0,0 +1,27 @@
import logging
from fastapi.testclient import TestClient
import numpy as np
from labthings_fastapi.server import ThingServer
from labthings_fastapi.client import ThingClient
from labthings_picamera2.thing import StreamingPiCamera2
logging.basicConfig(level=logging.DEBUG)
def test_sensor_mode():
cam = StreamingPiCamera2()
server = ThingServer()
server.add_thing(cam, "/camera/")
with TestClient(server.app) as test_client:
client = ThingClient.from_url("/camera/", client=test_client)
for size in [(3280, 2464), (1640, 1232)]:
client.sensor_mode = {"output_size": size, "bit_depth": 10}
arr = np.array(client.capture_array(stream_name="raw"))
assert arr.shape[0] == size[1]
if __name__ == "__main__":
test_sensor_mode()

View file

@ -0,0 +1,81 @@
import os
from picamera2 import Picamera2
from labthings_picamera2 import recalibrate_utils
import pytest
MODEL = Picamera2.global_camera_info()[0]['Model']
def check_camera_available():
assert len(Picamera2.global_camera_info()) >= 1
def load_default_tuning():
fname = f"{MODEL}.json"
return Picamera2.load_tuning_file(fname)
def generate_bad_tuning():
default_tuning = load_default_tuning()
bad_tuning = default_tuning.copy()
bad_tuning["version"] = 999
return bad_tuning
def print_tuning(read_file=False):
key = "LIBCAMERA_RPI_TUNING_FILE"
if key in os.environ:
print(f"Tuning file environment variable: {os.environ[key]}")
if read_file:
with open(os.environ[key], "r") as f:
print(f.read())
else:
print("Tuning file environment variable not set")
def _test_bad_tuning_after_good_tuning(configure):
bad_tuning = generate_bad_tuning()
default_tuning = load_default_tuning()
print_tuning()
print("opening camera with explicitly specified tuning")
with Picamera2(tuning=default_tuning) as cam:
print_tuning()
if configure:
cam.configure(cam.create_preview_configuration())
del cam
recalibrate_utils.recreate_camera_manager()
print(f"Opening camera with tuning['version'] = {bad_tuning['version']}")
with pytest.raises(IndexError):
# The bad version should cause a problem
cam = Picamera2(tuning=bad_tuning)
print_tuning()
print("Success (not expected)!")
del cam
recalibrate_utils.recreate_camera_manager()
with Picamera2(tuning=default_tuning) as cam:
# Reload the camera with working tuning, or it will stop responding
# and fail future tests
pass
del cam
@pytest.mark.filterwarnings("ignore: Exception ignored")
def test_bad_tuning_after_good_tuning_noconfigure():
_test_bad_tuning_after_good_tuning(False)
@pytest.mark.filterwarnings("ignore: Exception ignored")
def test_bad_tuning_after_good_tuning_configure():
_test_bad_tuning_after_good_tuning(True)
@pytest.mark.filterwarnings("ignore: Exception ignored")
def test_bad_tuning_after_good_tuning_noconfigure2():
_test_bad_tuning_after_good_tuning(False)
@pytest.mark.filterwarnings("ignore: Exception ignored")
def test_bad_tuning_after_good_tuning_configure2():
_test_bad_tuning_after_good_tuning(True)