def get_visitor_photos(self, visitors_info): for key, value in visitors_info.items(): image = requests.get(value['image'], allow_redirects=True) open(CONFIG["visitors"]["photos_target"] + key + ".jpg", "wb").write(image.content) log(f"Downloaded photo for visitor {key}") for image in os.listdir(CONFIG["visitors"]["photos_target"]): image_path = CONFIG["visitors"]["photos_target"] + image image_object = Image.open(image_path) try: for key, value in image_object._getexif().items(): if TAGS.get(key) == "Orientation": orientation = value if orientation == 3: image_object = image_object.rotate(180) elif orientation == 6: image_object = image_object.rotate(270) elif orientation == 8: image_object = image_object.rotate(90) except AttributeError: pass image_object.save(image_path)
def process_visitor_images(visitor_info): known_face_encodings = [] known_face_names = [] for key, value in visitor_info.items(): image = face_recognition.load_image_file( CONFIG["visitors"]["photos_target"] + key + ".jpg") face_encoding = face_recognition.face_encodings(image, num_jitters=100)[0] known_face_encodings.append(face_encoding) known_face_names.append(value['firstName'] + " " + value['lastName']) log(f"Visitor {key} processed and ready") return (known_face_encodings, known_face_names)
def handle_motion(firebase_connector, known_face_encodings, known_face_names): log("Motion event started") video_capture = cv2.VideoCapture(CONFIG["core"]["video_capture_url"]) log("Ready to grab frames") notification_response = firebase_connector.send_notification( "Bzz. Bzz. Motion was detected at your door.") log(f"Notification {notification_response} sent") face_locations = [] face_encodings = [] face_names = [] for i in range(0, 46): log(f"Checking frame {i} for faces") ret, frame = video_capture.read() if CONFIG["super-debug"]["copy_attempted_face_detection_frames"]: cv2.imwrite( CONFIG["super-debug"]["attempts_destination"] + datetime.now().strftime("%m-%d-%Y_%H_%M_%S_") + str(i) + ".jpg", frame) small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] face_locations = face_recognition.face_locations( rgb_small_frame) #, number_of_times_to_upsample=2) face_encodings = face_recognition.face_encodings( rgb_small_frame, face_locations) if len(face_encodings) != 0: log(f"Face detected in frame {i}") for face_encoding in face_encodings: # matches = face_recognition.compare_faces(known_face_encodings, # face_encoding) name = "An unknown visitor" face_distances = face_recognition.face_distance( known_face_encodings, face_encoding) log(f"Known face names: {str(known_face_names)}") log(f"Face distances: {str(face_distances)}") best_match_index = np.argmin(face_distances) if face_distances[best_match_index] <= 0.55: name = known_face_names[best_match_index] firebase_connector.add_visitor_log_entry(name) notification_response = firebase_connector.send_notification( name + " is at your door!") log(f"Notification {notification_response} sent") break video_capture.release() log("Destroyed frame capture object")
def main(): video_file = sys.argv[1] with Client(CONFIG["core"]["socket"], "AF_UNIX") as conn: conn.send("movie_end " + video_file) log("Sent movie_end " + video_file + " to server", client=True)
def main(): with Client(CONFIG["core"]["socket"], "AF_UNIX") as conn: conn.send("event_start") log("Sent event_start to server", client=True)
def main(): firebase_connector = FirebaseConnector() log("Firebase connection ready") visitor_info = firebase_connector.get_visitor_info() log("Got visitor info") if not CONFIG["super-debug"]["dont_redownload_visitor_photos"]: firebase_connector.get_visitor_photos(visitor_info) log("Finished getting visitor photos") else: log("Skipping getting visitor photos per config") if not CONFIG["super-debug"]["dont_reprocess_visitor_faces"]: known_face_encodings, known_face_names = process_visitor_images(visitor_info) else: log("Attempting to skip processing visitor faces per config") try: visitor_faces_pickle = open( CONFIG["super-debug"]["visitor_faces_pickle"], 'rb' ) saved_face_data = pickle.load(visitor_faces_pickle) known_face_encodings, known_face_names = saved_face_data log("Loaded saved visitor face data") except FileNotFoundError: log("Failed to load visitor face data, generating and saving it") known_face_encodings, known_face_names = process_visitor_images(visitor_info) visitor_faces_pickle = open( CONFIG["super-debug"]["visitor_faces_pickle"], 'wb' ) face_data = (known_face_encodings, known_face_names) pickle.dump(face_data, visitor_faces_pickle) try: os.unlink('/tmp/buzz_socket') except FileNotFoundError: pass while True: with Listener(CONFIG["core"]["socket"], "AF_UNIX") as listener: log("Ready to accept connections") with listener.accept() as conn: received_signal = conn.recv() if CONFIG["core"]["debug"]: log(f"Received signal: {received_signal}") if received_signal == "event_start": handle_motion(firebase_connector, known_face_encodings, known_face_names) elif received_signal == "event_end" and CONFIG["core"]["debug"]: notification_response = firebase_connector.send_notification( "DEBUG: Motion event ended.") log(f"Notification {notification_response} sent") elif "movie_end" in received_signal: log(f"Video file ended {received_signal.split(' ')[1]}") firebase_connector.upload_to_storage(received_signal.split(' ')[1]) log(f"Uploaded video file to Firebase Storage")