Actually (?) fixed notification spam!

This commit is contained in:
slashtechno 2023-10-05 20:00:12 -05:00
parent 9fb5818322
commit 2341f0005c
Signed by: slashtechno
GPG Key ID: 8EC1D9D9286C2B17
1 changed files with 38 additions and 25 deletions

View File

@ -58,8 +58,10 @@ def main():
stream_source = argparser.add_mutually_exclusive_group() stream_source = argparser.add_mutually_exclusive_group()
stream_source.add_argument( stream_source.add_argument(
'--url', "--url",
default=os.environ['URL'] if 'URL' in os.environ and os.environ['URL'] != '' else None, # noqa: E501 default=os.environ["URL"]
if "URL" in os.environ and os.environ["URL"] != ""
else None, # noqa: E501
type=str, type=str,
help="The URL of the stream to use", help="The URL of the stream to use",
) )
@ -148,36 +150,40 @@ def main():
# print("Probability:", conf) # print("Probability:", conf)
# print("---") # print("---")
# Now do stuff # Now do stuff (if conf > 0.5)
if conf < 0.5:
# If the confidence is less than 0.5, then SKIP!!!!
continue
# End goal: Send a notification when an object has been detected for 2 seconds in the past 15 seconds.
# However, don't send a notification if the last notification was less than 15 seconds ago
# If this is the first time the object has been detected
# or if it has been more than 15 seconds since the last detection
# reset the detection duration
if ( if (
# If the object has not been detected before
object_names[class_id]["last_detection_time"] is None object_names[class_id]["last_detection_time"] is None
# If the last detection was more than 15 seconds ago
or time.time() - object_names[class_id]["last_detection_time"] > 15 or time.time() - object_names[class_id]["last_detection_time"] > 15
or object_names[class_id]["detection_duration"] is None
): ):
print(f"First detection of {class_id} in session")
# time.time() returns the number of seconds since the epoch
object_names[class_id]["last_detection_time"] = time.time() object_names[class_id]["last_detection_time"] = time.time()
# object_names[class_id]["first_detection_time"] = time.time()
object_names[class_id]["detection_duration"] = 0 object_names[class_id]["detection_duration"] = 0
headers = notify.construct_ntfy_headers( print(f"First detection of {class_id} in this detection window")
title=f"{class_id} Detected",
tag="rotating_light",
priority="default",
)
notify.send_notification(
data=f"{class_id} Detected", headers=headers, url=args.ntfy_url
)
else: else:
# Add the time since the last detection to the total detection duration # Check if the last detection was under 15 seconds ago
# If it was, leave the detection duration as is
if (
time.time() - object_names[class_id]["last_detection_time"]
<= 15
):
pass
# If it was more than 15 seconds ago, reset the detection duration
# This effectively resets the cycle
else:
object_names[class_id]["detection_duration"] = 0
object_names[class_id]["detection_duration"] += ( object_names[class_id]["detection_duration"] += (
time.time() - object_names[class_id]["last_detection_time"] time.time() - object_names[class_id]["last_detection_time"]
) )
object_names[class_id]["last_detection_time"] = time.time()
# Check if detection has been ongoing for 2 seconds or more in the past 15 seconds # Check if detection has been ongoing for 2 seconds or more in the past 15 seconds
if ( if (
@ -186,7 +192,12 @@ def main():
<= 15 <= 15
): ):
# If the last notification was more than 15 seconds ago, then send a notification # If the last notification was more than 15 seconds ago, then send a notification
if object_names[class_id]["last_notification_time"] is None or time.time() - object_names[class_id]["last_notification_time"] > 15: if (
object_names[class_id]["last_notification_time"] is None
or time.time()
- object_names[class_id]["last_notification_time"]
> 15
):
object_names[class_id]["last_notification_time"] = time.time() object_names[class_id]["last_notification_time"] = time.time()
print(f"Detected {class_id} for 2 seconds") print(f"Detected {class_id} for 2 seconds")
headers = notify.construct_ntfy_headers( headers = notify.construct_ntfy_headers(
@ -195,7 +206,9 @@ def main():
priority="default", priority="default",
) )
notify.send_notification( notify.send_notification(
data=f"{class_id} Detected", headers=headers, url=args.ntfy_url data=f"{class_id} Detected",
headers=headers,
url=args.ntfy_url,
) )
# Reset the detection duration # Reset the detection duration
object_names[class_id]["detection_duration"] = 0 object_names[class_id]["detection_duration"] = 0