Don't spam notifications

This commit is contained in:
slashtechno 2023-10-05 19:12:42 -05:00
parent ea4009c898
commit 9fb5818322
Signed by: slashtechno
GPG Key ID: 8EC1D9D9286C2B17
1 changed files with 18 additions and 12 deletions

View File

@ -132,6 +132,7 @@ def main():
"last_detection_time": None, "last_detection_time": None,
"detection_duration": None, "detection_duration": None,
# "first_detection_time": None, # "first_detection_time": None,
"last_notification_time": None,
} }
for box in r.boxes: for box in r.boxes:
# Get the name of the object # Get the name of the object
@ -158,7 +159,7 @@ def main():
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 or object_names[class_id]["detection_duration"] is None
): ):
print(f"First detection of {class_id}") print(f"First detection of {class_id} in session")
# time.time() returns the number of seconds since the epoch # 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]["first_detection_time"] = time.time()
@ -184,15 +185,20 @@ def main():
and time.time() - object_names[class_id]["last_detection_time"] and time.time() - object_names[class_id]["last_detection_time"]
<= 15 <= 15
): ):
print(f"Detected {class_id} for 2 seconds") # If the last notification was more than 15 seconds ago, then send a notification
headers = notify.construct_ntfy_headers( if object_names[class_id]["last_notification_time"] is None or time.time() - object_names[class_id]["last_notification_time"] > 15:
title=f"{class_id} Detected", object_names[class_id]["last_notification_time"] = time.time()
tag="rotating_light", print(f"Detected {class_id} for 2 seconds")
priority="default", headers = notify.construct_ntfy_headers(
) title=f"{class_id} Detected",
notify.send_notification( tag="rotating_light",
data=f"{class_id} Detected", headers=headers, url=args.ntfy_url priority="default",
) )
notify.send_notification(
data=f"{class_id} Detected", headers=headers, url=args.ntfy_url
)
# Reset the detection duration
object_names[class_id]["detection_duration"] = 0
im_array = r.plot() im_array = r.plot()
# Scale back up the coordinates of the locations of detected objects. # Scale back up the coordinates of the locations of detected objects.
# im_array = np.multiply(im_array, 1/args.run_scale) # im_array = np.multiply(im_array, 1/args.run_scale)