-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_run_model.py
More file actions
66 lines (46 loc) · 1.62 KB
/
server_run_model.py
File metadata and controls
66 lines (46 loc) · 1.62 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
import torch
from model_architecture import BasicOneFrame
import constants
import socket
import numpy as np
import cv2
import keyboard
FPS = 20
SCALE_DOWN = 5
FRAME_WIDTH = 1920//5
FRAME_HEIGHT = 1080//5
FRAME_BYTES = 3 * FRAME_WIDTH * FRAME_HEIGHT
print("FRAME BYTES: ", FRAME_BYTES)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("10.0.0.28", 8080))
sock.listen(1)
print("server is listening...")
connection, address = sock.accept()
is_run_button_pressed = False
is_running = False
nnet = BasicOneFrame()
nnet.load_state_dict(torch.load("./trained/behavior_clone.pth"))
while not keyboard.is_pressed("ctrl + c"):
data = b''
while len(data) < FRAME_BYTES:
packet = connection.recv(FRAME_BYTES - len(data))
data += packet
if is_running:
frame = np.frombuffer(data, dtype=np.uint8)
frame = frame.reshape(FRAME_HEIGHT, FRAME_WIDTH, 3)
#print(image.shape)
cv2.imshow('screen capture', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
frame = torch.from_numpy(frame).to(torch.float32) / 255.0
frame = frame.permute(1,2,0)
with torch.no_grad():
mouse_movements = nnet(frame)[0]
x_move = (mouse_movements[0] * (mouse_movements[2] * constants.THEORETICAL_MAX_MAGNITUDE)).item()
y_move = (mouse_movements[1] * (mouse_movements[2] * constants.THEORETICAL_MAX_MAGNITUDE)).item()
x_move = int(x_move)
y_move = int(y_move)
data_to_send = bytes(np.array([x_move, y_move]))
connection.sendall(data_to_send)
cv2.destroyAllWindows()
connection.close()