From a04b33d01bc0378455e33226ab3d25b9b11b497f Mon Sep 17 00:00:00 2001 From: boltgolt Date: Sun, 21 Jun 2020 17:40:44 +0200 Subject: [PATCH] Reenfocing code style --- src/cli/disable.py | 1 - src/cli/test.py | 2 + src/compare.py | 9 +++-- src/config.ini | 2 +- src/recorders/video_capture.py | 70 ++++++++++++++++++---------------- 5 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/cli/disable.py b/src/cli/disable.py index cba8931..5f15d5b 100644 --- a/src/cli/disable.py +++ b/src/cli/disable.py @@ -3,7 +3,6 @@ # Import required modules import sys import os -import json import builtins import fileinput import configparser diff --git a/src/cli/test.py b/src/cli/test.py index fa2d7cd..21cb2db 100644 --- a/src/cli/test.py +++ b/src/cli/test.py @@ -49,7 +49,9 @@ def print_text(line_number, text): """Print the status text by line number""" cv2.putText(overlay, text, (10, height - 10 - (10 * line_number)), cv2.FONT_HERSHEY_SIMPLEX, .3, (0, 255, 0), 0, cv2.LINE_AA) + use_cnn = config.getboolean('core', 'use_cnn', fallback=False) + if use_cnn: face_detector = dlib.cnn_face_detection_model_v1( path + '/../dlib-data/mmod_human_face_detector.dat' diff --git a/src/compare.py b/src/compare.py index bef7df5..8ddd0c7 100644 --- a/src/compare.py +++ b/src/compare.py @@ -47,6 +47,7 @@ def init_detector(lock): timings["ll"] = time.time() - timings["ll"] lock.release() + # Make sure we were given an username to tast against if len(sys.argv) < 2: sys.exit(12) @@ -149,17 +150,17 @@ while True: # Stop if we've exceded the time limit if time.time() - timings["fr"] > timeout: - if (dark_tries == valid_frames ): + if (dark_tries == valid_frames): print("All frames were too dark, please check dark_threshold in config") print("Average darkness: " + str(dark_running_total / valid_frames) + ", Threshold: " + str(dark_threshold)) - sys.exit(13) + sys.exit(13) else: - sys.exit(11) + sys.exit(11) # Grab a single frame of video frame, gsframe = video_capture.read_frame() - gsframe = clahe.apply(gsframe) + gsframe = clahe.apply(gsframe) # Create a histogram of the image with 8 values hist = cv2.calcHist([gsframe], [0], None, [8], [0, 256]) diff --git a/src/config.ini b/src/config.ini index 1183f48..a6a0fa3 100644 --- a/src/config.ini +++ b/src/config.ini @@ -37,7 +37,7 @@ timeout = 4 # The path of the device to capture frames from # Should be set automatically by an installer if your distro has one -device_path = none +device_path = "/dev/video1" # Scale down the video feed to this maximum height # Speeds up face recognition but can make it less precise diff --git a/src/recorders/video_capture.py b/src/recorders/video_capture.py index d488ebe..af64ba2 100644 --- a/src/recorders/video_capture.py +++ b/src/recorders/video_capture.py @@ -7,21 +7,22 @@ import cv2 import os import sys -""" -Class to provide boilerplate code to build a video recorder with the -correct settings from the config file. -The internal recorder can be accessed with 'video_capture.internal' -""" +# Class to provide boilerplate code to build a video recorder with the +# correct settings from the config file. +# +# The internal recorder can be accessed with 'video_capture.internal' + + class VideoCapture: - """ - Creates a new VideoCapture instance depending on the settings in the - provided config file. - - Config can either be a string to the path, or a pre-setup configparser. - """ def __init__(self, config): + """ + Creates a new VideoCapture instance depending on the settings in the + provided config file. + + Config can either be a string to the path, or a pre-setup configparser. + """ if isinstance(config, str): self.config = configparser.ConfigParser() self.config.read(config) @@ -30,41 +31,43 @@ class VideoCapture: # Check device path if not os.path.exists(config.get("video", "device_path")): - print("Camera path is not configured correctly, " + - "please edit the 'device_path' config value.") + print("Camera path is not configured correctly, please edit the 'device_path' config value.") sys.exit(1) # Create reader - self.internal = None # The internal video recorder - self.fw = None # The frame width - self.fh = None # The frame height + # The internal video recorder + self.internal = None + # The frame width + self.fw = None + # The frame height + self.fh = None self._create_reader() # Request a frame to wake the camera up self.internal.grab() - """ - Frees resources when destroyed - """ def __del__(self): + """ + Frees resources when destroyed + """ if self is not None: self.internal.release() - """ - Reads a frame, returns the frame and an attempted grayscale conversion of - the frame in a tuple: - - (frame, grayscale_frame) - - If the grayscale conversion fails, both items in the tuple are identical. - """ def read_frame(self): + """ + Reads a frame, returns the frame and an attempted grayscale conversion of + the frame in a tuple: + + (frame, grayscale_frame) + + If the grayscale conversion fails, both items in the tuple are identical. + """ + # Grab a single frame of video # Don't remove ret, it doesn't work without it ret, frame = self.internal.read() if not ret: - print("Failed to read camera specified in your 'device_path', " + - "aborting") + print("Failed to read camera specified in your 'device_path', aborting") sys.exit(1) try: @@ -78,10 +81,11 @@ class VideoCapture: raise return frame, gsframe - """ - Sets up the video reader instance - """ def _create_reader(self): + """ + Sets up the video reader instance + """ + if self.config.get("video", "recording_plugin") == "ffmpeg": # Set the capture source for ffmpeg from recorders.ffmpeg_reader import ffmpeg_reader @@ -89,6 +93,7 @@ class VideoCapture: self.config.get("video", "device_path"), self.config.get("video", "device_format") ) + elif self.config.get("video", "recording_plugin") == "pyv4l2": # Set the capture source for pyv4l2 from recorders.pyv4l2_reader import pyv4l2_reader @@ -96,6 +101,7 @@ class VideoCapture: self.config.get("video", "device_path"), self.config.get("video", "device_format") ) + else: # Start video capture on the IR camera through OpenCV self.internal = cv2.VideoCapture(