-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
98 lines (69 loc) · 2.41 KB
/
test.py
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
from pynput.mouse import Controller as MouseController, Button
from pynput.keyboard import Controller as KeyboardController, Key
from math import dist
from enum import Enum
import time
from threading import Thread
from ultralytics import YOLO
class ConfigMapping(Enum):
MOVE_FORWARD = "w"
MOVE_LEFT = "a"
MOVE_BACKWARD = "s"
MOVE_RIGHT = "d"
MOVE_JUMP = Key.space
MOVE_SILENT = Key.shift_l
MOVE_CROUCH = Key.ctrl_l
MOUSE_FIRE = Button.left
MOUSE_ADS = Button.right
MOUSE_SWITCH = Button.scroll_down
class Movement(Enum):
FORWARD = "MOVE_FORWARD"
LEFT = "MOVE_LEFT"
BACKWARD = "MOVE_BACKWARD"
RIGHT = "MOVE_RIGHT"
JUMP = "MOVE_JUMP"
SILENT = "MOVE_SILENT"
CROUCH = "MOVE_CROUCH"
class InputService:
def __init__(self) -> None:
self.mouse = MouseController()
self.keyboard = KeyboardController()
def begin_movement(self, movement: Movement) -> None:
self.keyboard.press(ConfigMapping[movement.value])
def stop_movement(self, movement: Movement) -> None:
self.keyboard.release(ConfigMapping[movement.value])
def mouse_move(self, dx: int, dy: int) -> None:
self.mouse.move(dx, dy)
def mouse_fire(self) -> None:
self.mouse.click(ConfigMapping.MOUSE_FIRE)
def mouse_fire_press(self) -> None:
self.mouse.press(ConfigMapping.MOUSE_FIRE)
def mouse_fire_release(self) -> None:
self.mouse.release(ConfigMapping.MOUSE_FIRE)
def mouse_ads(self) -> None:
self.mouse.click(ConfigMapping.MOUSE_ADS)
def mouse_switch(self) -> None:
self.mouse.click(ConfigMapping.MOUSE_SWITCH)
lerp = 0.05
def calculate_pos(xyxy):
left, top, right, bottom = xyxy
width = 480
height = 640
return int((((left + right) / 2) + 1) * lerp), int((top + bottom) / 2 * lerp)
def thread_safe_predict(process_signal):
local_model = YOLO("yolov8n.pt")
local_model.to("cuda")
results = local_model.predict(0, stream=True, conf=0.5, iou=0.5, half=True)
for r in results:
for box in r.boxes:
label = local_model.names[int(box.cls)]
if label == "person":
b = box.xyxy[0]
x, y = calculate_pos(b)
print(x, y)
process_person(x, y)
svc = InputService()
def process_person(x, y):
svc.mouse_move(x, y)
thr = Thread(target=thread_safe_predict, args=(process_person,))
thr.start()