Fixed non-clashing basename generation
This commit is contained in:
parent
0e76ae673a
commit
69e97ba5a3
3 changed files with 50 additions and 38 deletions
|
|
@ -3,6 +3,7 @@ import time
|
||||||
import io
|
import io
|
||||||
import threading
|
import threading
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -33,6 +34,11 @@ def entry_by_id(id: str, object_list: list):
|
||||||
return found
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def generate_basename():
|
||||||
|
"""Return a default filename based on the capture datetime"""
|
||||||
|
return datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
||||||
|
|
||||||
|
|
||||||
class CameraEvent(object):
|
class CameraEvent(object):
|
||||||
"""
|
"""
|
||||||
A frame-signaller object used by any instances or subclasses of BaseCamera.
|
A frame-signaller object used by any instances or subclasses of BaseCamera.
|
||||||
|
|
@ -222,6 +228,18 @@ class BaseCamera(object):
|
||||||
self.videos,
|
self.videos,
|
||||||
shunt_others=shunt_others)
|
shunt_others=shunt_others)
|
||||||
|
|
||||||
|
# INTELLIGENTLY GENERATE FILENAMES
|
||||||
|
def generate_basename(self, obj_list: list) -> str:
|
||||||
|
initial_basename = generate_basename()
|
||||||
|
basename = initial_basename
|
||||||
|
# Handle clashing
|
||||||
|
iterator = 1
|
||||||
|
while basename in [obj.basename for obj in obj_list]:
|
||||||
|
basename = initial_basename + "_{}".format(iterator)
|
||||||
|
iterator += 1
|
||||||
|
|
||||||
|
return basename
|
||||||
|
|
||||||
# WORKER THREAD
|
# WORKER THREAD
|
||||||
|
|
||||||
def _thread(self):
|
def _thread(self):
|
||||||
|
|
|
||||||
|
|
@ -29,20 +29,10 @@ class StreamObject(object):
|
||||||
# Store file format
|
# Store file format
|
||||||
self.format = fmt
|
self.format = fmt
|
||||||
|
|
||||||
# Create file name
|
# Create file name. Default to UUID
|
||||||
iterator = 0
|
if not filename:
|
||||||
f_path, f_name = self.build_file_path(filename, folder, fmt)
|
filename = self.id
|
||||||
|
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_path, f_name = self.build_file_path(
|
|
||||||
filename,
|
|
||||||
folder,
|
|
||||||
fmt,
|
|
||||||
iterator=iterator) # Rebuild file name
|
|
||||||
|
|
||||||
self.file = f_path
|
|
||||||
self.filename = 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
|
||||||
|
|
@ -85,21 +75,13 @@ class StreamObject(object):
|
||||||
self,
|
self,
|
||||||
filename: str,
|
filename: str,
|
||||||
folder: str,
|
folder: str,
|
||||||
fmt: str,
|
fmt: str):
|
||||||
iterator: int=0) -> str:
|
|
||||||
"""
|
"""
|
||||||
Construct a full file path, based on filename, folder, and file format.
|
Construct a full file path, based on filename, folder, and file format.
|
||||||
|
|
||||||
Defaults to datestamp.
|
Defaults to datestamp.
|
||||||
Iterator adds a numeric increment to the file name.
|
|
||||||
"""
|
"""
|
||||||
if filename:
|
file_name = "{}.{}".format(filename, fmt)
|
||||||
file_name = "{}.{}".format(filename, fmt)
|
|
||||||
else:
|
|
||||||
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:
|
||||||
|
|
@ -110,7 +92,9 @@ class StreamObject(object):
|
||||||
else:
|
else:
|
||||||
file_path = file_name
|
file_path = file_name
|
||||||
|
|
||||||
return (file_path, file_name)
|
self.basename = filename
|
||||||
|
self.file = file_path
|
||||||
|
self.filename = file_name
|
||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
"""Set locked flag to True."""
|
"""Set locked flag to True."""
|
||||||
|
|
|
||||||
|
|
@ -238,11 +238,13 @@ 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:
|
||||||
# Create a new video and add to the video list
|
# Create a new video and add to the video list
|
||||||
target_obj = self.new_video(StreamObject(
|
target_obj = self.new_video(
|
||||||
write_to_file=write_to_file,
|
StreamObject(
|
||||||
filename=filename,
|
write_to_file=write_to_file,
|
||||||
folder=folder,
|
filename=filename,
|
||||||
fmt=fmt))
|
folder=folder,
|
||||||
|
fmt=fmt)
|
||||||
|
)
|
||||||
|
|
||||||
# Lock the StreamObject while recording
|
# Lock the StreamObject while recording
|
||||||
target_obj.lock()
|
target_obj.lock()
|
||||||
|
|
@ -355,15 +357,23 @@ class StreamingCamera(BaseCamera):
|
||||||
fmt (str): Format of the capture.
|
fmt (str): Format of the capture.
|
||||||
resize ((int, int)): Resize the captured image.
|
resize ((int, int)): Resize the captured image.
|
||||||
"""
|
"""
|
||||||
|
# If no filename is specified, build a non-clashing one
|
||||||
|
if not filename:
|
||||||
|
filename = self.generate_basename(self.images)
|
||||||
|
logging.debug(filename)
|
||||||
|
|
||||||
# If no target is specified, store to StreamingCamera
|
# If no target is specified, store to StreamingCamera
|
||||||
if not target:
|
if not target:
|
||||||
|
# TODO: Handle clashing file names here instead of in capture method.
|
||||||
# Create a new image and add to the image list
|
# Create a new image and add to the image list
|
||||||
target_obj = self.new_image(StreamObject(
|
target_obj = self.new_image(
|
||||||
write_to_file=write_to_file,
|
StreamObject(
|
||||||
keep_on_disk=keep_on_disk,
|
write_to_file=write_to_file,
|
||||||
filename=filename,
|
keep_on_disk=keep_on_disk,
|
||||||
folder=folder,
|
filename=filename,
|
||||||
fmt=fmt))
|
folder=folder,
|
||||||
|
fmt=fmt)
|
||||||
|
)
|
||||||
target = target_obj.target # Store to the StreamObject BytesIO
|
target = target_obj.target # Store to the StreamObject BytesIO
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue