import gradio as gr
import cv2
from anti_spoofing import AntiSpoofingSystem

# Initialize the anti-spoofing system
anti_spoofing_system = AntiSpoofingSystem()

def process_frame(image, student_id, student_name):
    # Set student details in the anti-spoofing system
    anti_spoofing_system.student_id = student_id
    anti_spoofing_system.student_name = student_name

    # Convert the Gradio image format to OpenCV format
    frame = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    
    # Process the frame using the anti-spoofing system
    processed_frame = frame.copy()  # If needed, adapt the anti-spoofing system to take individual frames
    anti_spoofing_system.detect_smartphone(processed_frame)
    
    # Display a message if a smartphone is detected or blinks are counted
    message = "Mobile phone detected, can't record attendance" if anti_spoofing_system.smartphone_detected else "Processing attendance..."
    
    return cv2.cvtColor(processed_frame, cv2.COLOR_BGR2RGB), message

iface = gr.Interface(
    fn=process_frame,
    inputs=[
        gr.Image(source="webcam", tool=None, streaming=True, label="Webcam Feed"),
        gr.Textbox(label="Student ID"), 
        gr.Textbox(label="Student Name")
    ],
    outputs=[
        gr.Image(label="Processed Frame"),
        gr.Textbox(label="Status Message")
    ]
)

iface.launch()