Allow log of multiple captures by StreamingCamera
This commit is contained in:
parent
8031327a5b
commit
d99602bd3d
3 changed files with 261 additions and 145 deletions
|
|
@ -4,6 +4,7 @@ import io
|
||||||
import threading
|
import threading
|
||||||
import copy
|
import copy
|
||||||
import datetime
|
import datetime
|
||||||
|
import uuid
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -26,43 +27,60 @@ def log(s):
|
||||||
class StreamObject(object):
|
class StreamObject(object):
|
||||||
def __init__(self, write_to_file: bool=None, filename: str=None, folder: str=None, fmt: str='file') -> None:
|
def __init__(self, write_to_file: bool=None, filename: str=None, folder: str=None, fmt: str='file') -> None:
|
||||||
# Store a nice ID
|
# Store a nice ID
|
||||||
self.id = filename
|
self.id = uuid.uuid4().hex
|
||||||
log("Created {}".format(self.id))
|
log("Created {}".format(self.id))
|
||||||
|
|
||||||
# Create file name
|
# Create file name
|
||||||
self.file = self.build_file_path(filename, folder, fmt)
|
iterator = 0
|
||||||
|
f_name = self.build_file_path(filename, folder, fmt)
|
||||||
|
|
||||||
|
while os.path.isfile(f_name): # While file already exists
|
||||||
|
iterator += 1 # Add a file name iterator
|
||||||
|
f_name = self.build_file_path(filename, folder, fmt, iterator=iterator) # Rebuild file name
|
||||||
|
|
||||||
|
self.file = f_name
|
||||||
|
|
||||||
# Byte stream properties
|
# Byte stream properties
|
||||||
self.stream = io.BytesIO() # Byte stream that data will be written to
|
self.stream = io.BytesIO() # Byte stream that data will be written to
|
||||||
|
|
||||||
# Set default write target
|
# Set default write target
|
||||||
self.write_to_file = write_to_file
|
self.write_to_file = write_to_file
|
||||||
if self.write_to_file == False:
|
if self.write_to_file is False:
|
||||||
log("Default target for {} set to 'stream'".format(self.id))
|
log("Default target for {} set to 'stream'".format(self.id))
|
||||||
self.target = self.stream
|
self.target = self.stream
|
||||||
else:
|
else:
|
||||||
log("Default target for {} set to 'file'".format(self.id))
|
log("Default target for {} set to 'file'".format(self.id))
|
||||||
self.target = self.file
|
self.target = self.file
|
||||||
|
|
||||||
|
# Keep on disk after close by default
|
||||||
|
self.keep_on_disk = True
|
||||||
|
|
||||||
# Object lock
|
# Object lock
|
||||||
self.locked = False # Is the StreamObject locked for writing? (Handled by StreamingCamera)
|
self.locked = False # Is the StreamObject locked for writing? (Handled by StreamingCamera)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
log("Entering context for {}. Stored files will be cleaned up automatically.".format(self.id))
|
log("Entering context for {}.\
|
||||||
|
Stored files will be cleaned up automatically.".format(self.id))
|
||||||
|
self.keep_on_disk = False # Flag file to be removed on close.
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *args):
|
def __exit__(self, *args):
|
||||||
log("Cleaning up {}".format(self.id))
|
log("Cleaning up {}".format(self.id))
|
||||||
self.delete()
|
self.close()
|
||||||
|
|
||||||
def build_file_path(self, filename: str, folder: str, fmt: str) -> str:
|
def build_file_path(self, filename: str, folder: str, fmt: str, iterator: int=0) -> str:
|
||||||
"""
|
"""
|
||||||
Construct a full file path, based on filename, folder, and file format. Defaults to datestamp
|
Construct a full file path, based on filename, folder, and file format.
|
||||||
|
|
||||||
|
Defaults to datestamp. Iterator adds a numeric increment to the file name.
|
||||||
"""
|
"""
|
||||||
if filename:
|
if filename:
|
||||||
file_name = "{}.{}".format(filename, fmt)
|
file_name = "{}.{}".format(filename, fmt)
|
||||||
else:
|
else:
|
||||||
file_name = "{}.{}".format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), fmt)
|
file_name_base = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
|
if iterator:
|
||||||
|
file_name_base = "{}_{}".format(file_name_base, iterator)
|
||||||
|
file_name = "{}.{}".format(file_name_base, fmt)
|
||||||
|
|
||||||
# Create folder and file
|
# Create folder and file
|
||||||
if folder:
|
if folder:
|
||||||
|
|
@ -76,48 +94,115 @@ class StreamObject(object):
|
||||||
return file_path
|
return file_path
|
||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
"""Set locked flag to True"""
|
"""Set locked flag to True."""
|
||||||
self.locked = True
|
self.locked = True
|
||||||
|
|
||||||
def unlock(self):
|
def unlock(self):
|
||||||
"""Set locked flag to False"""
|
"""Set locked flag to False."""
|
||||||
self.locked = False
|
self.locked = False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stream_exists(self, auto_rewind=True) -> bool:
|
||||||
|
"""Check if BytesIO stream is empty"""
|
||||||
|
if auto_rewind:
|
||||||
|
self.stream.seek(0) # Rewind the data bytes for reading
|
||||||
|
if self.stream.getvalue(): # If data stream contains data
|
||||||
|
self.stream.seek(0) # Rewind the data bytes for reading
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self.stream.seek(0) # Rewind the data bytes for reading
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def file_exists(self) -> bool:
|
||||||
|
"""Check if corresponding file exists"""
|
||||||
|
if os.path.isfile(self.file):
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def metadata(self) -> dict:
|
||||||
|
d = {
|
||||||
|
'id': self.id,
|
||||||
|
'locked': self.locked,
|
||||||
|
'keep_on_disk': self.keep_on_disk,
|
||||||
|
'path': self.file,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get file path
|
||||||
|
if self.file_exists:
|
||||||
|
d['file'] = self.file
|
||||||
|
else:
|
||||||
|
d['file'] = None
|
||||||
|
|
||||||
|
# Check stream
|
||||||
|
if self.stream_exists:
|
||||||
|
d['stream'] = True
|
||||||
|
else:
|
||||||
|
d['stream'] = False
|
||||||
|
|
||||||
|
# Check if file was manually deleted
|
||||||
|
if self.keep_on_disk and not self.file_exists:
|
||||||
|
d['path'] = "{} (Deleted)".format(d['path'])
|
||||||
|
|
||||||
|
return d
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def data(self) -> io.BytesIO:
|
def data(self) -> io.BytesIO:
|
||||||
"""Return a byte string of the capture data"""
|
"""Return a byte string of the capture data."""
|
||||||
self.stream.seek(0) # Rewind the data bytes for reading
|
self.stream.seek(0) # Rewind the data bytes for reading
|
||||||
|
|
||||||
if not self.stream.getvalue() and os.path.isfile(self.file): # If stream is empty but file is not
|
if self.stream_exists: # If data stream contains data
|
||||||
log("No stream data. Opening from file {}".format(self.file))
|
data = io.BytesIO(self.stream.getbuffer()) # Create a copy of the stream bytes
|
||||||
with open(self.file, 'rb') as f:
|
|
||||||
self.stream = io.BytesIO(f.read()) # Load bytes from file in disk
|
|
||||||
self.stream.seek(0) # Rewind data bytes again
|
|
||||||
|
|
||||||
data = self.stream.getbuffer() # Create a copy of the stream bytes
|
else: # If data stream is empty
|
||||||
|
if self.file_exists: # If data file exists
|
||||||
|
# TODO: Streamline this bit
|
||||||
|
log("No stream data. Opening from file {}".format(self.file))
|
||||||
|
with open(self.file, 'rb') as f:
|
||||||
|
d = io.BytesIO(f.read()) # Load bytes from file in disk
|
||||||
|
d.seek(0) # Rewind loaded stream
|
||||||
|
data = io.BytesIO(d.getbuffer()) # Create a copy of the stream bytes
|
||||||
|
else:
|
||||||
|
data = None
|
||||||
|
|
||||||
return io.BytesIO(data) # Read and return bytes data
|
return data # Read and return bytes data
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def binary(self) -> bytes:
|
def binary(self) -> bytes:
|
||||||
"""Return a byte string of the capture data"""
|
"""Return a byte string of the capture data."""
|
||||||
return self.data.getvalue()
|
return self.data.getvalue()
|
||||||
|
|
||||||
def save(self):
|
def load_file(self) -> bool:
|
||||||
"""
|
"""Load data stored on disk to the in-memory stream."""
|
||||||
Creates/opens a file, and writes this StreamObjects bytestring to the file.
|
if self.file_exists: # If data file exists
|
||||||
"""
|
# TODO: Streamline this bit
|
||||||
with open(self.file, 'ab') as f: # Load file as bytes
|
with open(self.file, 'rb') as f:
|
||||||
log("Writing stream to file {}".format(self.file))
|
self.stream = io.BytesIO(f.read()) # Load bytes from file in disk
|
||||||
f.seek(0, 0) # Seek to the start of the file
|
self.stream.seek(0) # Rewind data bytes again
|
||||||
f.write(self.binary) # Write data bytes to file
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
def clear_stream(self):
|
def save_file(self) -> bool:
|
||||||
|
"""
|
||||||
|
Write the StreamObjects stream to a file.
|
||||||
|
"""
|
||||||
|
if self.stream_exists: # If there's a stream to save
|
||||||
|
with open(self.file, 'ab') as f: # Load file as bytes
|
||||||
|
log("Writing stream to file {}".format(self.file))
|
||||||
|
f.seek(0, 0) # Seek to the start of the file
|
||||||
|
f.write(self.binary) # Write data bytes to file
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def delete_stream(self):
|
||||||
"""Clears the BytesIO stream of the StreamObject."""
|
"""Clears the BytesIO stream of the StreamObject."""
|
||||||
self.stream = io.BytesIO()
|
self.stream = io.BytesIO()
|
||||||
return True
|
|
||||||
|
|
||||||
def delete(self) -> bool:
|
def delete_file(self) -> bool:
|
||||||
"""
|
"""
|
||||||
If the StreamObject has been saved to disk, deletes the file and returns True.
|
If the StreamObject has been saved to disk, deletes the file and returns True.
|
||||||
"""
|
"""
|
||||||
|
|
@ -129,6 +214,18 @@ class StreamObject(object):
|
||||||
log("File not found {}".format(self.file))
|
log("File not found {}".format(self.file))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def shunt(self):
|
||||||
|
"""Demote the StreamObject from being stored in memory."""
|
||||||
|
if not self.file_exists: # If file doesn't already exist
|
||||||
|
self.save_file() # Save stream to disk, if it exists
|
||||||
|
self.delete_stream() # Delete the stream from memory
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
"""Both clear the stream, and delete any associated on-disk data"""
|
||||||
|
self.delete_stream()
|
||||||
|
if not self.keep_on_disk:
|
||||||
|
self.delete_file()
|
||||||
|
|
||||||
|
|
||||||
class CameraEvent(object):
|
class CameraEvent(object):
|
||||||
"""An Event-like class that signals all active clients when a new frame is
|
"""An Event-like class that signals all active clients when a new frame is
|
||||||
|
|
@ -181,11 +278,8 @@ class BaseCamera(object):
|
||||||
event = CameraEvent()
|
event = CameraEvent()
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.start_worker()
|
self.state = {} # Create dict for capture state
|
||||||
|
self.settings = {} # Create dict to store settings
|
||||||
# Stores state of StreamingCamera
|
|
||||||
self.state = {
|
|
||||||
}
|
|
||||||
|
|
||||||
def start_worker(self, timeout: int=5) -> bool:
|
def start_worker(self, timeout: int=5) -> bool:
|
||||||
"""Start the background camera thread if it isn't running yet."""
|
"""Start the background camera thread if it isn't running yet."""
|
||||||
|
|
|
||||||
|
|
@ -53,52 +53,88 @@ HERE = os.path.abspath(os.path.dirname(__file__))
|
||||||
DEFAULT_CONFIG = os.path.join(HERE, 'config_picamera.yaml')
|
DEFAULT_CONFIG = os.path.join(HERE, 'config_picamera.yaml')
|
||||||
|
|
||||||
|
|
||||||
|
def last_entry(object_list: list):
|
||||||
|
if object_list: # If any images have been captured
|
||||||
|
return object_list[-1] # Return the latest captured image
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def entry_by_id(id: str, object_list: list):
|
||||||
|
found = None
|
||||||
|
for o in object_list:
|
||||||
|
if o.id == id:
|
||||||
|
found = o
|
||||||
|
return found
|
||||||
|
|
||||||
class StreamingCamera(BaseCamera):
|
class StreamingCamera(BaseCamera):
|
||||||
"""Raspberry Pi camera implementation of StreamingCamera."""
|
"""Raspberry Pi camera implementation of StreamingCamera."""
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
# Run BaseCamera init to start thread
|
||||||
|
BaseCamera.__init__(self)
|
||||||
|
|
||||||
# Capture data
|
# Capture data
|
||||||
self.image = None
|
self.images = []
|
||||||
self.video = None
|
self.videos = []
|
||||||
|
|
||||||
# Camera settings
|
# Camera settings
|
||||||
self.settings = {
|
self.settings.update({
|
||||||
'video_resolution': (832, 624),
|
'video_resolution': (832, 624),
|
||||||
'image_resolution': (2592, 1944),
|
'image_resolution': (2592, 1944),
|
||||||
'numpy_resolution': (1312, 976),
|
'numpy_resolution': (1312, 976),
|
||||||
'jpeg_quality': 75,
|
'jpeg_quality': 75,
|
||||||
}
|
})
|
||||||
|
|
||||||
# Store state of StreamingCamera
|
# Store state of StreamingCamera
|
||||||
self.state = {
|
self.state.update({
|
||||||
'video_recent': None,
|
|
||||||
'image_recent': None,
|
|
||||||
'stream_active': False,
|
'stream_active': False,
|
||||||
'record_active': False,
|
'record_active': False,
|
||||||
'preview_active': False,
|
'preview_active': False,
|
||||||
}
|
})
|
||||||
|
|
||||||
# Finally, run BaseCamera init to start thread
|
self.start_worker()
|
||||||
BaseCamera.__init__(self)
|
|
||||||
|
|
||||||
def initialisation(self):
|
def initialisation(self):
|
||||||
"""Run any initialisation code when the frame iterator starts."""
|
"""Run any initialisation code when the frame iterator starts."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def start_preview(self) -> bool:
|
# RETURNING CAPTURES
|
||||||
"""Start the onboard GPU camera preview."""
|
|
||||||
self.start_worker()
|
|
||||||
|
|
||||||
self.camera.start_preview()
|
@property
|
||||||
self.state['preview_active'] = True
|
def image(self):
|
||||||
return True
|
"""Return the latest captured image"""
|
||||||
|
return last_entry(self.images)
|
||||||
|
|
||||||
def stop_preview(self) -> bool:
|
@property
|
||||||
"""Stop the onboard GPU camera preview."""
|
def video(self):
|
||||||
self.start_worker()
|
"""Return the latest recorded video"""
|
||||||
|
return last_entry(self.videos)
|
||||||
|
|
||||||
self.camera.stop_preview()
|
def image_from_id(self, id):
|
||||||
self.state['preview_active'] = False
|
"""Returns an image StreamObject with a matching ID"""
|
||||||
return True
|
return entry_by_id(id, self.images)
|
||||||
|
|
||||||
|
def video_from_id(self, id):
|
||||||
|
"""Returns a video StreamObject with a matching ID"""
|
||||||
|
return entry_by_id(id, self.videos)
|
||||||
|
|
||||||
|
# CREATING NEW CAPTURES
|
||||||
|
def new_stream_object(self, stream_object, target_list: list, shunt_others: bool=True):
|
||||||
|
"""Add a new capture to a list of captures, and shunt all others"""
|
||||||
|
if shunt_others: # If shunting all older captures
|
||||||
|
for obj in target_list: # For each older capture
|
||||||
|
obj.shunt() # Shunt capture from memory to storage
|
||||||
|
target_list.append(stream_object)
|
||||||
|
return stream_object
|
||||||
|
|
||||||
|
def new_image(self, stream_object, shunt_others=True):
|
||||||
|
"""Add a new capture to the image list, and shunt all others"""
|
||||||
|
return self.new_stream_object(stream_object, self.images, shunt_others=shunt_others)
|
||||||
|
|
||||||
|
def new_video(self, stream_object, shunt_others=True):
|
||||||
|
"""Add a new capture to the video list, and shunt all others"""
|
||||||
|
return self.new_stream_object(stream_object, self.videos, shunt_others=shunt_others)
|
||||||
|
|
||||||
|
# HANDLING SETTINGS
|
||||||
|
|
||||||
# TODO: Handle exceptions
|
# TODO: Handle exceptions
|
||||||
# TODO: Have this take a dictionary (not a config file)
|
# TODO: Have this take a dictionary (not a config file)
|
||||||
|
|
@ -154,10 +190,10 @@ class StreamingCamera(BaseCamera):
|
||||||
if 'digital_gain' in config:
|
if 'digital_gain' in config:
|
||||||
set_digital_gain(self.camera, config['digital_gain'])
|
set_digital_gain(self.camera, config['digital_gain'])
|
||||||
|
|
||||||
# TODO: Rewrite this bit?
|
|
||||||
# https://picamera.readthedocs.io/en/release-1.13/api_camera.html
|
|
||||||
def change_zoom(self, zoom_value: int=1) -> None:
|
def change_zoom(self, zoom_value: int=1) -> None:
|
||||||
"""Change the camera zoom, handling recentering and scaling."""
|
"""Change the camera zoom, handling recentering and scaling."""
|
||||||
|
self.start_worker()
|
||||||
|
|
||||||
zoom_value = float(zoom_value)
|
zoom_value = float(zoom_value)
|
||||||
if zoom_value < 1:
|
if zoom_value < 1:
|
||||||
zoom_value = 1
|
zoom_value = 1
|
||||||
|
|
@ -175,26 +211,22 @@ class StreamingCamera(BaseCamera):
|
||||||
new_fov = (centre[0] - size/2, centre[1] - size/2, size, size)
|
new_fov = (centre[0] - size/2, centre[1] - size/2, size, size)
|
||||||
self.camera.zoom = new_fov
|
self.camera.zoom = new_fov
|
||||||
|
|
||||||
def stop_recording(self, target=None) -> bool:
|
# LAUNCH ACTIONS
|
||||||
"""Stop the last started video recording on splitter port 2.
|
|
||||||
|
|
||||||
target (str/BytesIO): Target object to write data bytes to.
|
def start_preview(self) -> bool:
|
||||||
"""
|
"""Start the onboard GPU camera preview."""
|
||||||
if not target: # If no target is defined
|
self.start_worker()
|
||||||
target_obj = self.video # Assume StreamingCamera as target
|
|
||||||
target_obj.unlock() # Unlock the StreamObject
|
|
||||||
else:
|
|
||||||
target_obj = target
|
|
||||||
|
|
||||||
# Stop the camera video recording on port 2
|
self.camera.start_preview()
|
||||||
log("Stopping recording")
|
self.state['preview_active'] = True
|
||||||
self.camera.stop_recording(splitter_port=2)
|
return True
|
||||||
log("Recording stopped")
|
|
||||||
|
|
||||||
# Update state dictionary
|
def stop_preview(self) -> bool:
|
||||||
self.state['record_active'] = False
|
"""Stop the onboard GPU camera preview."""
|
||||||
self.state['video_recent'] = str(target_obj)
|
self.start_worker()
|
||||||
|
|
||||||
|
self.camera.stop_preview()
|
||||||
|
self.state['preview_active'] = False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def start_recording(
|
def start_recording(
|
||||||
|
|
@ -217,47 +249,56 @@ class StreamingCamera(BaseCamera):
|
||||||
fmt (str): Format of the capture.
|
fmt (str): Format of the capture.
|
||||||
(default 'h264')
|
(default 'h264')
|
||||||
"""
|
"""
|
||||||
self.start_worker()
|
|
||||||
|
|
||||||
# If no target is specified, store to StreamingCamera
|
# Start recording method only if a current recording is not running
|
||||||
if not target:
|
if not self.state['record_active']:
|
||||||
# If target is not locked
|
self.start_worker()
|
||||||
if (self.video is None) or (not self.video.locked):
|
|
||||||
|
|
||||||
self.video = StreamObject(
|
# If no target is specified, store to StreamingCamera
|
||||||
|
if not target:
|
||||||
|
# Create a new video and add to the video list
|
||||||
|
target_obj = self.new_video(StreamObject(
|
||||||
write_to_file=write_to_file,
|
write_to_file=write_to_file,
|
||||||
filename=filename,
|
filename=filename,
|
||||||
folder=folder,
|
folder=folder,
|
||||||
fmt=fmt) # Reset/create StreamObject
|
fmt=fmt))
|
||||||
|
|
||||||
# Store to the StreamObject BytesIO
|
# Lock the StreamObject while recording
|
||||||
target = self.video.target
|
target_obj.lock()
|
||||||
# Note the target StreamObject, used for function return
|
# Store to the StreamObject target
|
||||||
target_obj = self.video
|
target = target_obj.target
|
||||||
|
|
||||||
target_obj.lock() # Lock the StreamObject while recording
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Cannot start recording a new video \
|
target_obj = target
|
||||||
until the current recording has been stopped. \
|
|
||||||
Returning None.")
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
target_obj = target
|
|
||||||
|
|
||||||
# Start the camera video recording on port 2
|
# Start the camera video recording on port 2
|
||||||
log("Starting record at {}".format(self.settings['video_resolution']))
|
log("Starting record at {}".format(self.settings['video_resolution']))
|
||||||
self.camera.start_recording(
|
self.camera.start_recording(
|
||||||
target,
|
target,
|
||||||
format=fmt,
|
format=fmt,
|
||||||
splitter_port=2,
|
splitter_port=2,
|
||||||
resize=self.settings['video_resolution'],
|
resize=self.settings['video_resolution'],
|
||||||
quality=quality)
|
quality=quality)
|
||||||
|
|
||||||
|
# Update state dictionary
|
||||||
|
self.state['record_active'] = True
|
||||||
|
|
||||||
|
return target_obj
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("Cannot start a new recording until the current recording has stopped.")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def stop_recording(self) -> bool:
|
||||||
|
"""Stop the last started video recording on splitter port 2."""
|
||||||
|
|
||||||
|
# Stop the camera video recording on port 2
|
||||||
|
log("Stopping recording")
|
||||||
|
self.camera.stop_recording(splitter_port=2)
|
||||||
|
log("Recording stopped")
|
||||||
|
|
||||||
# Update state dictionary
|
# Update state dictionary
|
||||||
self.state['record_active'] = True
|
self.state['record_active'] = False
|
||||||
|
|
||||||
return target_obj
|
|
||||||
|
|
||||||
def pause_stream_for_capture(self, splitter_port: int=1, resolution: Tuple[int, int]=None) -> None:
|
def pause_stream_for_capture(self, splitter_port: int=1, resolution: Tuple[int, int]=None) -> None:
|
||||||
"""
|
"""
|
||||||
|
|
@ -331,10 +372,9 @@ class StreamingCamera(BaseCamera):
|
||||||
|
|
||||||
# If no target is specified, store to StreamingCamera
|
# If no target is specified, store to StreamingCamera
|
||||||
if not target:
|
if not target:
|
||||||
self.image = StreamObject(write_to_file=write_to_file, filename=filename, folder=folder, fmt=fmt) # Reset StreamObject
|
# Create a new image and add to the image list
|
||||||
|
target_obj = self.new_image(StreamObject(write_to_file=write_to_file, filename=filename, folder=folder, fmt=fmt))
|
||||||
target = self.image.target # Store to the StreamObject BytesIO
|
target = target_obj.target # Store to the StreamObject BytesIO
|
||||||
target_obj = self.image # Note the target StreamObject, used for function return
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
target_obj = target
|
target_obj = target
|
||||||
|
|
@ -412,6 +452,8 @@ class StreamingCamera(BaseCamera):
|
||||||
else:
|
else:
|
||||||
return output.array
|
return output.array
|
||||||
|
|
||||||
|
# HANDLE STREAM FRAMES
|
||||||
|
|
||||||
def frames(self):
|
def frames(self):
|
||||||
"""
|
"""
|
||||||
Create iterator used by worker thread to generate stream frames.
|
Create iterator used by worker thread to generate stream frames.
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import uuid
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
success_string = """
|
success_string = """
|
||||||
/O
|
/O
|
||||||
|
|
@ -40,13 +41,6 @@ class TestCaptureMethods(unittest.TestCase):
|
||||||
|
|
||||||
for use_video_port in [True, False]:
|
for use_video_port in [True, False]:
|
||||||
for resize in [None, (640, 480)]:
|
for resize in [None, (640, 480)]:
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
log("Resize: {}, V_Port: {}, ID: {}".format(
|
|
||||||
resize,
|
|
||||||
use_video_port,
|
|
||||||
id))
|
|
||||||
|
|
||||||
# Wait for camera
|
# Wait for camera
|
||||||
wait_for_cam(camera)
|
wait_for_cam(camera)
|
||||||
|
|
@ -55,11 +49,10 @@ class TestCaptureMethods(unittest.TestCase):
|
||||||
with camera.capture(
|
with camera.capture(
|
||||||
write_to_file=False,
|
write_to_file=False,
|
||||||
use_video_port=use_video_port,
|
use_video_port=use_video_port,
|
||||||
filename=id,
|
|
||||||
resize=resize) as stream:
|
resize=resize) as stream:
|
||||||
|
|
||||||
# Ensure file deletion fails and returns False
|
# Ensure file deletion fails and returns False
|
||||||
self.assertFalse(stream.delete())
|
self.assertFalse(stream.delete_file())
|
||||||
# Ensure capture not stored to file
|
# Ensure capture not stored to file
|
||||||
self.assertFalse(os.path.isfile(stream.file))
|
self.assertFalse(os.path.isfile(stream.file))
|
||||||
|
|
||||||
|
|
@ -75,12 +68,12 @@ class TestCaptureMethods(unittest.TestCase):
|
||||||
))
|
))
|
||||||
|
|
||||||
# Save capture to file
|
# Save capture to file
|
||||||
stream.save()
|
stream.save_file()
|
||||||
# Check file got saved
|
# Check file got saved
|
||||||
self.assertTrue(os.path.isfile(stream.file))
|
self.assertTrue(os.path.isfile(stream.file))
|
||||||
|
|
||||||
# Delete file
|
# Delete file
|
||||||
stream.delete()
|
stream.delete_file()
|
||||||
# Check file got deleted
|
# Check file got deleted
|
||||||
self.assertFalse(os.path.isfile(stream.file))
|
self.assertFalse(os.path.isfile(stream.file))
|
||||||
|
|
||||||
|
|
@ -117,9 +110,6 @@ class TestCaptureMethods(unittest.TestCase):
|
||||||
|
|
||||||
for use_video_port in [True, False]:
|
for use_video_port in [True, False]:
|
||||||
for resize in [None, (640, 480)]:
|
for resize in [None, (640, 480)]:
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
# Wait for camera
|
# Wait for camera
|
||||||
wait_for_cam(camera)
|
wait_for_cam(camera)
|
||||||
|
|
||||||
|
|
@ -127,7 +117,6 @@ class TestCaptureMethods(unittest.TestCase):
|
||||||
stream = camera.capture(
|
stream = camera.capture(
|
||||||
write_to_file=True,
|
write_to_file=True,
|
||||||
use_video_port=use_video_port,
|
use_video_port=use_video_port,
|
||||||
filename=id,
|
|
||||||
resize=resize)
|
resize=resize)
|
||||||
|
|
||||||
# Check file got saved
|
# Check file got saved
|
||||||
|
|
@ -141,7 +130,7 @@ class TestCaptureMethods(unittest.TestCase):
|
||||||
self.assertTrue(isinstance(stream.binary, (bytes, bytearray)))
|
self.assertTrue(isinstance(stream.binary, (bytes, bytearray)))
|
||||||
|
|
||||||
# Ensure file deletion completes and returns True
|
# Ensure file deletion completes and returns True
|
||||||
self.assertTrue(stream.delete())
|
self.assertTrue(stream.delete_file())
|
||||||
|
|
||||||
# Check file got deleted
|
# Check file got deleted
|
||||||
self.assertFalse(os.path.isfile(stream.file))
|
self.assertFalse(os.path.isfile(stream.file))
|
||||||
|
|
@ -155,8 +144,6 @@ class TestUnencodedMethods(unittest.TestCase):
|
||||||
|
|
||||||
for use_video_port in [True, False]:
|
for use_video_port in [True, False]:
|
||||||
for resize in [None, (640, 480)]:
|
for resize in [None, (640, 480)]:
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
print("{}, {}, {}".format(id, resize, use_video_port))
|
print("{}, {}, {}".format(id, resize, use_video_port))
|
||||||
|
|
||||||
|
|
@ -189,8 +176,6 @@ class TestUnencodedMethods(unittest.TestCase):
|
||||||
|
|
||||||
for use_video_port in [True, False]:
|
for use_video_port in [True, False]:
|
||||||
for resize in [None, (640, 480)]:
|
for resize in [None, (640, 480)]:
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
print("{}, {}, {}".format(id, resize, use_video_port))
|
print("{}, {}, {}".format(id, resize, use_video_port))
|
||||||
|
|
||||||
|
|
@ -227,16 +212,12 @@ class TestRecordMethods(unittest.TestCase):
|
||||||
|
|
||||||
for write_to_file in [True, False, None]:
|
for write_to_file in [True, False, None]:
|
||||||
|
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
# Wait for camera
|
# Wait for camera
|
||||||
wait_for_cam(camera)
|
wait_for_cam(camera)
|
||||||
|
|
||||||
# Start recording
|
# Start recording
|
||||||
with camera.start_recording(
|
with camera.start_recording(
|
||||||
write_to_file=write_to_file,
|
write_to_file=write_to_file) as stream:
|
||||||
filename=id) as stream:
|
|
||||||
|
|
||||||
# Record for 2 seconds
|
# Record for 2 seconds
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
@ -264,14 +245,11 @@ class TestRecordMethods(unittest.TestCase):
|
||||||
"""Tests recording videos to file on disk, without context manager."""
|
"""Tests recording videos to file on disk, without context manager."""
|
||||||
global camera
|
global camera
|
||||||
|
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
# Wait for camera
|
# Wait for camera
|
||||||
wait_for_cam(camera)
|
wait_for_cam(camera)
|
||||||
|
|
||||||
# Start recording
|
# Start recording
|
||||||
stream = camera.start_recording(filename=id)
|
stream = camera.start_recording()
|
||||||
# Record for 2 seconds
|
# Record for 2 seconds
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
# Stop recording
|
# Stop recording
|
||||||
|
|
@ -282,7 +260,7 @@ class TestRecordMethods(unittest.TestCase):
|
||||||
self.assertTrue(statinfo.st_size > 0)
|
self.assertTrue(statinfo.st_size > 0)
|
||||||
|
|
||||||
# Ensure file deletion completes and returns True
|
# Ensure file deletion completes and returns True
|
||||||
self.assertTrue(stream.delete())
|
self.assertTrue(stream.delete_file())
|
||||||
|
|
||||||
# Check file got deleted
|
# Check file got deleted
|
||||||
self.assertFalse(os.path.isfile(stream.file))
|
self.assertFalse(os.path.isfile(stream.file))
|
||||||
|
|
@ -293,9 +271,6 @@ class TestThreadStarting(unittest.TestCase):
|
||||||
"""Tests that a capture call restarts the camera worker thread."""
|
"""Tests that a capture call restarts the camera worker thread."""
|
||||||
global camera
|
global camera
|
||||||
|
|
||||||
# Create unique ID
|
|
||||||
id = uuid.uuid4().hex
|
|
||||||
|
|
||||||
# Wait for camera
|
# Wait for camera
|
||||||
wait_for_cam(camera)
|
wait_for_cam(camera)
|
||||||
|
|
||||||
|
|
@ -307,8 +282,7 @@ class TestThreadStarting(unittest.TestCase):
|
||||||
|
|
||||||
# Restart worker thread by initialising a capture
|
# Restart worker thread by initialising a capture
|
||||||
with camera.capture(
|
with camera.capture(
|
||||||
use_video_port=True,
|
use_video_port=True) as stream:
|
||||||
filename=id) as stream:
|
|
||||||
|
|
||||||
# Ensure StreamObject 'stream' has
|
# Ensure StreamObject 'stream' has
|
||||||
# a valid BytesIO object and byte string
|
# a valid BytesIO object and byte string
|
||||||
|
|
@ -334,4 +308,10 @@ if __name__ == '__main__':
|
||||||
if result.wasSuccessful():
|
if result.wasSuccessful():
|
||||||
print(success_string)
|
print(success_string)
|
||||||
|
|
||||||
|
# Show us what was captured
|
||||||
|
for im in camera.images:
|
||||||
|
pprint(im.metadata)
|
||||||
|
for im in camera.videos:
|
||||||
|
pprint(im.metadata)
|
||||||
|
|
||||||
camera.close()
|
camera.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue