forked from reinesana/Doomscroll-Skyrim-Edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
193 lines (153 loc) · 5.93 KB
/
main.py
File metadata and controls
193 lines (153 loc) · 5.93 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import argparse
import cv2
import time
import subprocess
from pathlib import Path
import mediapipe as mp
# MediaPipe Tasks setup
BaseOptions = mp.tasks.BaseOptions
FaceLandmarker = mp.tasks.vision.FaceLandmarker
FaceLandmarkerOptions = mp.tasks.vision.FaceLandmarkerOptions
VisionRunningMode = mp.tasks.vision.RunningMode
Image = mp.Image
ImageFormat = mp.ImageFormat
# Video player (mpv)
class LinuxVideoPlayer:
def __init__(self, fullscreen=False):
self.process = None
self.fullscreen = fullscreen
def play(self, video_path: Path) -> None:
if self.process is not None:
return
cmd = [
"mpv",
"--ontop",
"--no-border",
"--geometry=360x780+25+45",
"--loop",
]
if self.fullscreen:
cmd.append("--fs")
cmd.append(str(video_path)),
self.process = subprocess.Popen(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
def close(self, video_path: Path) -> None:
if self.process is not None:
self.process.terminate()
self.process = None
# Draw warning overlay
def draw_warning(frame, text="lock in twin"):
h, w = frame.shape[:2]
box_w, box_h = 500, 70
x1 = (w - box_w) // 2
y1 = 24
x2 = x1 + box_w
y2 = y1 + box_h
overlay = frame.copy()
cv2.rectangle(overlay, (x1, y1), (x2, y2), (15, 0, 15), -1)
cv2.addWeighted(overlay, 0.55, frame, 0.45, 0, frame)
cv2.rectangle(frame, (x1 - 2, y1 - 2), (x2 + 2, y2 + 2), (80, 255, 160), 4)
cv2.rectangle(frame, (x1, y1), (x2, y2), (80, 255, 160), 2)
cv2.putText(
frame,
text.upper(),
(x1 + 26, y1 + 48),
cv2.FONT_HERSHEY_DUPLEX,
1.2,
(255, 255, 255),
3,
cv2.LINE_AA,
)
# Main
def main(timer, looking_threshold, debounce_threshold):
skyrim_video = Path("./assets/skyrim-skeleton.mp4").resolve()
model_file = Path("./assets/face_landmarker_full.task").resolve()
if not skyrim_video.exists() or not model_file.exists():
print("Missing skyrim-skeleton.mp4 or face_landmarker_full.task in ./assets/")
return
video_player = LinuxVideoPlayer(fullscreen=args.fullscreen)
# Initialize FaceLandmarker
options = FaceLandmarkerOptions(
base_options=BaseOptions(model_asset_path=str(model_file)),
running_mode=VisionRunningMode.VIDEO,
output_face_blendshapes=False,
output_facial_transformation_matrixes=False,
)
face_landmarker = FaceLandmarker.create_from_options(options)
cam = cv2.VideoCapture(0)
if not cam.isOpened():
print("Could not open webcam")
return
doomscroll = None
video_playing = False
while True:
ret, frame = cam.read()
if not ret:
continue
frame = cv2.flip(frame, 1)
height, width, _ = frame.shape
# Convert to MediaPipe Image
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
mp_image = Image(image_format=ImageFormat.SRGB, data=rgb_frame)
result = face_landmarker.detect_for_video(mp_image, int(time.time() * 1000))
if result.face_landmarks and len(result.face_landmarks) > 0:
lm = result.face_landmarks[0]
left = [lm[145], lm[159]]
right = [lm[374], lm[386]]
lx = int((left[0].x + left[1].x) / 2 * width)
ly = int((left[0].y + left[1].y) / 2 * height)
rx = int((right[0].x + right[1].x) / 2 * width)
ry = int((right[0].y + right[1].y) / 2 * height)
box = 50
cv2.rectangle(
frame, (lx - box, ly - box), (lx + box, ly + box), (10, 255, 0), 2
)
cv2.rectangle(
frame, (rx - box, ry - box), (rx + box, ry + box), (10, 255, 0), 2
)
l_iris = lm[468]
r_iris = lm[473]
l_ratio = (l_iris.y - left[1].y) / (left[0].y - left[1].y + 1e-6)
r_ratio = (r_iris.y - right[1].y) / (right[0].y - right[1].y + 1e-6)
avg_ratio = (l_ratio + r_ratio) / 2.0
if video_playing:
is_looking_down = avg_ratio < debounce_threshold
else:
is_looking_down = avg_ratio < looking_threshold
if is_looking_down:
if doomscroll is None:
doomscroll = time.time()
if (time.time() - doomscroll) >= timer:
if not video_playing:
video_player.play(skyrim_video)
video_playing = True
else:
doomscroll = None
if video_playing:
video_player.close(skyrim_video)
video_playing = False
else:
doomscroll = None
if video_playing:
video_player.close(skyrim_video)
video_playing = False
if video_playing:
draw_warning(frame, "doomscrolling alarm")
cv2.imshow("lock in", frame)
if cv2.waitKey(1) == 27:
break
if video_playing:
video_player.close(skyrim_video)
cam.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Doomscrolling alarm with MediaPipe.")
parser.add_argument("--timer", type=float, default=2.0, help="Seconds before video plays when looking down (default: 2.0)")
parser.add_argument("--looking_threshold", type=float, default=0.25, help="Threshold for initial look-down detection (default: 0.25)")
parser.add_argument("--debounce_threshold", type=float, default=0.45, help="Threshold for continuing look-down detection when video is playing (default: 0.45)")
parser.add_argument("--fullscreen", action="store_true", help="Start the video in fullscreen mode")
args = parser.parse_args()
main(args.timer, args.looking_threshold, args.debounce_threshold)