From 2a809836c0ae104c9c8ad6418c018cc7d1ea9ffe Mon Sep 17 00:00:00 2001 From: slashtechno <77907286+slashtechno@users.noreply.github.com> Date: Sun, 18 Dec 2022 13:07:11 -0600 Subject: [PATCH] Added support for `ntfy.sh` --- environment.yml | Bin 4684 -> 5060 bytes main.py | 148 ++++++++++++++++++++++-------------------------- 2 files changed, 68 insertions(+), 80 deletions(-) diff --git a/environment.yml b/environment.yml index c8725cc9fb9be14fea7fb56eb2a6f00ed17cc066..207f2c7fab813da9cfc184743ae40e867ee52440 100644 GIT binary patch delta 331 zcmZ`!I|{-;5PhvenjAnZ1hI%{A|irS&LB41Y(A1evWbZivGEMzMLdM1hw|+dB7zLF zyEFUVn>Vk~bNm>%yBHZX8nj4pga?C)Rmom~92a&QY#2VKm`gh7WB3&w-#HR8wIBnc z7WbYTUVl*y)eh)*ww%>|bbYM;DtDY;h?fX~Fsut7OJM}CZfasu?CIi+HQj_f6}E{i zWmi*(2*@a-Nt;P1_G2zvk*?UU85UVm#xU)l;aZww6)S7Rjxn^Z datetime.timedelta(seconds=5): - print(f"{name} has been seen") - # Update the last seen time - config["faces"][name]["last_seen"] = datetime.datetime.now().strftime( - DATETIME_FORMAT - ) + # Resize frame of video to a smaller size for faster face recognition processing + run_frame = cv2.resize(frame, (0, 0), fx=RUN_SCALE, fy=RUN_SCALE) + view_frame = cv2.resize(frame, (0, 0), fx=VIEW_SCALE, fy=VIEW_SCALE) + # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) + rgb_run_frame = run_frame[:, :, ::-1] + # Find all the faces and face encodings in the current frame of video + # model cnn is gpu accelerated, but hog is cpu only + face_locations = face_recognition.face_locations(rgb_run_frame, model="hog") # This crashes the program without output on my laptop when it's running without Docker compose + face_encodings = face_recognition.face_encodings(rgb_run_frame, face_locations) + face_names = [] + for face_encoding in face_encodings: + # See if the face is a match for the known face(s) + matches = face_recognition.compare_faces(known_face_encodings, face_encoding) + name = "Unknown" + # Or instead, use the known face with the smallest distance to the new face + face_distances = face_recognition.face_distance( + known_face_encodings, face_encoding + ) + best_match_index = np.argmin(face_distances) + if matches[best_match_index]: + name = known_face_names[best_match_index] + last_seen = config["faces"][name]["last_seen"] + # If it's never been seen, set the last seen time to x+5 seconds ago so it will be seen + # Kind of a hacky way to do it, but it works... hopefully + if last_seen == "": + print(f"{name} has been seen") + config["faces"][name]["last_seen"] = ( + datetime.datetime.now() - datetime.timedelta(seconds=15) + ).strftime(DATETIME_FORMAT) write_config() - face_names.append(name) - - process_this_frame = not process_this_frame - + # Check if the face has been seen in the last 5 seconds + if datetime.datetime.now() - datetime.datetime.strptime( + last_seen, DATETIME_FORMAT + ) > datetime.timedelta(seconds=10): + print(f"{name} has been seen") + # Update the last seen time + config["faces"][name]["last_seen"] = datetime.datetime.now().strftime( + DATETIME_FORMAT + ) + # Send a notification + print(f"Sending notification to{NTFY_URL}") + requests.post( + NTFY_URL, + data=f'"{name}" has been seen', + headers={ + "Title": "Face Detected", + "Priority": "urgent", + "Tags": "neutral_face", + }, + ) + print("Writing config...") + write_config() + face_names.append(name) # Display the results # Iterate over each face found in the frame to draw a box around it # Zip is used to iterate over two lists at the same time for (top, right, bottom, left), name in zip(face_locations, face_names): + print(f"Face found at {top}, {right}, {bottom}, {left} with name {name}") # Scale back up face locations since the frame we detected in was scaled to 1/4 size top = int(top * (VIEW_SCALE / RUN_SCALE)) right = int(right * (VIEW_SCALE / RUN_SCALE)) @@ -215,5 +202,6 @@ while True: break # Release handle to the webcam +print("Releasing video capture") video_capture.release() cv2.destroyAllWindows()