Abhi-22 commited on
Commit
149db20
·
verified ·
1 Parent(s): b44bdba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -20
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  import numpy as np
3
  import time
 
 
 
 
4
 
5
  def process_video_frame(frame):
6
  """
@@ -23,6 +27,34 @@ def process_video_frame(frame):
23
 
24
  return status, result, conf_text
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  with gr.Blocks() as demo:
27
  gr.Markdown("# Real-Time Face Spoofing Detection")
28
 
@@ -53,35 +85,23 @@ with gr.Blocks() as demo:
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
  )
71
 
72
- # Process frames when detection is running
73
- video_feed.change(
74
- fn=process_video_frame,
75
- inputs=[video_feed],
76
- outputs=[status_text, result_text, confidence_text],
77
- every=PROCESS_INTERVAL
78
- )
79
-
80
  stop_button.click(
81
  fn=stop_detection,
82
- outputs=status_text,
83
- cancels=[video_feed]
84
  )
 
 
 
 
 
 
 
85
 
86
  if __name__ == "__main__":
87
  demo.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  import time
4
+ import threading
5
+
6
+ # Global flag to control detection
7
+ detection_active = False
8
 
9
  def process_video_frame(frame):
10
  """
 
27
 
28
  return status, result, conf_text
29
 
30
+ def start_detection():
31
+ """
32
+ Start the detection process
33
+ """
34
+ global detection_active
35
+ detection_active = True
36
+ return "Processing live feed..."
37
+
38
+ def stop_detection():
39
+ """
40
+ Stop the detection process
41
+ """
42
+ global detection_active
43
+ detection_active = False
44
+ return "Detection stopped"
45
+
46
+ def process_frames(video_feed, status_text, result_text, confidence_text):
47
+ """
48
+ Continuously process frames when detection is active
49
+ """
50
+ while detection_active:
51
+ if video_feed is not None:
52
+ status, result, conf = process_video_frame(video_feed)
53
+ status_text.update(value=status)
54
+ result_text.update(value=result)
55
+ confidence_text.update(value=conf)
56
+ time.sleep(0.1) # Adjust the sleep time as needed
57
+
58
  with gr.Blocks() as demo:
59
  gr.Markdown("# Real-Time Face Spoofing Detection")
60
 
 
85
  - Detection results update in real-time
86
  """)
87
 
 
 
 
 
 
 
 
 
 
 
88
  # Event handlers
89
  start_button.click(
90
  fn=start_detection,
91
  outputs=status_text
92
  )
93
 
 
 
 
 
 
 
 
 
94
  stop_button.click(
95
  fn=stop_detection,
96
+ outputs=status_text
 
97
  )
98
+
99
+ # Start a thread to process frames when detection is active
100
+ threading.Thread(
101
+ target=process_frames,
102
+ args=(video_feed, status_text, result_text, confidence_text),
103
+ daemon=True
104
+ ).start()
105
 
106
  if __name__ == "__main__":
107
  demo.launch()