commit bc6f916928f1c985b579a3521cc31371e1b6922d parent 62ed50730a83ea79c2c1315b467b15e4c999e903 Author: dwrz <dwrz@dwrz.net> Date: Thu, 27 Oct 2022 20:06:55 +0000 Add notify script Diffstat:
A | scripts/notify | | | 76 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 76 insertions(+), 0 deletions(-)
diff --git a/scripts/notify b/scripts/notify @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +# Backblaze B2 Bucket +readonly BUCKET="" + +# COCO Labels +readonly LABEL_PERSON=0 +readonly LABEL_CAT=15 + +# yolov7 working directory. +readonly PROJECT="/tmp/yolov7" + +# Notification recipient. +readonly RECIPIENT="" + +detect_objects() { + local filepath="$1" + + python "${HOME}/yolov7/detect.py" \ + --exist-ok \ + --no-trace \ + --save-txt \ + --project "${PROJECT}" \ + --name "motion" \ + --weights "${HOME}/yolov7/yolov7-tiny.pt" \ + --source "${filepath}" +} + +notify() { + local name="$1" + + echo "${HOSTNAME} at $(date '+%Y-%m-%dT%H:%M:%S%:z')" | \ + mutt -a "${PROJECT}/motion/${name}.jpg" \ + -s "${HOSTNAME}: Motion Detected" \ + -- "${RECIPIENT}" +} + +upload() { + local name="$1" + + backblaze-b2 upload-file \ + --threads 2 \ + "dwrz-motion" \ + "/${PROJECT}/motion/${name}.jpg" \ + "${HOSTNAME}/${name}.jpg" +} + +delete_outdated() { + local filepath="$1" + + rm -f "$1" + rm -f "/${PROJECT}/motion/${name}.jpg" + find "${HOME}"/photo/ -mmin +5 -delete + find "${HOME}"/timelapse/ -mmin +60 -delete + find "${PROJECT}/motion/" -iname "*.jpg" -mmin +5 -delete + find "${PROJECT}/motion/labels/" -mmin +5 -delete +} + +main() { + local filepath + local name + + detect_objects "${filepath}" + + # Send a notification if we match any labels. + labels="$(awk '{print $1}' "${PROJECT}/motion/labels/${name}.txt")" + if echo "${labels}" | grep -qw "${LABEL_PERSON}\|${LABEL_CAT}"; then + notify "${name}" + fi + + upload "${name}" + + delete_outdated "${filepath}" "${name}" +} + +main "$@"