-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (43 loc) · 1.26 KB
/
main.py
File metadata and controls
59 lines (43 loc) · 1.26 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
import cv2
import numpy as np
import mss
import time
import threading
WIDTH, HEIGHT = 1920, 1080
fps = 60.0
frame_duration = 1.0 / fps
codec = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("Recording.avi", codec, fps, (WIDTH, HEIGHT))
cv2.namedWindow("Live", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Live", 480, 270)
monitor = {"top": 0, "left": 0, "width": WIDTH, "height": HEIGHT}
latest_frame = None
running = True
lock = threading.Lock()
# 🔹 Capture thread (mss MUST be created here)
def capture():
global latest_frame
sct = mss.mss() # ✅ create inside thread
while running:
img = sct.grab(monitor)
frame = np.array(img)[:, :, :3]
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
with lock:
latest_frame = frame
# Start capture thread
threading.Thread(target=capture, daemon=True).start()
next_frame_time = time.perf_counter()
while True:
now = time.perf_counter()
if now >= next_frame_time:
next_frame_time += frame_duration
with lock:
frame = latest_frame
if frame is not None:
out.write(frame)
cv2.imshow("Live", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
running = False
out.release()
cv2.destroyAllWindows()