Merge branch '1.0.1-dev-cleanup' into 'master'

1.0.1 dev cleanup

See merge request openflexure/openflexure-microscope-server!20
This commit is contained in:
Joel Collins 2019-05-23 13:26:30 +00:00
commit fd4de9540a
2 changed files with 62 additions and 58 deletions

View file

@ -240,31 +240,30 @@ class StreamingCamera(BaseCamera):
def start_preview(self, fullscreen=True, window=None) -> bool: def start_preview(self, fullscreen=True, window=None) -> bool:
"""Start the on board GPU camera preview.""" """Start the on board GPU camera preview."""
logging.info("Starting the GPU preview") logging.info("Starting the GPU preview")
self.start_stream_recording()
# TODO: Commented out as I honestly can't remember why this was here. May need to put back?
#self.start_stream_recording()
try: try:
with self.lock: if not self.camera.preview:
if not self.camera.preview: logging.debug("Starting preview")
logging.debug("Starting preview") self.camera.start_preview(fullscreen=fullscreen, window=window)
self.camera.start_preview(fullscreen=fullscreen, window=window) else:
else: logging.debug("Resizing preview")
logging.debug("Resizing preview") if window:
if window: self.camera.preview.window = window
self.camera.preview.window = window if fullscreen:
if fullscreen: self.camera.preview.fullscreen = fullscreen
self.camera.preview.fullscreen = fullscreen self.state['preview_active'] = True
self.state['preview_active'] = True
except picamera.exc.PiCameraMMALError as e: except picamera.exc.PiCameraMMALError as e:
logging.error("Suppressed a MMALError in start_preview. Exception: {}".format(e)) logging.error("Suppressed a MMALError in start_preview. Exception: {}".format(e))
except picamera.exc.PiCameraValueError as e: except picamera.exc.PiCameraValueError as e:
logging.error("Suppressed a ValueError exception in start_preview. Exception: {}".format(e)) logging.error("Suppressed a ValueError exception in start_preview. Exception: {}".format(e))
return True
def stop_preview(self) -> bool: def stop_preview(self) -> bool:
"""Stop the on board GPU camera preview.""" """Stop the on board GPU camera preview."""
with self.lock: self.camera.stop_preview()
self.camera.stop_preview() self.state['preview_active'] = False
self.state['preview_active'] = False
return True
def start_recording( def start_recording(
self, self,
@ -340,21 +339,22 @@ class StreamingCamera(BaseCamera):
splitter_port (int): Splitter port to stop recording on splitter_port (int): Splitter port to stop recording on
resolution ((int, int)): Resolution to set the camera to, after stopping recording. resolution ((int, int)): Resolution to set the camera to, after stopping recording.
""" """
# If no resolution is specified, default to image_resolution with self.lock:
if not resolution: # If no resolution is specified, default to image_resolution
resolution = self.config['image_resolution'] if not resolution:
resolution = self.config['image_resolution']
# Stop the camera video recording on port 1 # Stop the camera video recording on port 1
try: try:
self.camera.stop_recording(splitter_port=splitter_port) self.camera.stop_recording(splitter_port=splitter_port)
except picamera.exc.PiCameraNotRecording: except picamera.exc.PiCameraNotRecording:
logging.info("Not recording on splitter_port {}".format(splitter_port)) logging.info("Not recording on splitter_port {}".format(splitter_port))
else: else:
logging.info("Stopped MJPEG stream on port {1}. Switching to {0}.".format(resolution, splitter_port)) logging.info("Stopped MJPEG stream on port {1}. Switching to {0}.".format(resolution, splitter_port))
# Increase the resolution for taking an image # Increase the resolution for taking an image
time.sleep(0.2) # Sprinkled a sleep to prevent camera getting confused by rapid commands time.sleep(0.2) # Sprinkled a sleep to prevent camera getting confused by rapid commands
self.camera.resolution = resolution self.camera.resolution = resolution
def start_stream_recording( def start_stream_recording(
self, self,
@ -367,38 +367,39 @@ class StreamingCamera(BaseCamera):
splitter_port (int): Splitter port to start recording on splitter_port (int): Splitter port to start recording on
resolution ((int, int)): Resolution to set the camera to, before starting recording. Defaults to `self.config['stream_resolution']`. resolution ((int, int)): Resolution to set the camera to, before starting recording. Defaults to `self.config['stream_resolution']`.
""" """
# If stream object was destroyed with self.lock:
if not hasattr(self, 'stream'): # If stream object was destroyed
self.stream = io.BytesIO() # Create a stream object if not hasattr(self, 'stream'):
self.stream = io.BytesIO() # Create a stream object
# If no explicit resolution is passed # If no explicit resolution is passed
if not resolution: if not resolution:
resolution = self.config['stream_resolution'] # Default to video recording resolution resolution = self.config['stream_resolution'] # Default to video recording resolution
# Reduce the resolution for video streaming # Reduce the resolution for video streaming
try:
self.camera._check_recording_stopped()
except picamera.exc.PiCameraRuntimeError:
logging.info("Error while changing resolution: Recording already running.")
else:
self.camera.resolution = resolution
# If the stream should be active
if self.state['stream_active']:
try: try:
# Start recording on stream port self.camera._check_recording_stopped()
self.camera.start_recording( except picamera.exc.PiCameraRuntimeError:
self.stream, logging.info("Error while changing resolution: Recording already running.")
format='mjpeg',
quality=self.config['jpeg_quality'],
bitrate=-1, # RWB: disable bitrate control
# (bitrate control makes JPEG size less good as a focus
# metric)
splitter_port=splitter_port)
except picamera.exc.PiCameraAlreadyRecording:
logging.info("Error while starting preview: Recording already running.")
else: else:
logging.debug("Started MJPEG stream at {} on port {}".format(resolution, splitter_port)) self.camera.resolution = resolution
# If the stream should be active
if self.state['stream_active']:
try:
# Start recording on stream port
self.camera.start_recording(
self.stream,
format='mjpeg',
quality=self.config['jpeg_quality'],
bitrate=-1, # RWB: disable bitrate control
# (bitrate control makes JPEG size less good as a focus
# metric)
splitter_port=splitter_port)
except picamera.exc.PiCameraAlreadyRecording:
logging.info("Error while starting preview: Recording already running.")
else:
logging.debug("Started MJPEG stream at {} on port {}".format(resolution, splitter_port))
def capture( def capture(
self, self,

View file

@ -11,6 +11,9 @@ class StrictLock(object):
self._lock = RLock() self._lock = RLock()
self.timeout = timeout self.timeout = timeout
def locked(self):
return self._lock.locked()
def acquire(self, blocking=True): def acquire(self, blocking=True):
return self._lock.acquire(blocking, timeout=self.timeout) return self._lock.acquire(blocking, timeout=self.timeout)