rewrited logic for pausing recognition
This commit is contained in:
parent
1d2865d361
commit
7e33098759
@ -189,11 +189,11 @@ def gen_frames_raw():
|
||||
continue
|
||||
|
||||
# OPTIONAL
|
||||
# rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
# face_locations = face_recognition.face_locations(rgb_frame)
|
||||
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
face_locations = face_recognition.face_locations(rgb_frame)
|
||||
|
||||
# for (top, right, bottom, left) in face_locations:
|
||||
# cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
|
||||
for (top, right, bottom, left) in face_locations:
|
||||
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
|
||||
|
||||
ret, buffer = cv2.imencode('.jpg', frame)
|
||||
if not ret:
|
||||
|
@ -17,36 +17,59 @@ if not cap.isOpened():
|
||||
|
||||
known_face_encodings, known_face_names = load_encodings()
|
||||
|
||||
def run_recognition():
|
||||
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
|
||||
import time
|
||||
|
||||
def run_recognition():
|
||||
global known_face_encodings, known_face_names
|
||||
grace_period = 2.0
|
||||
|
||||
while True:
|
||||
ret, frame = cap.read()
|
||||
if not ret:
|
||||
print("Error: Could not read frame.")
|
||||
continue
|
||||
|
||||
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):
|
||||
if name != "Unknown":
|
||||
print(f"Detected: {name}")
|
||||
log_recognition(name)
|
||||
subprocess.run(["python", "open_door.py"], check=True)
|
||||
last_trigger_time = time.time()
|
||||
flush_camera(cap, flush_time=2.0)
|
||||
triggered = True
|
||||
recognized_face_found = True
|
||||
break
|
||||
else:
|
||||
print("Detected: Unknown")
|
||||
if triggered:
|
||||
print("Cooldown activated. Waiting before next recognition...")
|
||||
time.sleep(1)
|
||||
cap.release()
|
||||
|
||||
if recognized_face_found:
|
||||
print("Checking for face departure with debounce...")
|
||||
last_seen_time = None
|
||||
|
||||
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__":
|
||||
run_recognition()
|
||||
|
Loading…
Reference in New Issue
Block a user