From da6212616cf62f07441bb21fe8bf02daae8a4f80 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Tue, 17 Nov 2020 16:31:32 +0000 Subject: [PATCH] Cleaned up code layout --- openflexure_microscope/camera/base.py | 18 +++++++++--------- openflexure_microscope/camera/pi.py | 27 ++++++++++----------------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/openflexure_microscope/camera/base.py b/openflexure_microscope/camera/base.py index c26d9f48..8d63c704 100644 --- a/openflexure_microscope/camera/base.py +++ b/openflexure_microscope/camera/base.py @@ -13,14 +13,14 @@ TrackerFrame = namedtuple("TrackerFrame", ["size", "time"]) class FrameStream(io.BytesIO): """ - A file-like object used to analyse MJPEG frames. + A file-like object used to analyse and stream MJPEG frames. Instead of analysing a load of real MJPEG frames after they've been stored in a BytesIO stream, we tell the camera to write frames to this class instead. - We then do analysis as the frames are written, and discard the heavier - image data. + We then do analysis as the frames are written, and discard + old frames as each new frame is written. """ def __init__(self, *args, **kwargs): @@ -39,16 +39,19 @@ class FrameStream(io.BytesIO): self.new_frame = ClientEvent() def start_tracking(self): + """Start tracking frame sizes""" if not self.tracking: logging.debug("Started tracking frame data") self.tracking = True def stop_tracking(self): + """Stop tracking frame sizes""" if self.tracking: logging.debug("Stopped tracking frame data") self.tracking = False def reset_tracking(self): + """Empty the array of tracked frame sizes""" self.frames = [] def write(self, s): @@ -72,9 +75,7 @@ class FrameStream(io.BytesIO): self.new_frame.set() def getvalue(self) -> bytes: - """ - Clear tne new_frame event and return frame data - """ + """Clear tne new_frame event and return frame data""" self.new_frame.clear() return super().getvalue() @@ -90,10 +91,9 @@ class BaseCamera(metaclass=ABCMeta): """ def __init__(self): - self.camera = None - + #: :py:class:`labthings.StrictLock`: Access lock for the camera self.lock = StrictLock(name="Camera", timeout=None) - + #: :py:class:`FrameStream`: Streaming and analysis frame buffer self.stream = FrameStream() self.stream_active = False diff --git a/openflexure_microscope/camera/pi.py b/openflexure_microscope/camera/pi.py index 2c09a570..523267d2 100644 --- a/openflexure_microscope/camera/pi.py +++ b/openflexure_microscope/camera/pi.py @@ -39,7 +39,6 @@ import picamerax import picamerax.array from openflexure_microscope.camera.base import BaseCamera -from openflexure_microscope.paths import settings_file_path from openflexure_microscope.utilities import json_to_ndarray, ndarray_to_json # Richard's fix gain @@ -76,10 +75,9 @@ class PiCameraStreamer(BaseCamera): def __init__(self): # Run BaseCamera init BaseCamera.__init__(self) - # Attach to Pi camera - self.camera = ( - picamerax.PiCamera() - ) #: :py:class:`picamerax.PiCamera`: Picamera object + + #: :py:class:`picamerax.PiCamera`: Attached Picamera object + self.camera = picamerax.PiCamera() # Store state of PiCameraStreamer self.preview_active = False @@ -87,18 +85,13 @@ class PiCameraStreamer(BaseCamera): # Reset variable states self.set_zoom(1.0) - # Set default settings - self.image_resolution = tuple( - self.camera.MAX_RESOLUTION - ) #: tuple: Resolution for image captures - self.stream_resolution = ( - 832, - 624, - ) #: tuple: Resolution for stream and video captures - self.numpy_resolution = ( - 1312, - 976, - ) #: tuple: Resolution for numpy array captures + #: tuple: Resolution for image captures + self.image_resolution = tuple(self.camera.MAX_RESOLUTION) + #: tuple: Resolution for stream and video captures + self.stream_resolution = (832, 624) + #: tuple: Resolution for numpy array captures + self.numpy_resolution = (1312, 976) + self.jpeg_quality = 100 #: int: JPEG quality self.mjpeg_quality = 75 #: int: MJPEG quality