-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (85 loc) · 2.96 KB
/
main.py
File metadata and controls
106 lines (85 loc) · 2.96 KB
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
import argparse
import time
import cv2
from gaze_detector import GazeDetector
from alarm_controller import AlarmController
from config import DEFAULT_THRESHOLD_SECONDS, DEFAULT_GAZE_TOLERANCE, DEFAULT_VOLUME
def parse_args():
parser = argparse.ArgumentParser(
description="Play a meme video when you look away from the screen"
)
parser.add_argument("--video", required=True, help="Path to meme video file")
parser.add_argument(
"--threshold",
type=float,
default=DEFAULT_THRESHOLD_SECONDS,
help=f"Seconds before alarm triggers (default: {DEFAULT_THRESHOLD_SECONDS})",
)
parser.add_argument(
"--gaze-tolerance",
type=float,
default=DEFAULT_GAZE_TOLERANCE,
help=f"Gaze tolerance from center (default: {DEFAULT_GAZE_TOLERANCE})",
)
parser.add_argument(
"--volume",
type=int,
default=DEFAULT_VOLUME,
help=f"Video volume 0-100 (default: {DEFAULT_VOLUME})",
)
return parser.parse_args()
def main():
args = parse_args()
print(f"Starting LookAtScreen...")
print(f" Video: {args.video}")
print(f" Threshold: {args.threshold}s")
print(f" Gaze tolerance: {args.gaze_tolerance}")
print(f" Press 'q' to quit")
print()
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam")
return 1
detector = GazeDetector(tolerance=args.gaze_tolerance)
alarm = AlarmController(args.video, volume=args.volume)
looking_away_since = None
try:
while True:
ret, frame = cap.read()
if not ret:
print("Error: Could not read frame")
break
is_looking = detector.process_frame(frame)
if is_looking:
looking_away_since = None
if alarm.is_playing():
alarm.stop()
status = "LOOKING AT SCREEN"
color = (0, 255, 0)
else:
if looking_away_since is None:
looking_away_since = time.time()
elapsed = time.time() - looking_away_since
if elapsed >= args.threshold:
if not alarm.is_playing():
alarm.play()
status = "ALARM!"
color = (0, 0, 255)
else:
status = f"Looking away... {elapsed:.1f}s"
color = (0, 165, 255)
# Draw face/eye overlays
detector.draw_overlays(frame, color)
cv2.putText(frame, status, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2)
frame = cv2.flip(frame, 1)
cv2.imshow("LookAtScreen", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
finally:
alarm.cleanup()
detector.cleanup()
cap.release()
cv2.destroyAllWindows()
return 0
if __name__ == "__main__":
exit(main())