From fff9f3a4abf79c70d9319bb8019ed16dceaef613 Mon Sep 17 00:00:00 2001 From: boltgolt Date: Sun, 21 Jun 2020 22:17:48 +0200 Subject: [PATCH] Added snapshot command --- src/cli.py | 6 ++++-- src/cli/snap.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ src/snapshot.py | 3 +++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/cli/snap.py diff --git a/src/cli.py b/src/cli.py index 5906f4d..78e251a 100755 --- a/src/cli.py +++ b/src/cli.py @@ -34,9 +34,9 @@ parser = argparse.ArgumentParser(description="Command line interface for Howdy f # Add an argument for the command parser.add_argument("command", - help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove or test.", + help="The command option to execute, can be one of the following: add, clear, config, disable, list, remove, test or snapshot.", metavar="command", - choices=["add", "clear", "config", "disable", "list", "remove", "test"]) + choices=["add", "clear", "config", "disable", "list", "remove", "test", "snapshot"]) # Add an argument for the extra arguments of diable and remove parser.add_argument("argument", @@ -99,3 +99,5 @@ elif args.command == "remove": import cli.remove elif args.command == "test": import cli.test +elif args.command == "snapshot": + import cli.snap diff --git a/src/cli/snap.py b/src/cli/snap.py new file mode 100644 index 0000000..7dce8f5 --- /dev/null +++ b/src/cli/snap.py @@ -0,0 +1,51 @@ +# Create a snapshot + +# Import required modules +import os +import configparser +import datetime +import snapshot +from recorders.video_capture import VideoCapture + +# Get the absolute path to the current directory +path = os.path.abspath(__file__ + "/..") + +# Read the config +config = configparser.ConfigParser() +config.read(path + "/../config.ini") + +# Start video capture +video_capture = VideoCapture(config) + +# Read a frame to activate emitters +video_capture.read_frame() + +# Read exposure and dark_thresholds from config to use in the main loop +exposure = config.getint("video", "exposure", fallback=-1) +dark_threshold = config.getfloat("video", "dark_threshold") + +# COllection of recorded frames +frames = [] + +while True: + # Grab a single frame of video + frame, gsframe = video_capture.read_frame() + + # Add the frame to the list + frames.append(frame) + + # Stop the loop if we have 4 frames + if len(frames) >= 4: + break + +# Generate a snapshot image from the frames +file = snapshot.generate(frames, [ + "GENERATED SNAPSHOT", + "Date: " + datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S UTC"), + "Dark threshold config: " + str(config.getfloat("video", "dark_threshold", fallback=50.0)), + "Certainty config: " + str(config.getfloat("video", "certainty", fallback=3.5)) +]) + +# Show the file location in console +print("Generated snapshot saved as") +print(file) diff --git a/src/snapshot.py b/src/snapshot.py index 20f1f54..a2d8d8c 100644 --- a/src/snapshot.py +++ b/src/snapshot.py @@ -57,3 +57,6 @@ def generate(frames, text_lines): filename = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%S.jpg") # Write the image to that file cv2.imwrite(abpath + "/snapshots/" + filename, snap) + + # Return the saved file location + return abpath + "/snapshots/" + filename