End or pause the facial recognise process with OpenCV

I have been trying to write a python script with the following process:

  1. Show first frame of a sprite sheet (successful)
  2. Delay face detection for 10 seconds (successful)
  3. If face is recognised as being part of the trained model, then show a short animation according to who is detected (successful)
  4. Return to default animation (un-successfull)
  5. Pause the facial recognition for a set time period (eg 20 minutes) (un-successfull)

What seems to be happening is, when a recognised face is detected, the usb camera keeps on detected causing the personalized animation to loop. Only way to stop it is to cover the webcam.

I would like to see the personalized animation play out once and return to the default animation. Perhaps I am taking the wrong approach, so any suggestions would be welcome.

import cv2
import face_recognition
import numpy as np
import pygame
import sys
import time
import random

def load_sprites(sprite_sheet_path, frame_width, frame_height):
sprite_sheet = pygame.image.load(sprite_sheet_path)
sprite_sheet_width, sprite_sheet_height = sprite_sheet.get_size()
sprites =

for i in range(0, sprite_sheet_width, frame_width):
    sprite = sprite_sheet.subsurface((i, 0, frame_width, frame_height))
    sprites.append(sprite)

return sprites

def main():
# Initialize Pygamez
pygame.init()
pygame.display.set_caption(“Facial Recognition Animation”)
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)

# Load sprite sheets
sprite_sheet_0 = load_sprites("foxsprite0.bmp", 592, 504)
sprite_sheet_1 = load_sprites("foxsprite1.bmp", 592, 504)
sprite_sheet_2 = load_sprites("foxsprite2.bmp", 592, 504)

# Animation variables
animation_delay = 100  # milliseconds
current_time = pygame.time.get_ticks()
frame_index = 0
animation_running = False
current_sprite_sheet = sprite_sheet_0

# Load known face encodings and names
data = np.load("encodings.npz")
known_encodings = data['encodings']
known_names = data['names']

# Start the webcam
cap = cv2.VideoCapture(0)

# Flags for controlling face recognition
recognition_active = False
last_recognition_time = 0
recognition_pause_duration = 600  # seconds

while True:
    ret, frame = cap.read()
    if not ret:
        break

    # Display animation
    if animation_running:
        if pygame.time.get_ticks() - current_time >= animation_delay:
            current_time = pygame.time.get_ticks()
            frame_index += 1
            if frame_index >= len(current_sprite_sheet):
                frame_index = 0
                animation_running = False
                current_sprite_sheet = sprite_sheet_0

            screen.fill((0, 0, 0))
            screen.blit(current_sprite_sheet[frame_index], (0, 0))
            pygame.display.flip()
            pygame.time.delay(animation_delay)

    else:
        # Check if enough time has passed to start facial recognition
        if pygame.time.get_ticks() - current_time >= random.randint(10000, 19000):
            # Reset animation variables
            frame_index = 0
            # Start facial recognition
            recognition_active = True
            start_recognition_time = pygame.time.get_ticks()

    if recognition_active:
        # Check if enough time has passed since last recognition
        if pygame.time.get_ticks() - last_recognition_time >= recognition_pause_duration * 1000:
            recognition_active = False
            current_time = pygame.time.get_ticks()  # Reset time for next recognition

        # Resize frame for faster processing
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # Convert the image from BGR color (OpenCV) to RGB color (face_recognition)
        rgb_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB)

        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_frame)
        face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

        # Initialize name variable
        name = "Unknown"

        for face_encoding in face_encodings:
            # Compare face encoding with known face encodings
            matches = face_recognition.compare_faces(known_encodings, face_encoding)
            if True in matches:
                # Get the name of the matched face
                name = known_names[matches.index(True)]
                last_recognition_time = pygame.time.get_ticks()

        # Check if recognized face is name1 or name2
        if name == "name1":
            current_sprite_sheet = sprite_sheet_1
            animation_running = True
        elif name == "name2":
            current_sprite_sheet = sprite_sheet_2
            animation_running = True


    # Display default sprite
    screen.fill((0, 0, 0))
    screen.blit(current_sprite_sheet[0], (0, 0))
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            cap.release()
            cv2.destroyAllWindows()
            sys.exit()

if name == “main”:
main()