-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatarecorder.py
71 lines (54 loc) · 1.62 KB
/
datarecorder.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
import numpy as np
from PIL import ImageGrab
import cv2
from util import timers, capture_keys
from image_processing.image import Image
from gtav import gtav_input
import os
def load_training_data():
file_name = 'training_data.npy'
training_data = []
if os.path.isfile(file_name):
print('[+] Found previous data. Loading...')
training_data = list(np.load(file_name, allow_pickle=True))
else:
print('[-] No previous training data found.')
return training_data
def dump_training_data(training_data):
file_name = 'training_data.npy'
np.save(file_name, training_data)
def keys_to_output(keys):
# check only A, W and D
output = [0, 0, 0]
if 'A' in keys:
output[0] = 1
elif 'D' in keys:
output[2] = 1
else:
output[1] = 1
return output
training_data = []
training_data = load_training_data()
timer = timers.Timers()
gameinput = gtav_input.GtaVInput()
while (True):
timer.start('per frame')
# grab screen
printscr = ImageGrab.grab(bbox=(0, 40, 800, 640))
image = Image.from_screen_caputre(printscr)
processed = image\
.copy()\
.bgr_to_gray()\
.resize((320, 240))
keys = capture_keys.key_check()
output = keys_to_output(keys)
training_data.append([processed.get_image(), output])
if len(training_data) % 500 == 0:
print('....')
dump_training_data(training_data)
cv2.imshow('window', processed.get_image())
timer.end('per frame')
# wait at least 25ms for a key event
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break