Spaces:
Sleeping
Sleeping
| 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() |