Skip to content

Commit d5c83f9

Browse files
authored
Merge pull request #58 from GautamMg/enhancement/time-gap-handler
Enhancement/time gap handler
2 parents 7486f11 + c2a3c53 commit d5c83f9

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

external_plugins/image_detecting_plugin/docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ services:
3030
privileged: true
3131
volumes:
3232
- ./image_data:/var/lib/motion
33+
environment:
34+
- MIN_SECONDS_BETWEEN_IMAGES=2
3335
depends_on:
3436
- engine

external_plugins/image_detecting_plugin/image_detecting_plugin.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# By default, we set this directory to `/var/lib/motion` in the container, assuming
1717
# that the Linux Motion package will also be configured and running in the same container.
1818
DATA_MONITORING_PATH = os.environ.get("DATA_MONITORING_PATH", "/var/lib/motion")
19-
19+
MIN_SECONDS_BETWEEN_IMAGES = float(os.environ.get("MIN_SECONDS_BETWEEN_IMAGES", "2.0"))
2020

2121
def get_socket():
2222
"""
@@ -80,6 +80,35 @@ class NewFileHandler(FileSystemEventHandler):
8080
For now, we are only interested in *new* files, hence, we implement
8181
on_create.
8282
"""
83+
84+
def __init__(self):
85+
super().__init__()
86+
self.last_image_time = 0
87+
88+
def extract_timestamp(self, file_path):
89+
basename = os.path.basename(file_path)
90+
try:
91+
# Expected format: 20250714-21:51:49-00.jpg
92+
if "-" in basename and ":" in basename:
93+
# Splitting logic
94+
parts = basename.split("-")
95+
if len(parts) >= 2 and ":" in parts[1]:
96+
date_part = parts[0]
97+
time_part = parts[1].replace(":", "")
98+
datetime_str = date_part + time_part
99+
if len(datetime_str) == 14:
100+
ts = time.strptime(datetime_str, "%Y%m%d%H%M%S")
101+
return time.mktime(ts)
102+
except Exception as e:
103+
logging.warning(f"Filename parsing failed: {basename}{e}")
104+
105+
# Fallback
106+
try:
107+
return os.path.getmtime(file_path)
108+
except Exception as e:
109+
logging.warning(f"Fallback to time.time(): {file_path}{e}")
110+
return time.time()
111+
83112
def on_closed(self, event):
84113
"""
85114
Watch the directory for new files (not directories), and trigger the
@@ -95,9 +124,17 @@ def process_file(self, file_path):
95124
Basic processing of a new file event.
96125
"""
97126
try:
127+
current_time = self.extract_timestamp(file_path)
128+
if current_time - self.last_image_time < MIN_SECONDS_BETWEEN_IMAGES:
129+
logging.info(f"Skipping image (too soon): {file_path}")
130+
os.remove(file_path)
131+
return
132+
98133
logging.debug(f"Processing file at path: {file_path}")
99134
uuid = generate_new_image_event(file_path)
100-
logging.info(f"Generated uuid ({uuid}) and successfully sent new image event for file: {file_path}")
135+
if uuid:
136+
self.last_image_time = current_time
137+
logging.info(f"Generated uuid ({uuid}) and successfully sent new image event for file: {file_path}")
101138
except Exception as e:
102139
logging.error(f"Error processing {file_path}: {e}")
103140

installer/defaults.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ image_detecting_plugin_image: tapis/image_detecting_plugin
2626
run_image_detecting_privileged: true
2727
motion_video_device: /dev/video0
2828
motion_framerate: 1
29-
motion_minimum_frame_time: 3
29+
motion_minimum_frame_time: 0
30+
motion_minimum_motion_frames: 1
3031
motion_event_gap: 1
31-
motion_threshold: 1500
32+
motion_threshold: 100
3233
motion_width: 640
3334
motion_height: 480
3435

0 commit comments

Comments
 (0)