Reenfocing code style

This commit is contained in:
boltgolt 2020-06-21 17:40:44 +02:00
parent b11c8188e6
commit a04b33d01b
No known key found for this signature in database
GPG key ID: BECEC9937E1AAE26
5 changed files with 46 additions and 38 deletions

View file

@ -3,7 +3,6 @@
# Import required modules # Import required modules
import sys import sys
import os import os
import json
import builtins import builtins
import fileinput import fileinput
import configparser import configparser

View file

@ -49,7 +49,9 @@ def print_text(line_number, text):
"""Print the status text by line number""" """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) 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) use_cnn = config.getboolean('core', 'use_cnn', fallback=False)
if use_cnn: if use_cnn:
face_detector = dlib.cnn_face_detection_model_v1( face_detector = dlib.cnn_face_detection_model_v1(
path + '/../dlib-data/mmod_human_face_detector.dat' path + '/../dlib-data/mmod_human_face_detector.dat'

View file

@ -47,6 +47,7 @@ def init_detector(lock):
timings["ll"] = time.time() - timings["ll"] timings["ll"] = time.time() - timings["ll"]
lock.release() lock.release()
# Make sure we were given an username to tast against # Make sure we were given an username to tast against
if len(sys.argv) < 2: if len(sys.argv) < 2:
sys.exit(12) sys.exit(12)
@ -149,17 +150,17 @@ while True:
# Stop if we've exceded the time limit # Stop if we've exceded the time limit
if time.time() - timings["fr"] > timeout: 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("All frames were too dark, please check dark_threshold in config")
print("Average darkness: " + str(dark_running_total / valid_frames) + ", Threshold: " + str(dark_threshold)) print("Average darkness: " + str(dark_running_total / valid_frames) + ", Threshold: " + str(dark_threshold))
sys.exit(13) sys.exit(13)
else: else:
sys.exit(11) sys.exit(11)
# Grab a single frame of video # Grab a single frame of video
frame, gsframe = video_capture.read_frame() frame, gsframe = video_capture.read_frame()
gsframe = clahe.apply(gsframe) gsframe = clahe.apply(gsframe)
# Create a histogram of the image with 8 values # Create a histogram of the image with 8 values
hist = cv2.calcHist([gsframe], [0], None, [8], [0, 256]) hist = cv2.calcHist([gsframe], [0], None, [8], [0, 256])

View file

@ -37,7 +37,7 @@ timeout = 4
# The path of the device to capture frames from # The path of the device to capture frames from
# Should be set automatically by an installer if your distro has one # 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 # Scale down the video feed to this maximum height
# Speeds up face recognition but can make it less precise # Speeds up face recognition but can make it less precise

View file

@ -7,21 +7,22 @@ import cv2
import os import os
import sys 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: 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): 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): if isinstance(config, str):
self.config = configparser.ConfigParser() self.config = configparser.ConfigParser()
self.config.read(config) self.config.read(config)
@ -30,41 +31,43 @@ class VideoCapture:
# Check device path # Check device path
if not os.path.exists(config.get("video", "device_path")): if not os.path.exists(config.get("video", "device_path")):
print("Camera path is not configured correctly, " + print("Camera path is not configured correctly, please edit the 'device_path' config value.")
"please edit the 'device_path' config value.")
sys.exit(1) sys.exit(1)
# Create reader # Create reader
self.internal = None # The internal video recorder # The internal video recorder
self.fw = None # The frame width self.internal = None
self.fh = None # The frame height # The frame width
self.fw = None
# The frame height
self.fh = None
self._create_reader() self._create_reader()
# Request a frame to wake the camera up # Request a frame to wake the camera up
self.internal.grab() self.internal.grab()
"""
Frees resources when destroyed
"""
def __del__(self): def __del__(self):
"""
Frees resources when destroyed
"""
if self is not None: if self is not None:
self.internal.release() 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): 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 # Grab a single frame of video
# Don't remove ret, it doesn't work without it # Don't remove ret, it doesn't work without it
ret, frame = self.internal.read() ret, frame = self.internal.read()
if not ret: if not ret:
print("Failed to read camera specified in your 'device_path', " + print("Failed to read camera specified in your 'device_path', aborting")
"aborting")
sys.exit(1) sys.exit(1)
try: try:
@ -78,10 +81,11 @@ class VideoCapture:
raise raise
return frame, gsframe return frame, gsframe
"""
Sets up the video reader instance
"""
def _create_reader(self): def _create_reader(self):
"""
Sets up the video reader instance
"""
if self.config.get("video", "recording_plugin") == "ffmpeg": if self.config.get("video", "recording_plugin") == "ffmpeg":
# Set the capture source for ffmpeg # Set the capture source for ffmpeg
from recorders.ffmpeg_reader import ffmpeg_reader from recorders.ffmpeg_reader import ffmpeg_reader
@ -89,6 +93,7 @@ class VideoCapture:
self.config.get("video", "device_path"), self.config.get("video", "device_path"),
self.config.get("video", "device_format") self.config.get("video", "device_format")
) )
elif self.config.get("video", "recording_plugin") == "pyv4l2": elif self.config.get("video", "recording_plugin") == "pyv4l2":
# Set the capture source for pyv4l2 # Set the capture source for pyv4l2
from recorders.pyv4l2_reader import pyv4l2_reader from recorders.pyv4l2_reader import pyv4l2_reader
@ -96,6 +101,7 @@ class VideoCapture:
self.config.get("video", "device_path"), self.config.get("video", "device_path"),
self.config.get("video", "device_format") self.config.get("video", "device_format")
) )
else: else:
# Start video capture on the IR camera through OpenCV # Start video capture on the IR camera through OpenCV
self.internal = cv2.VideoCapture( self.internal = cv2.VideoCapture(