rewrited logic for pausing recognition

This commit is contained in:
Valér Jakubčo 2025-05-16 13:46:23 +01:00
parent 1d2865d361
commit 7e33098759
2 changed files with 44 additions and 21 deletions

View File

@ -189,11 +189,11 @@ def gen_frames_raw():
continue continue
# OPTIONAL # OPTIONAL
# rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
# face_locations = face_recognition.face_locations(rgb_frame) face_locations = face_recognition.face_locations(rgb_frame)
# for (top, right, bottom, left) in face_locations: for (top, right, bottom, left) in face_locations:
# cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
ret, buffer = cv2.imencode('.jpg', frame) ret, buffer = cv2.imencode('.jpg', frame)
if not ret: if not ret:

View File

@ -17,36 +17,59 @@ if not cap.isOpened():
known_face_encodings, known_face_names = load_encodings() known_face_encodings, known_face_names = load_encodings()
def run_recognition(): import time
global last_trigger_time, known_face_encodings, known_face_names
while True:
current_time = time.time()
if current_time - last_trigger_time < COOLDOWN:
time.sleep(0.5)
continue
def run_recognition():
global known_face_encodings, known_face_names
grace_period = 2.0
while True:
ret, frame = cap.read() ret, frame = cap.read()
if not ret: if not ret:
print("Error: Could not read frame.") print("Error: Could not read frame.")
continue continue
face_locations, face_names = process_frame(frame, known_face_encodings, known_face_names) face_locations, face_names = process_frame(frame, known_face_encodings, known_face_names)
triggered = False
recognized_face_found = False
for face, name in zip(face_locations, face_names): for face, name in zip(face_locations, face_names):
if name != "Unknown": if name != "Unknown":
print(f"Detected: {name}") print(f"Detected: {name}")
log_recognition(name) log_recognition(name)
subprocess.run(["python", "open_door.py"], check=True) subprocess.run(["python", "open_door.py"], check=True)
last_trigger_time = time.time() recognized_face_found = True
flush_camera(cap, flush_time=2.0)
triggered = True
break break
else:
print("Detected: Unknown") if recognized_face_found:
if triggered: print("Checking for face departure with debounce...")
print("Cooldown activated. Waiting before next recognition...") last_seen_time = None
time.sleep(1)
cap.release() while True:
ret, frame = cap.read()
if not ret:
continue
face_locations, face_names = process_frame(frame, known_face_encodings, known_face_names)
face_present = any(name != "Unknown" for name in face_names)
current_time = time.time()
if face_present:
print("Face still present, resetting debounce timer...")
last_seen_time = current_time
else:
if last_seen_time is None:
last_seen_time = current_time
print("Face not detected, starting debounce timer...")
elif current_time - last_seen_time >= grace_period:
print("Face gone for grace period, resuming recognition.")
break
else:
print(f"Face gone, waiting for debounce: {grace_period - (current_time - last_seen_time):.1f}s")
time.sleep(0.2)
else:
print("No known face detected.")
time.sleep(0.5)
if __name__ == "__main__": if __name__ == "__main__":
run_recognition() run_recognition()