Skip to content

Commit e5b5404

Browse files
committed
Implement caching of the GPS data to reduce latency
It appears that the GPS can take up to 1 second to update results. This is way too much latency when trying to capture fast-moving objects like birds.
1 parent 2ed62c4 commit e5b5404

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

README.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ just --list
297297
Can't cache the time and date from the GPS then.
298298
Use TTLCache from https://github.com/tkem/cachetools/[cachetools].
299299
- [] Add support for a TOML config file with https://github.com/bw2/ConfigArgParse[ConfigArgParse].
300+
- [] async
300301
- [] Switch from picamera2 to gstreamer to work with more hardware.
301302
- [] mypy
302303
- [] Create a weatherproof enclosure for the camera.

detectionator.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22
import argparse
3+
from cachetools import cached, TTLCache
34
from dateutil import parser
45
from functools import partial
56
import logging
@@ -114,6 +115,9 @@ def scale(coord, scaler_crop_maximum, lores):
114115

115116

116117
# Retrieve and format the data from the GPS for EXIF.
118+
# Only update the GPS data every 5 minutes to reduce latency.
119+
# Retrieving data from the GPS can take up to one second, which is too long.
120+
@cached(cache=TTLCache(maxsize=1, ttl=300))
117121
def get_gps_exif_metadata(session: gps.gps) -> dict:
118122
while True:
119123
if session.read() != 0:
@@ -235,17 +239,17 @@ def main():
235239
)
236240
args = parser.parse_args()
237241

242+
numeric_log_level = getattr(logging, args.log_level.upper(), None)
243+
if not isinstance(numeric_log_level, int):
244+
raise ValueError(f"Invalid log level: {args.log_level}")
245+
logging.basicConfig(level=numeric_log_level)
246+
238247
if args.burst < 1:
239248
logging.warn(
240249
f"The burst value must be at least 1. Ignoring the provided burst value of '{args.burst}'."
241250
)
242251
args.burst = 1
243252

244-
numeric_log_level = getattr(logging, args.log_level.upper(), None)
245-
if not isinstance(numeric_log_level, int):
246-
raise ValueError(f"Invalid log level: {args.log_level}")
247-
logging.basicConfig(level=numeric_log_level)
248-
249253
output_directory = os.path.join(os.getenv("HOME"), "Pictures")
250254
if args.output:
251255
output_directory = args.output
@@ -397,6 +401,9 @@ def capture_sample_signal_handler(_sig, _frame):
397401
image = picam2.capture_array("lores")
398402
matches = inference_tensorflow(image, args.model, labels, match)
399403
if len(matches) == 0:
404+
# Retrieve the GPS data to ensure the cache is up-to-date in order to reduce latency when there is a detection.
405+
# todo Update the GPS data asynchronously to allow the detection process to continue uninterrupted instead of blocking when there is a cache miss.
406+
get_gps_exif_metadata(gps_session)
400407
# Take a quick breather to give the CPU a break.
401408
# todo Increase / decrease this wait based on recent detections.
402409
time.sleep(0.2)

requirements-dev.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pip-tools
33
pre-commit
44
pytest
55
ruff
6+
types-cachetools
67
yamllint

requirements-dev.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ ruff==0.4.3 \
163163
--hash=sha256:eeb039f8428fcb6725bb63cbae92ad67b0559e68b5d80f840f11914afd8ddf7f \
164164
--hash=sha256:ff0a3ef2e3c4b6d133fbedcf9586abfbe38d076041f2dc18ffb2c7e0485d5a07
165165
# via -r requirements-dev.in
166+
types-cachetools==5.3.0.7 \
167+
--hash=sha256:27c982cdb9cf3fead8b0089ee6b895715ecc99dac90ec29e2cab56eb1aaf4199 \
168+
--hash=sha256:2c6d19900131b36d4fd5d7b316fd060870145153ead45e29dafbe10538533008 \
169+
--hash=sha256:98c069dc7fc087b1b061703369c80751b0a0fc561f6fb072b554e5eee23773a0
170+
# via -r requirements-dev.in
166171
virtualenv==20.26.1 \
167172
--hash=sha256:604bfdceaeece392802e6ae48e69cec49168b9c5f4a44e483963f9242eb0e78b \
168173
--hash=sha256:71c11a0f2ec69d818248974a7f948ee69655547ddc2148f45dd204e73eaaed04 \

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cachetools
12
numpy
23
opencv-python
34
piexif

0 commit comments

Comments
 (0)