Skip to content

Commit

Permalink
Cleanup Pylint warnings (#33).
Browse files Browse the repository at this point in the history
  • Loading branch information
Breakthrough committed Feb 3, 2021
1 parent d4634a9 commit c588aa2
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 166 deletions.
19 changes: 16 additions & 3 deletions dvr_scan/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# DVR-Scan: Find & Export Motion Events in Video Footage
# --------------------------------------------------------------
Expand All @@ -24,6 +25,19 @@
# OTHER DEALINGS IN THE SOFTWARE.
#

""" ``dvr_scan`` Module
This is the main DVR-Scan module containing all application logic,
motion detection implementation, and command line processing. The
modules are organized as follows:
dvr_scan.cli:
Command-line interface (argparse)
dvr_scan.scanner:
Application logic + motion detection algorithm (ScanContext)
"""

# Standard Library Imports
from __future__ import print_function

Expand All @@ -33,13 +47,13 @@


# Used for module identification and when printing copyright & version info.
__version__ = 'v1.1'
__version__ = 'v1.2-dev'

# About & copyright message string shown for the -v/--version CLI argument.
ABOUT_STRING = """-----------------------------------------------
DVR-Scan %s
-----------------------------------------------
Copyright (C) 2016-2020 Brandon Castellano
Copyright (C) 2016-2021 Brandon Castellano
< https://github.com/Breakthrough/DVR-Scan >
This DVR-Scan is licensed under the BSD 2-Clause license; see the
Expand All @@ -64,4 +78,3 @@ def main():
# If the context was successfully initialized, we can process the video(s).
if sctx.initialized is True:
sctx.scan_motion()

8 changes: 6 additions & 2 deletions dvr_scan/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# DVR-Scan: Video Motion Event Detection & Extraction Tool
# --------------------------------------------------------------
Expand Down Expand Up @@ -32,7 +32,11 @@
# OTHER DEALINGS IN THE SOFTWARE.
#

""" ``dvr_scan.__main__`` Module
Provides entry point for DVR-Scan's command-line interface (CLI).
"""

if __name__ == '__main__':
import dvr_scan
dvr_scan.main()

248 changes: 125 additions & 123 deletions dvr_scan/cli.py

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions dvr_scan/platform.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# DVR-Scan: Find & Export Motion Events in Video Footage
# --------------------------------------------------------------
Expand Down Expand Up @@ -25,6 +26,21 @@
# OTHER DEALINGS IN THE SOFTWARE.
#

""" ``dvr_scan.platform`` Module
This file contains all platform/library/OS-specific compatibility fixes,
intended to improve the systems that are able to run DVR-Scan, and allow
for maintaining backwards compatibility with existing libraries going forwards.
For OpenCV 2.x, the scenedetect.platform module also makes a copy of the
OpenCV VideoCapture property constants from the cv2.cv namespace directly
to the cv2 namespace. This ensures that the cv2 API is consistent
with those changes made to it in OpenCV 3.0 and above.
TODO: Replace with PySceneDetect's platform module to reduce code duplication
across both projects.
"""

# Third-Party Library Imports
import cv2

Expand Down
57 changes: 28 additions & 29 deletions dvr_scan/scanner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# DVR-Scan: Video Motion Event Detection & Extraction Tool
# --------------------------------------------------------------
Expand Down Expand Up @@ -26,60 +27,65 @@
# OTHER DEALINGS IN THE SOFTWARE.
#

""" ``dvr_scan.scanner`` Module
This module contains the ScanContext class, which implements the
DVR-Scan program logic, as well as the motion detection algorithm.
"""

# Standard Library Imports
from __future__ import print_function
import os
import time

# DVR-Scan Library Imports
from dvr_scan.timecode import FrameTimecode
import dvr_scan.platform

# Third-Party Library Imports
import cv2
import numpy as np

# DVR-Scan Library Imports
from dvr_scan.timecode import FrameTimecode
import dvr_scan.platform


class ScanContext(object):
""" The ScanContext object represents the DVR-Scan program state,
which includes application initialization, handling the options,
and coordinating overall application logic (via scan_motion()). """

def __init__(self, args, gui=None):
def __init__(self, args):
""" Initializes the ScanContext with the supplied arguments. """
# We close the open file handles, as only the paths are required.
for input_file in args.input:
input_file.close()

if not args.quiet_mode:
print("[DVR-Scan] Initializing scan context...")

self.initialized = False
self.running = True # Allows asynchronous termination of scanning loop.

self.event_list = []

self.suppress_output = args.quiet_mode
self.frames_processed = -1
self.frames_processed = -1
self.frames_total = -1

self._cap = None
self._cap_path = None

self.video_resolution = None
self.video_fps = None
self.video_paths = [input_file.name for input_file in args.input]
self.frames_total = 0
self.frames_processed = 0
self.running = True
# We close the open file handles, as only the paths are required.
for input_file in args.input:
input_file.close()
if not len(args.fourcc_str) == 4:
print("[DVR-Scan] Error: Specified codec (-c/--codec) must be exactly 4 characters.")
return
self.fourcc = cv2.VideoWriter_fourcc(*args.fourcc_str.upper())
if args.kernel_size == -1:
self.kernel = None
elif (args.kernel_size % 2) == 0:
print("[DVR-Scan] Error: Kernel size must be an odd, positive integer (e.g. 3, 5, 7.")
return
else:
self.kernel = np.ones((args.kernel_size, args.kernel_size), np.uint8)
self.fourcc = cv2.VideoWriter_fourcc(*args.fourcc_str.upper())
self.comp_file = None
self.scan_only_mode = args.scan_only_mode
if args.output:
Expand Down Expand Up @@ -135,9 +141,6 @@ def __init__(self, args, gui=None):
def _load_input_videos(self):
""" Opens and checks that all input video files are valid, can
be processed, and have the same resolution and framerate. """
self.video_resolution = None
self.video_fps = None
self.frames_total = 0
if not len(self.video_paths) > 0:
return False
for video_path in self.video_paths:
Expand Down Expand Up @@ -205,7 +208,7 @@ def _get_next_frame(self, retrieve=True):

return None

def _stampText(self, frame, text, line):
def _stamp_text(self, frame, text, line):
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
margin = 5
Expand All @@ -218,11 +221,10 @@ def _stampText(self, frame, text, line):
text_height = size[0][1]
line_height = text_height + size[1] + margin

x = margin
y = margin + size[0][1] + line * line_height
text_pos = (margin, margin + size[0][1] + line * line_height)
cv2.rectangle(frame, (margin, margin),
(margin + text_width, margin + text_height + 2), (0, 0, 0), -1)
cv2.putText(frame, text, (x, y), font, font_scale, color, thickness)
cv2.putText(frame, text, text_pos, font, font_scale, color, thickness)
return None

def scan_motion(self):
Expand Down Expand Up @@ -256,7 +258,6 @@ def scan_motion(self):
#curr_state = 'no_event' # 'no_event', 'in_event', or 'post_even
in_motion_event = False
self.frames_processed = 0
num_frames_processed = 0
processing_start = time.time()

# Seek to starting position if required.
Expand All @@ -272,7 +273,7 @@ def scan_motion(self):
print("[DVR-Scan] selecting area of interest:")
frame_for_crop = self._get_next_frame()
if self.draw_timecode:
self._stampText(frame_for_crop, curr_pos.get_timecode(), 0)
self._stamp_text(frame_for_crop, curr_pos.get_timecode(), 0)
self.roi = cv2.selectROI("DVR-Scan ROI Selection", frame_for_crop)
cv2.destroyAllWindows()
if all([coord == 0 for coord in self.roi]):
Expand Down Expand Up @@ -337,7 +338,7 @@ def scan_motion(self):
# the current scene's post-event counter.
if not self.scan_only_mode:
if self.draw_timecode:
self._stampText(frame_rgb_origin, curr_pos.get_timecode(), 0)
self._stamp_text(frame_rgb_origin, curr_pos.get_timecode(), 0)
video_writer.write(frame_rgb_origin)
if frame_score >= self.threshold:
num_frames_post_event = 0
Expand Down Expand Up @@ -373,13 +374,12 @@ def scan_motion(self):
self.video_resolution)
for frame in buffered_frames:
if self.draw_timecode:
self._stampText(frame, curr_pos.get_timecode(), 0)
self._stamp_text(frame, curr_pos.get_timecode(), 0)
video_writer.write(frame)
buffered_frames = []

curr_pos.frame_num += 1
self.frames_processed += 1
num_frames_processed += 1
if progress_bar:
progress_bar.update(1)

Expand All @@ -400,8 +400,8 @@ def scan_motion(self):
elif not self.suppress_output:
processing_time = time.time() - processing_start
processing_rate = float(self.frames_processed) / processing_time
print("[DVR-Scan] Processed %d / %d frames read in %3.1f secs (avg %3.1f FPS)." % (
num_frames_processed, self.frames_processed, processing_time, processing_rate))
print("[DVR-Scan] Processed %d frames read in %3.1f secs (avg %3.1f FPS)." % (
self.frames_processed, processing_time, processing_rate))
if not len(self.event_list) > 0:
print("[DVR-Scan] No motion events detected in input.")
return
Expand All @@ -427,4 +427,3 @@ def scan_motion(self):
print(','.join(timecode_list))
else:
print("[DVR-Scan] Motion events written to disk.")

9 changes: 8 additions & 1 deletion dvr_scan/timecode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#
# DVR-Scan: Find & Export Motion Events in Video Footage
# --------------------------------------------------------------
Expand Down Expand Up @@ -25,6 +26,13 @@
# OTHER DEALINGS IN THE SOFTWARE.
#

""" ``dvr_scan.timecode`` Module
This module provides the FrameTimecode class, used for representing
video timecodes that are frame-based.
TODO: Replace with PySceneDetect's FrameTimecode to reduce code duplication.
"""

class FrameTimecode(object):
""" Object for frame-based timecodes, using the video framerate
Expand Down Expand Up @@ -137,4 +145,3 @@ def get_timecode(self, precision = 3, use_rounding = True):
secs = '%02d' % int(round(secs, 0)) if use_rounding else '%02d' % int(secs)
# Return hours, minutes, and seconds as a formatted timecode string.
return '%02d:%02d:%s' % (hrs, mins, secs)

16 changes: 8 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# DVR-Scan: Video Motion Event Detection & Extraction Tool
# --------------------------------------------------------------
# [ Site: https://github.com/Breakthrough/DVR-Scan/ ]
# [ Documentation: http://dvr-scan.readthedocs.org/ ]
#
# Installation and build script for DVR-Scan. To install DVR-Scan in the
# current environment, run:
#
# > python setup.py install
#
# This will allow you to use the `dvr-scan` command from the terminal or
# command prompt. When upgrading to a new version, running the above command
# will automatically overwrite any existing older version.
# Copyright (C) 2014-2021 Brandon Castellano <http://www.bcastell.com>.
#

""" DVR-Scan setup.py
To install DVR-Scan:
python setup.py install
"""

import sys

Expand Down

0 comments on commit c588aa2

Please sign in to comment.