Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,50 +2,139 @@ import gradio as gr
|
|
| 2 |
import numpy as np
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
with gr.Blocks() as demo:
|
| 13 |
-
gr.Markdown("# Face Spoofing Detection
|
| 14 |
-
|
| 15 |
-
with gr.
|
| 16 |
-
with gr.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
inputs=webcam,
|
| 26 |
-
outputs=[webcam_status, webcam_result, webcam_conf]
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
with gr.Tab("Image Upload"):
|
| 30 |
-
image_input = gr.Image(label="Upload Image")
|
| 31 |
-
image_status = gr.Textbox(label="Status", value="Ready")
|
| 32 |
-
image_result = gr.Textbox(label="Detection Result")
|
| 33 |
-
image_conf = gr.Textbox(label="Confidence Score")
|
| 34 |
-
image_button = gr.Button("Analyze")
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
gr.Markdown("""
|
| 43 |
### Instructions:
|
| 44 |
-
1.
|
| 45 |
-
2.
|
| 46 |
-
3.
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
if __name__ == "__main__":
|
| 51 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import time
|
| 4 |
|
| 5 |
+
def process_video_frame(frame):
|
| 6 |
+
"""
|
| 7 |
+
Placeholder function for processing each video frame
|
| 8 |
+
Replace this with your actual face spoofing detection logic
|
| 9 |
+
"""
|
| 10 |
+
if frame is None:
|
| 11 |
+
return "No frame received", "", ""
|
| 12 |
+
|
| 13 |
+
# Simulate processing time (remove this in production)
|
| 14 |
+
time.sleep(0.1)
|
| 15 |
+
|
| 16 |
+
# Placeholder result - replace with your actual detection
|
| 17 |
+
is_real = np.random.choice([True, False], p=[0.7, 0.3])
|
| 18 |
+
confidence = np.random.uniform(0.8, 1.0)
|
| 19 |
+
|
| 20 |
+
result = "Real Face" if is_real else "Spoof Detected"
|
| 21 |
+
status = "Processing live feed..."
|
| 22 |
+
conf_text = f"{confidence:.2%}"
|
| 23 |
+
|
| 24 |
+
return status, result, conf_text
|
| 25 |
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# Real-Time Face Spoofing Detection")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column(scale=2):
|
| 31 |
+
# Main video feed
|
| 32 |
+
video_feed = gr.Image(label="Live Camera Feed")
|
| 33 |
+
|
| 34 |
+
with gr.Column(scale=1):
|
| 35 |
+
# Status and results
|
| 36 |
+
status_text = gr.Textbox(label="Status", value="Waiting for camera...")
|
| 37 |
+
result_text = gr.Textbox(label="Detection Result")
|
| 38 |
+
confidence_text = gr.Textbox(label="Confidence Score")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Control buttons
|
| 41 |
+
start_button = gr.Button("Start Detection", variant="primary")
|
| 42 |
+
stop_button = gr.Button("Stop", variant="secondary")
|
| 43 |
+
|
|
|
|
|
|
|
| 44 |
gr.Markdown("""
|
| 45 |
### Instructions:
|
| 46 |
+
1. Allow camera access when prompted
|
| 47 |
+
2. Click 'Start Detection' to begin real-time analysis
|
| 48 |
+
3. Click 'Stop' to pause the detection
|
| 49 |
+
|
| 50 |
+
### Note:
|
| 51 |
+
- Keep your face centered and well-lit
|
| 52 |
+
- Maintain a stable position for better results
|
| 53 |
+
- Detection results update in real-time
|
| 54 |
""")
|
| 55 |
+
|
| 56 |
+
# Update interval in seconds (adjust based on your needs)
|
| 57 |
+
PROCESS_INTERVAL = 0.1
|
| 58 |
+
|
| 59 |
+
# Handle start/stop functionality
|
| 60 |
+
def start_detection():
|
| 61 |
+
return gr.update(value="Processing live feed...")
|
| 62 |
+
|
| 63 |
+
def stop_detection():
|
| 64 |
+
return gr.update(value="Detection stopped")
|
| 65 |
+
|
| 66 |
+
# Event handlers
|
| 67 |
+
start_button.click(
|
| 68 |
+
fn=start_detection,
|
| 69 |
+
outputs=status_text,
|
| 70 |
+
every=PROCESS_INTERVAL
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Process frames when detection is running
|
| 74 |
+
video_feed.change(
|
| 75 |
+
fn=process_video_frame,
|
| 76 |
+
inputs=[video_feed],
|
| 77 |
+
outputs=[status_text, result_text, confidence_text],
|
| 78 |
+
every=PROCESS_INTERVAL
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
stop_button.click(
|
| 82 |
+
fn=stop_detection,
|
| 83 |
+
outputs=status_text,
|
| 84 |
+
cancels=[start_button, video_feed]
|
| 85 |
+
)
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
+
demo.launch()
|
| 89 |
+
|
| 90 |
+
# import gradio as gr
|
| 91 |
+
# import numpy as np
|
| 92 |
+
# import time
|
| 93 |
+
|
| 94 |
+
# def process_image(img):
|
| 95 |
+
# """Placeholder function - replace with your backend integration"""
|
| 96 |
+
# if img is None:
|
| 97 |
+
# return "No image provided", "", ""
|
| 98 |
+
# time.sleep(1) # Simulate processing
|
| 99 |
+
# return "Processing Complete", "Real Face", "Confidence: 95%"
|
| 100 |
+
|
| 101 |
+
# with gr.Blocks() as demo:
|
| 102 |
+
# gr.Markdown("# Face Spoofing Detection System")
|
| 103 |
+
|
| 104 |
+
# with gr.Tabs():
|
| 105 |
+
# with gr.Tab("Webcam Detection"):
|
| 106 |
+
# webcam = gr.Image(label="Webcam Feed")
|
| 107 |
+
# webcam_status = gr.Textbox(label="Status", value="Ready")
|
| 108 |
+
# webcam_result = gr.Textbox(label="Detection Result")
|
| 109 |
+
# webcam_conf = gr.Textbox(label="Confidence Score")
|
| 110 |
+
# webcam_button = gr.Button("Analyze")
|
| 111 |
+
|
| 112 |
+
# webcam_button.click(
|
| 113 |
+
# fn=process_image,
|
| 114 |
+
# inputs=webcam,
|
| 115 |
+
# outputs=[webcam_status, webcam_result, webcam_conf]
|
| 116 |
+
# )
|
| 117 |
+
|
| 118 |
+
# with gr.Tab("Image Upload"):
|
| 119 |
+
# image_input = gr.Image(label="Upload Image")
|
| 120 |
+
# image_status = gr.Textbox(label="Status", value="Ready")
|
| 121 |
+
# image_result = gr.Textbox(label="Detection Result")
|
| 122 |
+
# image_conf = gr.Textbox(label="Confidence Score")
|
| 123 |
+
# image_button = gr.Button("Analyze")
|
| 124 |
+
|
| 125 |
+
# image_button.click(
|
| 126 |
+
# fn=process_image,
|
| 127 |
+
# inputs=image_input,
|
| 128 |
+
# outputs=[image_status, image_result, image_conf]
|
| 129 |
+
# )
|
| 130 |
+
|
| 131 |
+
# gr.Markdown("""
|
| 132 |
+
# ### Instructions:
|
| 133 |
+
# 1. Choose either Webcam or Image Upload tab
|
| 134 |
+
# 2. For webcam: Allow camera access and take a photo
|
| 135 |
+
# 3. For images: Upload an image from your device
|
| 136 |
+
# 4. Click Analyze to process the image
|
| 137 |
+
# """)
|
| 138 |
+
|
| 139 |
+
# if __name__ == "__main__":
|
| 140 |
+
# demo.launch()
|