class BaseCamera(lt.Thing): (source)
Known subclasses: openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCamera
Constructor: BaseCamera()
The base class for all cameras. All cameras must directly inherit from this class.
The connection to the camera hardware should be added to the __enter__ method not __init__ method of the subclass.
| Method | __enter__ |
Open hardware connection when the Thing context manager is opened. |
| Method | __exit__ |
Close hardware connection when the Thing context manager is closed. |
| Method | __init__ |
Initialise the base camera, this creates the background detectors. |
| Method | background |
The data for each background detector, used to save to disk. |
| Method | background |
Set the data for each detector. Only to be used as settings are loaded from disk. |
| Method | capture |
Capture an image and save it to disk. |
| Method | capture |
Acquire one image from the camera and return as an array. |
| Method | capture |
Acquire one image from the camera, downsample, and return as an array. |
| Method | capture |
Capture a PIL image from stream stream_name with timeout wait. |
| Method | capture |
Acquire one image from the camera and return as a JPEG blob. |
| Method | capture |
Capture an image to memory. This can be saved later with save_from_memory. |
| Method | clear |
Clear all images in memory. |
| Method | detector |
The name of the active background selector. |
| Method | detector |
Validate and set detector_name. |
| Method | discard |
Discard frames so that the next frame captured is fresh. |
| Method | grab |
Acquire one image from the preview stream and return as an array. |
| Method | grab |
Acquire one image from the preview stream and return as blob of JPEG data. |
| Method | grab |
Acquire one image from the preview stream and return its size. |
| Method | image |
Label the current image as either background or sample. |
| Method | kill |
Kill the streams now as the server is shutting down. |
| Method | save |
Save an image that has been captured to memory. |
| Method | set |
Grab an image, and use its statistics to set the background. |
| Method | settle |
Sleep for the settling time, ready to provide a fresh frame. |
| Method | start |
Start (or stop and restart) the camera. |
| Method | update |
Update the settings of the current detector. |
| Class Variable | downsampled |
The downsampling factor when calling capture_downsampled_array. |
| Class Variable | lores |
Undocumented |
| Class Variable | mjpeg |
Undocumented |
| Class Variable | settling |
The settling time when calling the settle() method. |
| Instance Variable | background |
Undocumented |
| Property | active |
The active background detector instance. |
| Property | background |
The status of the active detector for the UI. |
| Property | manual |
The camera settings to expose as property controls in the settings panel. |
| Property | primary |
The calibration actions for both calibration wizard and settings panel. |
| Property | secondary |
The calibration actions that appear only in settings panel. |
| Property | stream |
Whether the MJPEG stream is active. |
| Method | _robust |
Capture an image in memory and return it with metadata. |
| Method | _save |
Save the captured image and metadata to disk. |
| Class Variable | _memory |
Undocumented |
| Instance Variable | _detector |
Undocumented |
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraOpen hardware connection when the Thing context manager is opened.
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraClose hardware connection when the Thing context manager is closed.
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraInitialise the base camera, this creates the background detectors.
This must be run by all child camera classes.
To add a new background detector to the server it must be added to the dictionary in this function. Configuration will be added at a later date.
Set the data for each detector. Only to be used as settings are loaded from disk.
Do not call over HTTP. This needs to be updated once LbaThings Settings can be read-only over HTTP (#484).
str, logger: lt.deps.InvocationLogger, metadata_getter: lt.deps.GetThingStates, save_resolution: tuple[ int, int] | None = None):
(source)
¶
Capture an image and save it to disk.
| Parameters | |
jpegstr | The path to save the file to |
logger:lt.deps.InvocationLogger | This should be injected automatically by Labthings FastAPI when calling the action |
metadatalt.deps.GetThingStates | This should be injected automatically by Labthings FastAPI when calling the action |
savetuple[ | can be set to resize the image before saving. By default this is None meaning that the image is saved at original resolution. |
Literal[ 'main', 'lores', 'raw', 'full'] = 'main', wait: float | None = 5) -> ArrayModel:
(source)
¶
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraAcquire one image from the camera and return as an array.
Acquire one image from the camera, downsample, and return as an array.
- The array is downsamples by the thing property
downsampled_array_factor. - The default capture array arguments are used.
This method provides the interface expected by the camera_stage_mapping.
Literal[ 'main', 'lores', 'raw'], wait: float | None) -> Image:
(source)
¶
openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraCapture a PIL image from stream stream_name with timeout wait.
lt.deps.GetThingStates, resolution: Literal[ 'lores', 'main', 'full'] = 'main', wait: float | None = 5) -> JPEGBlob:
(source)
¶
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraAcquire one image from the camera and return as a JPEG blob.
lt.deps.InvocationLogger, metadata_getter: lt.deps.GetThingStates, buffer_max: int = 1) -> int:
(source)
¶
Capture an image to memory. This can be saved later with save_from_memory.
Note that only one image is held in memory so this will overwrite any image in memory.
| Parameters | |
logger:lt.deps.InvocationLogger | This should be injected automatically by Labthings FastAPI when calling the action |
metadatalt.deps.GetThingStates | This should be injected automatically by Labthings FastAPI when calling the action |
bufferint | The maximum number of images that should be in the buffer once this images is added. Default is 1. |
| Returns | |
int | the buffer id of the image captured |
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraDiscard frames so that the next frame captured is fresh.
lt.deps.BlockingPortal, stream_name: Literal[ 'main', 'lores'] = 'main') -> ArrayModel:
(source)
¶
Acquire one image from the preview stream and return as an array.
It works like grab_jpeg but reliably handles broken streams. Prefer using this method over directly grabbing the frame and converting to a numpy array via PIL.
This differs from capture_array in that it does not pause the MJPEG preview stream.
lt.deps.BlockingPortal, stream_name: Literal[ 'main', 'lores'] = 'main') -> JPEGBlob:
(source)
¶
Acquire one image from the preview stream and return as blob of JPEG data.
Note: in rare cases the JPEG stream may be broken. This can cause an OS error when loading the image. If loading with PIL, as long as the header data is complete, this error will not be raised until the data is accessed. Consider using grab_jpeg_as_array instead.
This differs from capture_jpeg in that it does not pause the MJPEG preview stream. Instead, we simply return the next frame from that stream (either "main" for the preview stream, or "lores" for the low resolution preview). No metadata is returned.
lt.deps.BlockingPortal, stream_name: Literal[ 'main', 'lores'] = 'main') -> int:
(source)
¶
Acquire one image from the preview stream and return its size.
lt.deps.BlockingPortal) -> tuple[ bool, str]:
(source)
¶
Label the current image as either background or sample.
Kill the streams now as the server is shutting down.
This is called when uvicorn gets the a shutdown signal. As this is called from the event loop it cannot interact with the our ThingProperties or run self.mjpeg_stream.stop() as the portal cannot be called from this loop.
Instead we just set the _streaming value to False. This stops the async frame generator when the next frame notifies.
str, logger: lt.deps.InvocationLogger, save_resolution: tuple[ int, int] | None = None, buffer_id: int | None = None):
(source)
¶
Save an image that has been captured to memory.
| Parameters | |
jpegstr | The path to save the file to |
logger:lt.deps.InvocationLogger | This should be injected automatically by Labthings FastAPI when calling the action |
savetuple[ | can be set to resize the image before saving. By default this is None meaning that the image is saved at original resolution. |
bufferint | None | The buffer id of the image to save, this was returned by capture_to_memory |
Grab an image, and use its statistics to set the background.
This should be run when the microscope is looking at an empty region, and will calculate the mean and standard deviation of the pixel values in the LUV colourspace. These values will then be used to compare future images to the distribution, to determine if each pixel is foreground or background.
Sleep for the settling time, ready to provide a fresh frame.
This function will sleep for the given time, and clear the buffer after sleeping. As such, the next frame captured from the camera after running this function will always be captured after settling.
This method provides the interface expected by the camera_stage_mapping.
tuple[ int, int], buffer_count: int):
(source)
¶
openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraStart (or stop and restart) the camera.
| Parameters | |
maintuple[ | the resolution to use for the main stream. |
bufferint | number of images in the stream buffer. |
Update the settings of the current detector.
This is an action not a setting/property as the data model depends on the selected detector. As such, it cannot be specified with the necessary precision to be included in a ThingDescription as a setting/property, while retaining enough useful information to communicate to the UI how it is set and read.
The information on how to read the settings is exposed in background_detector_status.
openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraThe camera settings to expose as property controls in the settings panel.
openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraThe calibration actions that appear only in settings panel.
openflexure_microscope_server.things.camera.opencv.OpenCVCamera, openflexure_microscope_server.things.camera.picamera.StreamingPiCamera2, openflexure_microscope_server.things.camera.simulation.SimulatedCameraWhether the MJPEG stream is active.
lt.deps.GetThingStates, logger: lt.deps.InvocationLogger) -> Image:
(source)
¶
Capture an image in memory and return it with metadata.
This robust capturing method attempts to capture the image five times each time with a 5 second timeout set.
| Returns | |
Image | tuple with PIL Image, and dictionary of metadata. |
| Raises | |
CaptureError | if the capture fails for any reason |
str, image: Image, metadata: dict, logger: lt.deps.InvocationLogger, save_resolution: tuple[ int, int] | None = None):
(source)
¶
Save the captured image and metadata to disk.
A warning (via InvocationLogger) is raised if metadata is failed to be added
nothing is returned on success
| Raises | |
IOError | if the file cannot be saved |