Allow log of multiple captures by StreamingCamera

This commit is contained in:
Joel Collins 2018-11-06 18:24:35 +00:00
parent 8031327a5b
commit d99602bd3d
3 changed files with 261 additions and 145 deletions

View file

@ -4,6 +4,7 @@ import io
import threading
import copy
import datetime
import uuid
from PIL import Image
try:
@ -26,43 +27,60 @@ def log(s):
class StreamObject(object):
def __init__(self, write_to_file: bool=None, filename: str=None, folder: str=None, fmt: str='file') -> None:
# Store a nice ID
self.id = filename
self.id = uuid.uuid4().hex
log("Created {}".format(self.id))
# 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
self.stream = io.BytesIO() # Byte stream that data will be written to
# Set default write target
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))
self.target = self.stream
else:
log("Default target for {} set to 'file'".format(self.id))
self.target = self.file
# Keep on disk after close by default
self.keep_on_disk = True
# Object lock
self.locked = False # Is the StreamObject locked for writing? (Handled by StreamingCamera)
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
def __exit__(self, *args):
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:
file_name = "{}.{}".format(filename, fmt)
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
if folder:
@ -76,48 +94,115 @@ class StreamObject(object):
return file_path
def lock(self):
"""Set locked flag to True"""
"""Set locked flag to True."""
self.locked = True
def unlock(self):
"""Set locked flag to False"""
"""Set locked flag to 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
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
if not self.stream.getvalue() and os.path.isfile(self.file): # If stream is empty but file is not
log("No stream data. Opening from file {}".format(self.file))
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
if self.stream_exists: # If data stream contains data
data = io.BytesIO(self.stream.getbuffer()) # Create a copy of the stream bytes
return io.BytesIO(data) # Read and return bytes data
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 data # Read and return bytes data
@property
def binary(self) -> bytes:
"""Return a byte string of the capture data"""
"""Return a byte string of the capture data."""
return self.data.getvalue()
def save(self):
"""
Creates/opens a file, and writes this StreamObjects bytestring to the file.
"""
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
def load_file(self) -> bool:
"""Load data stored on disk to the in-memory stream."""
if self.file_exists: # If data file exists
# TODO: Streamline this bit
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
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."""
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.
"""
@ -129,6 +214,18 @@ class StreamObject(object):
log("File not found {}".format(self.file))
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):
"""An Event-like class that signals all active clients when a new frame is
@ -181,11 +278,8 @@ class BaseCamera(object):
event = CameraEvent()
def __init__(self):
self.start_worker()
# Stores state of StreamingCamera
self.state = {
}
self.state = {} # Create dict for capture state
self.settings = {} # Create dict to store settings
def start_worker(self, timeout: int=5) -> bool:
"""Start the background camera thread if it isn't running yet."""