From 28150dd2c4d03ed844e8d1a5cfc463f5d1f9b45f Mon Sep 17 00:00:00 2001 From: Franoosh Date: Tue, 26 May 2026 16:32:13 +0200 Subject: client.cfg : move log dir to /var/log client.py : use ComputingContoursException and DetectMovementException helpers.py : define ComputingContoursException and DetectMovementException clients.html : small changes webserver.py : minor changes worker.py : log disconnectd from web client on debug, not info --- client.cfg | 2 +- client.py | 17 ++++++++--------- helpers.py | 14 +++++++++----- templates/client.html | 34 ++++++++++++++++++++++------------ webserver.py | 13 ++++++++++--- worker.py | 2 +- 6 files changed, 51 insertions(+), 31 deletions(-) diff --git a/client.cfg b/client.cfg index c407c4e..d86ba77 100644 --- a/client.cfg +++ b/client.cfg @@ -6,7 +6,7 @@ router_address = 127.0.0.1 router_port = 5569 [logging] -logdir = logs +logdir = /var/log/zmq_client logfile = client.log loglevel = INFO diff --git a/client.py b/client.py index 81c8f8b..7b7a697 100644 --- a/client.py +++ b/client.py @@ -35,6 +35,8 @@ from helpers import ( bytes_to_timestamp, write_yappi_stats, auth_service, + ComputingContoursException, + DetectMovementException, ) ################################################### @@ -187,8 +189,7 @@ class ClientVideo(Thread): List of contours to draw on a frame """ - raw_frame = self.frame_deque[-1][2] - scaling_factor = self.frame_deque[-1][1] + _, scaling_factor, raw_frame = self.frame_deque[-1] scaled_contours = scale_contours(contours, scaling_factor) try: frame_out = draw_contours( @@ -262,15 +263,13 @@ class ClientVideo(Thread): sample_frames = self.frame_deque[0][0], self.frame_deque[-1][0] try: contours = compute_contours(sample_frames) - except Exception as exc: - logger.error("ClientVideo %r: Error computing contours: %r", self.identity, exc) - continue - try: movement_now = detect_movement(contours, min_area=self.device_threshold) - except Exception as exc: - logger.error("ClientVideo %r: Error detecting movement: %r", self.identity, exc) + except ComputingContoursException: + logger.error("ClientVideo %r: Error computing contours", self.identity) + continue + except DetectMovementException: + logger.error("ClientVideo %r: Error detecting movement", self.identity) continue - # Only update movement start time and send start message # if movement is detected and was not detected before: if movement_now: diff --git a/helpers.py b/helpers.py index 8a47415..1ea057b 100644 --- a/helpers.py +++ b/helpers.py @@ -96,15 +96,13 @@ def bytes_to_timestamp(byte_data): def compute_contours(sample_frames): """Compute contours between two frames""" - all_contours = [] frame_0, frame_1 = sample_frames frame_delta = cv2.absdiff(frame_0, frame_1) threshold = cv2.threshold(frame_delta, 25, 255, cv2.THRESH_BINARY)[1] threshold = cv2.dilate(threshold, None, iterations=2) - contours, _ = cv2.findContours(threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - all_contours.extend(contours) + contours, _ = cv2.findContours(threshold.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) - return all_contours + return contours def scale_contours(contours, scaling_factor): @@ -123,7 +121,7 @@ def scale_contours(contours, scaling_factor): def draw_contours(frame, contours, min_contour_area=500): """Draw contours on the frame.""" for contour in contours: - if cv2.contourArea(contour) > min_contour_area: + if cv2.contourArea(contour) >= min_contour_area: (x, y, w, h) = cv2.boundingRect(contour) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) @@ -272,3 +270,9 @@ def auth_service(context, cert_dir): auth.start() auth.configure_curve(location=cert_dir) zmq.auth.load_certificates(cert_dir) + +class ComputingContoursException(Exception): + pass + +class DetectMovementException(Exception): + pass \ No newline at end of file diff --git a/templates/client.html b/templates/client.html index 785fbcf..09365df 100644 --- a/templates/client.html +++ b/templates/client.html @@ -3,6 +3,7 @@ Client {{ client_id }} - Camera Streams +