File size: 5,011 Bytes
0a1fb69
e7f0a52
 
149db20
 
 
 
e7f0a52
4809422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7f0a52
149db20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55f4e33
4809422
 
 
 
 
b44bdba
4809422
 
 
 
 
 
e7f0a52
4809422
 
 
 
95981ec
 
4809422
 
 
 
 
 
 
 
95981ec
4809422
 
 
 
b44bdba
4809422
 
 
 
149db20
4809422
149db20
 
 
 
 
 
 
e7f0a52
 
4809422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import gradio as gr
import numpy as np
import time
import threading

# Global flag to control detection
detection_active = False

def process_video_frame(frame):
    """
    Placeholder function for processing each video frame
    Replace this with your actual face spoofing detection logic
    """
    if frame is None:
        return "No frame received", "", ""
    
    # Simulate processing time (remove this in production)
    time.sleep(0.1)
    
    # Placeholder result - replace with your actual detection
    is_real = np.random.choice([True, False], p=[0.7, 0.3])
    confidence = np.random.uniform(0.8, 1.0)
    
    result = "Real Face" if is_real else "Spoof Detected"
    status = "Processing live feed..."
    conf_text = f"{confidence:.2%}"
    
    return status, result, conf_text

def start_detection():
    """
    Start the detection process
    """
    global detection_active
    detection_active = True
    return "Processing live feed..."

def stop_detection():
    """
    Stop the detection process
    """
    global detection_active
    detection_active = False
    return "Detection stopped"

def process_frames(video_feed, status_text, result_text, confidence_text):
    """
    Continuously process frames when detection is active
    """
    while detection_active:
        if video_feed is not None:
            status, result, conf = process_video_frame(video_feed)
            status_text.update(value=status)
            result_text.update(value=result)
            confidence_text.update(value=conf)
        time.sleep(0.1)  # Adjust the sleep time as needed

with gr.Blocks() as demo:
    gr.Markdown("# Real-Time Face Spoofing Detection")
    
    with gr.Row():
        with gr.Column(scale=2):
            # Main video feed
            video_feed = gr.Image(label="Live Camera Feed", streaming=True)
        
        with gr.Column(scale=1):
            # Status and results
            status_text = gr.Textbox(label="Status", value="Waiting for camera...")
            result_text = gr.Textbox(label="Detection Result")
            confidence_text = gr.Textbox(label="Confidence Score")
            
            # Control buttons
            start_button = gr.Button("Start Detection", variant="primary")
            stop_button = gr.Button("Stop", variant="secondary")
    
    gr.Markdown("""
    ### Instructions:
    1. Allow camera access when prompted
    2. Click 'Start Detection' to begin real-time analysis
    3. Click 'Stop' to pause the detection
    
    ### Note:
    - Keep your face centered and well-lit
    - Maintain a stable position for better results
    - Detection results update in real-time
    """)
    
    # Event handlers
    start_button.click(
        fn=start_detection,
        outputs=status_text
    )
    
    stop_button.click(
        fn=stop_detection,
        outputs=status_text
    )
    
    # Start a thread to process frames when detection is active
    threading.Thread(
        target=process_frames,
        args=(video_feed, status_text, result_text, confidence_text),
        daemon=True
    ).start()

if __name__ == "__main__":
    demo.launch()

# import gradio as gr
# import numpy as np
# import time

# def process_image(img):
#     """Placeholder function - replace with your backend integration"""
#     if img is None:
#         return "No image provided", "", ""
#     time.sleep(1)  # Simulate processing
#     return "Processing Complete", "Real Face", "Confidence: 95%"

# with gr.Blocks() as demo:
#     gr.Markdown("# Face Spoofing Detection System")
    
#     with gr.Tabs():
#         with gr.Tab("Webcam Detection"):
#             webcam = gr.Image(label="Webcam Feed")
#             webcam_status = gr.Textbox(label="Status", value="Ready")
#             webcam_result = gr.Textbox(label="Detection Result")
#             webcam_conf = gr.Textbox(label="Confidence Score")
#             webcam_button = gr.Button("Analyze")
            
#             webcam_button.click(
#                 fn=process_image,
#                 inputs=webcam,
#                 outputs=[webcam_status, webcam_result, webcam_conf]
#             )

#         with gr.Tab("Image Upload"):
#             image_input = gr.Image(label="Upload Image")
#             image_status = gr.Textbox(label="Status", value="Ready")
#             image_result = gr.Textbox(label="Detection Result")
#             image_conf = gr.Textbox(label="Confidence Score")
#             image_button = gr.Button("Analyze")
            
#             image_button.click(
#                 fn=process_image,
#                 inputs=image_input,
#                 outputs=[image_status, image_result, image_conf]
#             )

#     gr.Markdown("""
#     ### Instructions:
#     1. Choose either Webcam or Image Upload tab
#     2. For webcam: Allow camera access and take a photo
#     3. For images: Upload an image from your device
#     4. Click Analyze to process the image
#     """)

# if __name__ == "__main__":
#     demo.launch()