Guru-25 commited on
Commit
8f43a66
·
verified ·
1 Parent(s): 4aecca0
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -124,7 +124,7 @@ def analyze_video(input_video):
124
  out.release()
125
  return temp_path
126
 
127
- def process_webcam(state, log_output):
128
  """Process webcam frames in real-time and update log output"""
129
  if state is None:
130
  # Initialize state
@@ -133,7 +133,7 @@ def process_webcam(state, log_output):
133
  cap = cv2.VideoCapture(0)
134
 
135
  if not cap.isOpened():
136
- return None, None, "Error: Could not open webcam."
137
 
138
  GAZE_STABILITY_THRESHOLD = 0.5
139
  TIME_THRESHOLD = 15
@@ -150,6 +150,7 @@ def process_webcam(state, log_output):
150
  blink_count = 0
151
  start_time = time.time()
152
  is_unconscious = False
 
153
 
154
  state = {
155
  "gaze_predictor": gaze_predictor,
@@ -168,7 +169,8 @@ def process_webcam(state, log_output):
168
  "TIME_THRESHOLD": TIME_THRESHOLD,
169
  "BLINK_RATE_THRESHOLD": BLINK_RATE_THRESHOLD,
170
  "EYE_CLOSURE_THRESHOLD": EYE_CLOSURE_THRESHOLD,
171
- "HEAD_STABILITY_THRESHOLD": HEAD_STABILITY_THRESHOLD
 
172
  }
173
 
174
  # Extract state variables
@@ -178,11 +180,12 @@ def process_webcam(state, log_output):
178
  gaze_history = state["gaze_history"]
179
  head_history = state["head_history"]
180
  ear_history = state["ear_history"]
 
181
 
182
  # Capture frame
183
  ret, frame = cap.read()
184
  if not ret:
185
- return state, None, log_output + "\nError: Could not read from webcam."
186
 
187
  # Process frame
188
  head_pose_gaze, gaze_h, gaze_v = gaze_predictor.predict_gaze(frame)
@@ -274,22 +277,21 @@ def process_webcam(state, log_output):
274
  log_lines.append(log_text)
275
  if len(log_lines) > 20: # Keep only last 20 entries
276
  log_lines = log_lines[-20:]
277
- updated_log = "\n".join(log_lines)
278
 
279
  # Convert from BGR to RGB for Gradio
280
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
281
 
282
- return state, frame_rgb, updated_log
283
 
284
  def create_webcam_interface():
285
- webcam = gr.Image(source="webcam", streaming=True)
286
  log_output = gr.Textbox(label="Gaze Tracking Log", lines=10)
287
  processed_frame = gr.Image(label="Processed Frame")
288
 
289
  webcam_demo = gr.Interface(
290
  fn=process_webcam,
291
- inputs=[gr.State(), gr.State("")],
292
- outputs=[gr.State(), processed_frame, log_output],
293
  live=True,
294
  title="Real-time Gaze Tracking"
295
  )
@@ -305,11 +307,11 @@ def create_video_interface():
305
  )
306
  return video_demo
307
 
 
308
  demo = gr.TabbedInterface(
309
  [create_video_interface(), create_webcam_interface()],
310
  ["Video Upload", "Webcam"],
311
- title="Gaze Tracker",
312
- description="Analyze gaze and detect drowsiness in videos or using webcam."
313
  )
314
 
315
  if __name__ == "__main__":
 
124
  out.release()
125
  return temp_path
126
 
127
+ def process_webcam(state):
128
  """Process webcam frames in real-time and update log output"""
129
  if state is None:
130
  # Initialize state
 
133
  cap = cv2.VideoCapture(0)
134
 
135
  if not cap.isOpened():
136
+ return None, "Error: Could not open webcam.", None
137
 
138
  GAZE_STABILITY_THRESHOLD = 0.5
139
  TIME_THRESHOLD = 15
 
150
  blink_count = 0
151
  start_time = time.time()
152
  is_unconscious = False
153
+ log_output = ""
154
 
155
  state = {
156
  "gaze_predictor": gaze_predictor,
 
169
  "TIME_THRESHOLD": TIME_THRESHOLD,
170
  "BLINK_RATE_THRESHOLD": BLINK_RATE_THRESHOLD,
171
  "EYE_CLOSURE_THRESHOLD": EYE_CLOSURE_THRESHOLD,
172
+ "HEAD_STABILITY_THRESHOLD": HEAD_STABILITY_THRESHOLD,
173
+ "log_output": log_output
174
  }
175
 
176
  # Extract state variables
 
180
  gaze_history = state["gaze_history"]
181
  head_history = state["head_history"]
182
  ear_history = state["ear_history"]
183
+ log_output = state["log_output"]
184
 
185
  # Capture frame
186
  ret, frame = cap.read()
187
  if not ret:
188
+ return state, log_output + "\nError: Could not read from webcam.", None
189
 
190
  # Process frame
191
  head_pose_gaze, gaze_h, gaze_v = gaze_predictor.predict_gaze(frame)
 
277
  log_lines.append(log_text)
278
  if len(log_lines) > 20: # Keep only last 20 entries
279
  log_lines = log_lines[-20:]
280
+ state["log_output"] = "\n".join(log_lines)
281
 
282
  # Convert from BGR to RGB for Gradio
283
  frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
284
 
285
+ return state, state["log_output"], frame_rgb
286
 
287
  def create_webcam_interface():
 
288
  log_output = gr.Textbox(label="Gaze Tracking Log", lines=10)
289
  processed_frame = gr.Image(label="Processed Frame")
290
 
291
  webcam_demo = gr.Interface(
292
  fn=process_webcam,
293
+ inputs=[gr.State()],
294
+ outputs=[gr.State(), log_output, processed_frame],
295
  live=True,
296
  title="Real-time Gaze Tracking"
297
  )
 
307
  )
308
  return video_demo
309
 
310
+ # Create a tabbed interface without the unsupported 'description' parameter
311
  demo = gr.TabbedInterface(
312
  [create_video_interface(), create_webcam_interface()],
313
  ["Video Upload", "Webcam"],
314
+ title="Gaze Tracker"
 
315
  )
316
 
317
  if __name__ == "__main__":